fixed bug with moving documents across patients when the target pubpid != pid
[openemr.git] / accounting / ws_server.pl
blob8a2b5e71ce71812a3c48509d29f3dce13110d7f2
1 #!/usr/bin/perl
3 use Frontier::Responder;
4 use DBI;
6 # You need to modify this as needed:
7 use lib qw (/srv/www/sql-ledger);
9 use SL::User;
10 use SL::Form;
11 use SL::CT;
12 use SL::HR;
13 use SL::IS;
14 use SL::IC;
15 use SL::AR;
16 require "sql-ledger.conf";
18 my $add_customer = \&rpc_add_customer;
19 my $add_salesman = \&rpc_add_employee;
20 my $add_invoice = \&rpc_add_invoice;
21 my $customer_balance = \&rpc_customer_balance;
23 my $res = Frontier::Responder->new( methods => {
24 'ezybiz.add_invoice' => $add_invoice,
25 'ezybiz.add_salesman' => $add_salesman,
26 'ezybiz.customer_balance' =>$customer_balance,
27 'ezybiz.add_customer' => $add_customer
28 }, );
30 print $res->answer;
32 sub rpc_customer_balance {
33 my ($post_hash) = @_;
34 if ($$post_hash{id} > 0 ) {
35 my $myconfig = new User "$memberfile", "$oemr_username";
36 $myconfig->{dbpasswd} = unpack 'u', $myconfig->{dbpasswd};
37 my $form = new Form;
38 $form->{title} = "AR Outstanding";
39 $form->{outstanding} = "1";
40 $form->{customer_id} = $$post_hash{id};
41 $form->{sort} = "transdate" ;
42 $form->{l_due} = 1;
43 $form->{nextsub} = "ar_transaction";
44 $form->{action} = 'Continue';
46 AR::ar_transactions("",\%$myconfig, \%$form);
47 my ($paid,$amount) =0;
49 # Exclude invoices that are not yet due (i.e. waiting for insurance).
51 my @now = localtime;
52 my $today = sprintf("%04u-%02u-%02u", $now[5] + 1900, $now[4] + 1, $now[3]);
53 foreach my $resref (@{$$form{transactions}}) {
54 my $duedate = substr($$resref{duedate}, 6) . "-" . substr($$resref{duedate}, 0, 5);
55 if ($duedate le $today) {
56 $paid += $$resref{paid};
57 $amount += $$resref{amount};
61 my $retval = $amount - $paid;
62 return($retval);
66 sub rpc_add_customer
68 use lib '/usr/lib/perl5/site_perl/5.8.3';
70 my ($post_hash) = @_;
72 #take struct of data and map to post data to create the customer, return the id
73 my $myconfig = new User "$memberfile", "$oemr_username";
74 $myconfig->{dbpasswd} = unpack 'u', $myconfig->{dbpasswd};
75 my $form = new Form;
76 $form->{name} = substr($$post_hash{'firstname'} . " " . $$post_hash{'lastname'}, 0, 64);
77 $form->{discount} = "";
78 $form->{terms} = "";
79 $form->{taxincluded} = "1";
80 $form->{creditlimit} = "0";
81 $form->{id} = $$post_hash{'foreign_id'};
82 $form->{login} = "";
83 $form->{employee} = "";
84 $form->{pricegroup} = "";
85 $form->{business} = "";
86 $form->{language} = "";
87 $form->{address1} = substr($$post_hash{'address'}, 0, 32);
88 $form->{address2} = substr($$post_hash{'address'}, 32, 32);
89 $form->{city} = substr($$post_hash{'suburb'}, 0, 32);
91 if($$post_hash{'state'}){
92 $form->{state} = substr($$post_hash{'state'}, 0, 32);
93 }else{
94 $form->{state} = substr($$post_hash{'geo_zone_id'}, 0, 32);
96 $form->{zipcode} = substr($$post_hash{'postcode'}, 0, 10);
97 $form->{country} = "";
98 $form->{contact} = "";
99 $form->{phone} = substr($$post_hash{'phone1'}, 0, 20);
100 $form->{fax} = "";
101 $form->{email} = $$post_hash{'email'};
102 $form->{taxnumber} = substr($$post_hash{'ssn'}, 0, 32);
103 $form->{curr} = "USD";
104 $form->{customernumber} = $$post_hash{'customernumber'};
105 @t = localtime(time);
106 $dd = $t[3];
107 $mm = $t[4] + 1;
108 $yy = $t[5] + 1900;
110 $form->{startdate} = "$mm-$dd-$yy";
112 CT::save_customer('', \%$myconfig, \%$form);
113 my $retVal = $form->{id};
115 return($retVal);
118 sub rpc_add_employee
120 my ($post_hash) = @_;
121 my $myconfig = new User "$memberfile", "$oemr_username";
122 $myconfig->{dbpasswd} = unpack 'u', $myconfig->{dbpasswd};
123 my $form = new Form;
124 $form->{id} = $$post_hash{'foreign_id'};
125 $form->{name} = $$post_hash{'fname'} . " " . $$post_hash{'lname'};
126 $form->{sales} = $$post_hash{'authorized'};
127 @t = localtime(time);
128 $dd = $t[3];
129 $mm = $t[4] + 1;
130 $yy = $t[5] + 1900;
132 $form->{startdate} = "$mm-$dd-$yy";
133 HR::save_employee("",\%$myconfig, \%$form);
134 my $retVal = $form->{id};
135 return($retVal);
138 sub rpc_add_invoice
140 my ($post_hash) = @_;
142 my $myconfig = new User "$memberfile", "$oemr_username";
143 $myconfig->{dbpasswd} = unpack 'u', $myconfig->{dbpasswd};
144 my $form = new Form;
145 $form->{id};
146 $form->{employee} = "--" . $$post_hash{'salesman'};
147 $form->{customer_id} = $$post_hash{'customerid'};
148 $form->{invnumber} = $$post_hash{'invoicenumber'};
149 $form->{amount} = $$post_hash{'total'};
150 $form->{netamount} = $$post_hash{'total'};
151 $form->{notes} = $$post_hash{'notes'};
152 $form->{department} = "";
153 $form->{currency} = "USD";
154 $form->{defaultcurrency} = "USD";
156 # This is the AR account number, needed by IS::post_invoice.
157 $form->{AR} = $oemr_ar_acc;
159 # This will use the posting date as the billing date
160 @t = localtime(time);
161 $dd = $t[3];
162 $mm = $t[4] + 1;
163 $yy = $t[5] + 1900;
165 $form->{transdate} = sprintf("%02u-%02u-%04u", $t[4] + 1, $t[3], $t[5] + 1900);
167 # This overrides the above statement to use the date of service as the
168 # invoice date, which should be preferable for most practices. Comment
169 # out the following line if you really want the billing date instead.
171 $form->{transdate} = $$post_hash{'dosdate'};
173 # If there is insurance, set a future due date so we don't bother
174 # the patient for a while.
176 if ($$post_hash{'payer_id'}) {
177 @t = localtime(60 * 60 * 24 * $oemr_due_days + time);
178 $form->{duedate} = sprintf("%02u-%02u-%04u", $t[4] + 1, $t[3], $t[5] + 1900);
179 } else {
180 $form->{duedate} = $form->{transdate};
183 # Get out if the invoice already exists.
184 my $trans_id = 0;
185 my $dbh = $form->dbconnect($myconfig);
186 my $query = qq|SELECT id FROM ar WHERE invnumber = ?|;
187 my $eth = $dbh->prepare($query) || die "Failed to prepare ar query";
188 $eth->execute($$post_hash{'invoicenumber'}) || die "Failed to execute ar query";
189 ($trans_id) = $eth->fetchrow_array;
190 $eth->finish;
191 $dbh->disconnect;
192 if ($trans_id) {
193 print STDERR "Skipping invoice $trans_id = " . $$post_hash{'invoicenumber'} . "\n";
194 return 0;
197 #loop through line items and add them to invoice
198 my $i = 1;
199 my $j = 1; #this is for copays should only be one but who knows -j
200 my $count = 0;
201 my $items = $$post_hash{'items'};
203 foreach my $line_item (@$items)
205 if($$line_item{'itemtext'} =~ /COPAY/){
206 $form->{"datepaid_$j"} = "$mm-$dd-$yy";
207 # For copays we use a dummy procedure code because it may be applicable
208 # to multiple procedures during the visit.
209 $form->{"memo_$j"} = 'Co-pay';
210 # Put the payment method and check number in the source field if they are
211 # present (i.e. from pos_checkout.php).
212 if ($$line_item{'itemtext'} =~ /^COPAY:([A-Z].*)$/) {
213 $form->{"source_$j"} = $1;
214 } else {
215 $form->{"source_$j"} = 'Co-pay';
217 $form->{"paid_$j"} = abs($$line_item{'price'});
218 $form->{"AR_paid_$j"} = "$oemr_cash_acc" . "--";
219 $j++;
221 else{
222 $form->{"qty_$i"} = $$line_item{'qty'};
223 $form->{"discount_$i"} = 0;
224 $form->{"sellprice_$i"} = $$line_item{'price'};
226 $form->{taxincluded} = 1;
227 $form->{"taxaccounts_$i"} = 0;
228 $form->{"income_accno_$i"} = $$line_item{'glaccountid'};
230 $form->{"id_$i"} = add_goodsandservices(\%$myconfig, \%$form, $oemr_services_partnumber,
231 'Medical Services', '');
233 $form->{"description_$i"} = $$line_item{'itemtext'};
234 $form->{"unit_$i"} = '';
235 $form->{"serialnumber_$i"} = $$line_item{'maincode'};
237 # Save the insurance company ID as the SL project ID. This gives us a way
238 # to associate each invoice item with its insurance payer. The clinic will
239 # probably want to write some reporting software taking advantage of this.
241 $form->{"projectnumber_$i"} = "--" . $$post_hash{'payer_id'}
242 if ($$post_hash{'payer_id'});
243 $i++;
247 $form->{paidaccounts} = $j - 1;
248 $form->{rowcount} = $i - 1;
249 IS::post_invoice("", \%$myconfig, \%$form);
250 my $retVal = $form->{id};
251 return($retVal);
254 sub get_partid
256 my ($myconfig, $form, $number) = @_;
257 my $retval = 0;
258 # connect to database
259 my $dbh = $form->dbconnect($myconfig);
261 my $query = qq|SELECT id FROM parts WHERE partnumber = ?|;
262 my $eth = $dbh->prepare($query) || die "Failed to create select id from parts query";
263 $eth->execute($number) || die "Failed to execute select id from parts query";
264 ($retval) = $eth->fetchrow_array;
265 $eth->finish;
266 $dbh->disconnect;
267 return($retval);
270 sub add_goodsandservices
272 my ($myconfig, $form, $code, $desc, $price) = @_;
273 my $retval = 0;
274 $retval = get_partid($myconfig, $form, $code);
276 if($retval == 0)
278 # connect to database, turn off autocommit
279 my $dbh = $form->dbconnect_noauto($myconfig);
280 my $query = qq|insert into parts (partnumber,description,listprice,sellprice) values(?,?,?,?)|;
281 my $eth = $dbh->prepare($query) || die "failed to create insert into parts query" . $dbh->errstr;
282 $eth->execute($code,$desc,$price,$price) || die "failed to execute insert into parts query" . $dbh->errstr;
283 $dbh->commit || die $dbh->errstr;
284 $eth->finish || die "cannot finish " . $dbh->errstr;
285 $dbh->disconnect;
286 $retval = get_partid($myconfig, $form, $code);
289 return($retval);