Fully responsive globals.php with vertical menu (#2460)
[openemr.git] / library / payment.inc.php
blobb3c7cee05f417bca947ef7abdf828bffc1ae3408
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 https://www.open-emr.org
11 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
14 use OpenEMR\Billing\SLEOB;
15 use OpenEMR\Common\Logging\EventAuditLogger;
17 // Post a payment to the payments table.
19 function frontPayment($patient_id, $encounter, $method, $source, $amount1, $amount2, $timestamp, $auth = "")
22 if (empty($auth)) {
23 $auth=$_SESSION['authUser'];
26 $tmprow = sqlQuery(
27 "SELECT date FROM form_encounter WHERE " .
28 "encounter=? and pid=?",
29 array($encounter,$patient_id)
31 //the manipulation is done to insert the amount paid into payments table in correct order to show in front receipts report,
32 //if the payment is for today's encounter it will be shown in the report under today field and otherwise shown as previous
33 $tmprowArray=explode(' ', $tmprow['date']);
34 if (date('Y-m-d')==$tmprowArray[0]) {
35 if ($amount1==0) {
36 $amount1=$amount2;
37 $amount2=0;
39 } else {
40 if ($amount2==0) {
41 $amount2=$amount1;
42 $amount1=0;
46 $payid = sqlInsert("INSERT INTO payments ( " .
47 "pid, encounter, dtime, user, method, source, amount1, amount2 " .
48 ") VALUES ( ?, ?, ?, ?, ?, ?, ?, ?)", array($patient_id,$encounter,$timestamp,$auth,$method,$source,$amount1,$amount2));
49 return $payid;
52 //===============================================================================
53 //This section handles the common functins of payment screens.
54 //===============================================================================
55 function DistributionInsert($CountRow, $created_time, $user_id)
57 //Function inserts the distribution.Payment,Adjustment,Deductible,Takeback & Follow up reasons are inserted as seperate rows.
58 //It automatically pushes to next insurance for billing.
59 //In the screen a drop down of Ins1,Ins2,Ins3,Pat are given.The posting can be done for any level.
60 $Affected='no';
61 if (isset($_POST["Payment$CountRow"]) && $_POST["Payment$CountRow"]*1>0) {
62 if (trim(formData('type_name'))=='insurance') {
63 if (trim(formData("HiddenIns$CountRow"))==1) {
64 $AccountCode="IPP";
67 if (trim(formData("HiddenIns$CountRow"))==2) {
68 $AccountCode="ISP";
71 if (trim(formData("HiddenIns$CountRow"))==3) {
72 $AccountCode="ITP";
74 } elseif (trim(formData('type_name'))=='patient') {
75 $AccountCode="PP";
78 sqlBeginTrans();
79 $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"))));
80 sqlStatement("insert into ar_activity set " .
81 "pid = '" . trim(formData('hidden_patient_code')) .
82 "', encounter = '" . trim(formData("HiddenEncounter$CountRow")) .
83 "', sequence_no = '" . $sequence_no['increment'] .
84 "', code_type = '" . trim(formData("HiddenCodetype$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 "'");
96 sqlCommitTrans();
97 $Affected='yes';
100 if (isset($_POST["AdjAmount$CountRow"]) && $_POST["AdjAmount$CountRow"]*1!=0) {
101 if (trim(formData('type_name'))=='insurance') {
102 $AdjustString="Ins adjust Ins".trim(formData("HiddenIns$CountRow"));
103 $AccountCode="IA";
104 } elseif (trim(formData('type_name'))=='patient') {
105 $AdjustString="Pt adjust";
106 $AccountCode="PA";
109 sqlBeginTrans();
110 $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"))));
111 sqlStatement("insert into ar_activity set " .
112 "pid = '" . trim(formData('hidden_patient_code')) .
113 "', encounter = '" . trim(formData("HiddenEncounter$CountRow")) .
114 "', sequence_no = '" . $sequence_no['increment'] .
115 "', code_type = '" . trim(formData("HiddenCodetype$CountRow")) .
116 "', code = '" . trim(formData("HiddenCode$CountRow")) .
117 "', modifier = '" . trim(formData("HiddenModifier$CountRow")) .
118 "', payer_type = '" . trim(formData("HiddenIns$CountRow")) .
119 "', post_time = '" . trim($created_time) .
120 "', post_user = '" . trim($user_id) .
121 "', session_id = '" . trim(formData('payment_id')) .
122 "', modified_time = '" . trim($created_time) .
123 "', pay_amount = '" . 0 .
124 "', adj_amount = '" . trim(formData("AdjAmount$CountRow")) .
125 "', memo = '" . "$AdjustString" .
126 "', account_code = '" . "$AccountCode" .
127 "'");
128 sqlCommitTrans();
129 $Affected='yes';
132 if (isset($_POST["Deductible$CountRow"]) && $_POST["Deductible$CountRow"]*1>0) {
133 sqlBeginTrans();
134 $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"))));
135 sqlStatement("insert into ar_activity set " .
136 "pid = '" . trim(formData('hidden_patient_code')) .
137 "', encounter = '" . trim(formData("HiddenEncounter$CountRow")) .
138 "', sequence_no = '" . $sequence_no['increment'] .
139 "', code_type = '" . trim(formData("HiddenCodetype$CountRow")) .
140 "', code = '" . trim(formData("HiddenCode$CountRow")) .
141 "', modifier = '" . trim(formData("HiddenModifier$CountRow")) .
142 "', payer_type = '" . trim(formData("HiddenIns$CountRow")) .
143 "', post_time = '" . trim($created_time) .
144 "', post_user = '" . trim($user_id) .
145 "', session_id = '" . trim(formData('payment_id')) .
146 "', modified_time = '" . trim($created_time) .
147 "', pay_amount = '" . 0 .
148 "', adj_amount = '" . 0 .
149 "', memo = '" . "Deductible $".trim(formData("Deductible$CountRow")) .
150 "', account_code = '" . "Deduct" .
151 "'");
152 sqlCommitTrans();
153 $Affected='yes';
156 if (isset($_POST["Takeback$CountRow"]) && $_POST["Takeback$CountRow"]*1>0) {
157 sqlBeginTrans();
158 $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"))));
159 sqlStatement("insert into ar_activity set " .
160 "pid = '" . trim(formData('hidden_patient_code')) .
161 "', encounter = '" . trim(formData("HiddenEncounter$CountRow")) .
162 "', sequence_no = '" . $sequence_no['increment'] .
163 "', code_type = '" . trim(formData("HiddenCodetype$CountRow")) .
164 "', code = '" . trim(formData("HiddenCode$CountRow")) .
165 "', modifier = '" . trim(formData("HiddenModifier$CountRow")) .
166 "', payer_type = '" . trim(formData("HiddenIns$CountRow")) .
167 "', post_time = '" . trim($created_time) .
168 "', post_user = '" . trim($user_id) .
169 "', session_id = '" . trim(formData('payment_id')) .
170 "', modified_time = '" . trim($created_time) .
171 "', pay_amount = '" . trim(formData("Takeback$CountRow"))*-1 .
172 "', adj_amount = '" . 0 .
173 "', account_code = '" . "Takeback" .
174 "'");
175 sqlCommitTrans();
176 $Affected='yes';
179 if (isset($_POST["FollowUp$CountRow"]) && $_POST["FollowUp$CountRow"]=='y') {
180 sqlBeginTrans();
181 $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"))));
182 sqlStatement("insert into ar_activity set " .
183 "pid = '" . trim(formData('hidden_patient_code')) .
184 "', encounter = '" . trim(formData("HiddenEncounter$CountRow")) .
185 "', sequence_no = '" . $sequence_no['increment'] .
186 "', code_type = '" . trim(formData("HiddenCodetype$CountRow")) .
187 "', code = '" . trim(formData("HiddenCode$CountRow")) .
188 "', modifier = '" . trim(formData("HiddenModifier$CountRow")) .
189 "', payer_type = '" . trim(formData("HiddenIns$CountRow")) .
190 "', post_time = '" . trim($created_time) .
191 "', post_user = '" . trim($user_id) .
192 "', session_id = '" . trim(formData('payment_id')) .
193 "', modified_time = '" . trim($created_time) .
194 "', pay_amount = '" . 0 .
195 "', adj_amount = '" . 0 .
196 "', follow_up = '" . "y" .
197 "', follow_up_note = '" . trim(formData("FollowUpReason$CountRow")) .
198 "'");
199 sqlCommitTrans();
200 $Affected='yes';
203 if ($Affected=='yes') {
204 if (trim(formData('type_name'))!='patient') {
205 $ferow = sqlQuery("select last_level_closed from form_encounter where
206 pid ='".trim(formData('hidden_patient_code'))."' and encounter='".trim(formData("HiddenEncounter$CountRow"))."'");
207 //multiple charges can come.
208 if ($ferow['last_level_closed']<trim(formData("HiddenIns$CountRow"))) {
209 sqlStatement("update form_encounter set last_level_closed='".trim(formData("HiddenIns$CountRow"))."' where
210 pid ='".trim(formData('hidden_patient_code'))."' and encounter='".trim(formData("HiddenEncounter$CountRow"))."'");
211 //last_level_closed gets increased.
212 //-----------------------------------
213 // Determine the next insurance level to be billed.
214 $ferow = sqlQuery("SELECT date, last_level_closed " .
215 "FROM form_encounter WHERE " .
216 "pid = '".trim(formData('hidden_patient_code'))."' AND encounter = '".trim(formData("HiddenEncounter$CountRow"))."'");
217 $date_of_service = substr($ferow['date'], 0, 10);
218 $new_payer_type = 0 + $ferow['last_level_closed'];
219 if ($new_payer_type <= 3 && !empty($ferow['last_level_closed']) || $new_payer_type == 0) {
220 ++$new_payer_type;
223 $new_payer_id = SLEOB::arGetPayerID(trim(formData('hidden_patient_code')), $date_of_service, $new_payer_type);
224 if ($new_payer_id>0) {
225 SLEOB::arSetupSecondary(trim(formData('hidden_patient_code')), trim(formData("HiddenEncounter$CountRow")), 0);
228 //-----------------------------------
233 //===============================================================================
234 // Delete rows, with logging, for the specified table using the
235 // specified WHERE clause. Borrowed from deleter.php.
237 function row_delete($table, $where)
239 $tres = sqlStatement("SELECT * FROM " . escape_table_name($table) . " WHERE $where");
240 $count = 0;
241 while ($trow = sqlFetchArray($tres)) {
242 $logstring = "";
243 foreach ($trow as $key => $value) {
244 if (! $value || $value == '0000-00-00 00:00:00') {
245 continue;
248 if ($logstring) {
249 $logstring .= " ";
252 $logstring .= $key . "='" . addslashes($value) . "'";
255 EventAuditLogger::instance()->newEvent("delete", $_SESSION['authUser'], $_SESSION['authProvider'], 1, "$table: $logstring");
256 ++$count;
259 if ($count) {
260 $query = "DELETE FROM " . escape_table_name($table) . " WHERE $where";
261 sqlStatement($query);
264 //===============================================================================