Fix a bug when calling from webinterface.
[openemr.git] / accounting / ws_server_26.pl
blobc298e0782fb7c4976fcf05249c4538377a681fe2
1 #!/usr/bin/perl
3 ######################################################################
4 # This module is compatible only with SQL-Ledger version 2.6.x.
5 # Copy it to your SQL-Ledger installation directory as ws_server.pl.
6 ######################################################################
8 use Frontier::Responder;
9 use DBI;
11 ######################################################################
12 # IMPORTANT - modify this to point to your SQL-Ledger installation!
13 ######################################################################
14 use lib qw (/var/www/sql-ledger);
16 use SL::User;
17 use SL::Form;
18 use SL::CT;
19 use SL::HR;
20 use SL::IS;
21 use SL::IC;
22 use SL::AA;
24 require "sql-ledger.conf";
26 my $add_customer = \&rpc_add_customer;
27 my $add_salesman = \&rpc_add_employee;
28 my $add_invoice = \&rpc_add_invoice;
29 my $customer_balance = \&rpc_customer_balance;
31 # In case we are running under Windows, do not strip carriage returns
32 # from POSTed data, otherwise Frontier::Responder may fail.
33 binmode(STDIN);
35 my $res = Frontier::Responder->new( methods => {
36 'ezybiz.add_invoice' => $add_invoice,
37 'ezybiz.add_salesman' => $add_salesman,
38 'ezybiz.customer_balance' =>$customer_balance,
39 'ezybiz.add_customer' => $add_customer
40 }, );
42 print $res->answer;
44 sub rpc_customer_balance {
45 my ($post_hash) = @_;
46 if ($$post_hash{id} > 0 ) {
47 my $myconfig = new User "$memberfile", "$oemr_username";
48 $myconfig->{dbpasswd} = unpack 'u', $myconfig->{dbpasswd};
49 my $form = new Form;
50 $form->{title} = "AR Outstanding";
51 $form->{outstanding} = "1";
52 $form->{customer_id} = $$post_hash{id};
53 $form->{sort} = "transdate" ;
54 $form->{l_due} = 1;
55 $form->{nextsub} = "transaction";
56 $form->{vc} = "customer" ;
57 $form->{action} = 'Continue';
59 AA::transactions("",\%$myconfig, \%$form);
61 my ($paid,$amount) = 0;
63 # Exclude invoices that are not yet due (i.e. waiting for insurance).
64 # We no longer use the due date for this; instead ar.notes identifies
65 # insurances used, and ar.shipvia indicates which of those are done.
66 # If all insurances are done, it's due.
68 foreach my $resref (@{$$form{transactions}}) {
69 my $inspending = 0;
70 foreach my $tmp ('Ins1','Ins2','Ins3') {
71 ++$inspending if ($$resref{notes} =~ /$tmp/ && $$resref{shipvia} !~ /$tmp/);
73 if ($inspending == 0) {
74 $paid += $$resref{paid};
75 $amount += $$resref{amount};
79 my $retval = $amount - $paid;
80 return($retval);
84 sub rpc_add_customer
86 use lib '/usr/lib/perl5/site_perl/5.8.3';
88 my ($post_hash) = @_;
90 #take struct of data and map to post data to create the customer, return the id
91 my $myconfig = new User "$memberfile", "$oemr_username";
92 $myconfig->{dbpasswd} = unpack 'u', $myconfig->{dbpasswd};
93 my $form = new Form;
94 $form->{name} = substr($$post_hash{'firstname'} . " " . $$post_hash{'lastname'}, 0, 64);
95 $form->{discount} = "";
96 $form->{terms} = "";
97 $form->{taxincluded} = "1";
98 $form->{creditlimit} = "0";
99 $form->{id} = $$post_hash{'foreign_id'};
100 $form->{login} = "";
101 $form->{employee} = "";
102 $form->{pricegroup} = "";
103 $form->{business} = "";
104 $form->{language} = "";
105 $form->{address1} = substr($$post_hash{'address'}, 0, 32);
106 $form->{address2} = substr($$post_hash{'address'}, 32, 32);
107 $form->{city} = substr($$post_hash{'suburb'}, 0, 32);
109 if($$post_hash{'state'}){
110 $form->{state} = substr($$post_hash{'state'}, 0, 32);
111 }else{
112 $form->{state} = substr($$post_hash{'geo_zone_id'}, 0, 32);
114 $form->{zipcode} = substr($$post_hash{'postcode'}, 0, 10);
115 $form->{country} = "";
116 $form->{contact} = "";
117 $form->{phone} = substr($$post_hash{'phone1'}, 0, 20);
118 $form->{fax} = "";
119 $form->{email} = $$post_hash{'email'};
120 $form->{taxnumber} = substr($$post_hash{'ssn'}, 0, 32);
121 $form->{curr} = "USD";
122 $form->{customernumber} = $$post_hash{'customernumber'};
123 @t = localtime(time);
124 $dd = $t[3];
125 $mm = $t[4] + 1;
126 $yy = $t[5] + 1900;
128 $form->{startdate} = "$mm-$dd-$yy";
130 CT::save_customer('', \%$myconfig, \%$form);
131 my $retVal = $form->{id};
133 return($retVal);
136 sub rpc_add_employee
138 my ($post_hash) = @_;
139 my $myconfig = new User "$memberfile", "$oemr_username";
140 $myconfig->{dbpasswd} = unpack 'u', $myconfig->{dbpasswd};
141 my $form = new Form;
142 $form->{id} = $$post_hash{'foreign_id'};
143 $form->{name} = $$post_hash{'fname'} . " " . $$post_hash{'lname'};
144 $form->{sales} = $$post_hash{'authorized'};
145 @t = localtime(time);
146 $dd = $t[3];
147 $mm = $t[4] + 1;
148 $yy = $t[5] + 1900;
150 $form->{startdate} = "$mm-$dd-$yy";
151 HR::save_employee("",\%$myconfig, \%$form);
152 my $retVal = $form->{id};
153 return($retVal);
156 sub rpc_add_invoice
158 my ($post_hash) = @_;
160 my $myconfig = new User "$memberfile", "$oemr_username";
161 $myconfig->{dbpasswd} = unpack 'u', $myconfig->{dbpasswd};
162 my $form = new Form;
163 $form->{id};
164 $form->{employee} = "--" . $$post_hash{'salesman'};
165 $form->{customer_id} = $$post_hash{'customerid'};
166 $form->{invnumber} = $$post_hash{'invoicenumber'};
167 $form->{amount} = $$post_hash{'total'};
168 $form->{netamount} = $$post_hash{'total'};
169 $form->{notes} = $$post_hash{'notes'};
170 $form->{department} = "";
171 $form->{currency} = "USD";
172 $form->{defaultcurrency} = "USD";
174 # This is the AR account number, needed by IS::post_invoice.
175 $form->{AR} = $oemr_ar_acc;
177 # This will use the posting date as the billing date
178 @t = localtime(time);
180 # $dd = $t[3];
181 # $mm = $t[4] + 1;
182 # $yy = $t[5] + 1900;
184 $form->{transdate} = sprintf("%02u-%02u-%04u", $t[4] + 1, $t[3], $t[5] + 1900);
186 # This overrides the above statement to use the date of service as the
187 # invoice date, which should be preferable for most practices. Comment
188 # out the following line if you really want the billing date instead.
190 $form->{transdate} = $$post_hash{'dosdate'};
192 # If there is insurance, set a future due date so we don't bother
193 # the patient for a while.
195 if ($$post_hash{'payer_id'}) {
196 @t = localtime(60 * 60 * 24 * $oemr_due_days + time);
197 $form->{duedate} = sprintf("%02u-%02u-%04u", $t[4] + 1, $t[3], $t[5] + 1900);
198 } else {
199 $form->{duedate} = $form->{transdate};
202 # Get out if the invoice already exists.
203 my $trans_id = 0;
204 my $dbh = $form->dbconnect($myconfig);
205 my $query = qq|SELECT id FROM ar WHERE invnumber = ?|;
206 my $eth = $dbh->prepare($query) || die "Failed to prepare ar query";
207 $eth->execute($$post_hash{'invoicenumber'}) || die "Failed to execute ar query";
208 ($trans_id) = $eth->fetchrow_array;
209 $eth->finish;
211 if ($trans_id) {
212 print STDERR "Skipping invoice $trans_id = " . $$post_hash{'invoicenumber'} . "\n";
213 $dbh->disconnect;
214 return 0;
217 #loop through line items and add them to invoice
218 my $i = 1;
219 my $j = 1; #this is for copays should only be one but who knows -j
220 my $count = 0;
221 my $items = $$post_hash{'items'};
223 foreach my $line_item (@$items)
225 if ($$line_item{'itemtext'} =~ /COPAY/) {
226 $form->{"datepaid_$j"} = $form->{transdate}; # "$mm-$dd-$yy";
227 # For copays we use a dummy procedure code because it may be applicable
228 # to multiple procedures during the visit.
229 $form->{"memo_$j"} = 'Co-pay';
230 # Put the payment method and check number in the source field if they are
231 # present (i.e. from pos_checkout.php).
232 if ($$line_item{'itemtext'} =~ /^COPAY:([A-Z].*)$/) {
233 $form->{"source_$j"} = $1;
234 } else {
235 $form->{"source_$j"} = 'Co-pay';
237 # $form->{"paid_$j"} = abs($$line_item{'price'});
238 $form->{"paid_$j"} = 0 - $$line_item{'price'};
239 $form->{"AR_paid_$j"} = "$oemr_cash_acc" . "--";
240 $j++;
242 else{
243 my $chart_id = 0;
244 my $query = qq|SELECT id FROM chart WHERE accno = ?|;
245 my $eth = $dbh->prepare($query) || die "Failed to prepare chart query";
246 $eth->execute($$line_item{'glaccountid'}) || die "Failed to execute chart query";
247 ($chart_id) = $eth->fetchrow_array;
248 $eth->finish;
250 $form->{"qty_$i"} = $$line_item{'qty'};
251 $form->{"discount_$i"} = 0;
252 $form->{"sellprice_$i"} = $$line_item{'price'};
254 $form->{taxincluded} = 1;
255 $form->{"taxaccounts_$i"} = 0;
256 $form->{"income_accno_$i"} = $$line_item{'glaccountid'};
257 $form->{"income_accno_id_$i"} = $chart_id;
259 $form->{"id_$i"} = add_goodsandservices(\%$myconfig, \%$form, $oemr_services_partnumber,
260 'Medical Services', '');
262 $form->{"description_$i"} = $$line_item{'itemtext'};
263 $form->{"unit_$i"} = '';
264 $form->{"serialnumber_$i"} = $$line_item{'maincode'};
266 # Save the insurance company ID as the SL project ID. This gives us a way
267 # to associate each invoice item with its insurance payer. The clinic will
268 # probably want to write some reporting software taking advantage of this.
270 $form->{"projectnumber_$i"} = "--" . $$post_hash{'payer_id'}
271 if ($$post_hash{'payer_id'});
272 $i++;
276 $dbh->disconnect;
278 $form->{paidaccounts} = $j - 1;
279 $form->{rowcount} = $i - 1;
280 IS::post_invoice("", \%$myconfig, \%$form);
281 my $retVal = $form->{id};
282 return($retVal);
285 sub get_partid
287 my ($myconfig, $form, $number) = @_;
288 my $retval = 0;
289 # connect to database
290 my $dbh = $form->dbconnect($myconfig);
292 my $query = qq|SELECT id FROM parts WHERE partnumber = ?|;
293 my $eth = $dbh->prepare($query) || die "Failed to create select id from parts query";
294 $eth->execute($number) || die "Failed to execute select id from parts query";
295 ($retval) = $eth->fetchrow_array;
296 $eth->finish;
297 $dbh->disconnect;
298 return($retval);
301 sub add_goodsandservices
303 my ($myconfig, $form, $code, $desc, $price) = @_;
304 my $retval = 0;
305 $retval = get_partid($myconfig, $form, $code);
307 if($retval == 0)
309 # connect to database, turn off autocommit
310 my $dbh = $form->dbconnect_noauto($myconfig);
311 my $query = qq|insert into parts (partnumber,description,listprice,sellprice) values(?,?,?,?)|;
312 my $eth = $dbh->prepare($query) || die "failed to create insert into parts query" . $dbh->errstr;
313 $eth->execute($code,$desc,$price,$price) || die "failed to execute insert into parts query" . $dbh->errstr;
314 $dbh->commit || die $dbh->errstr;
315 $eth->finish || die "cannot finish " . $dbh->errstr;
316 $dbh->disconnect;
317 $retval = get_partid($myconfig, $form, $code);
320 return($retval);