added Portuguese (European) language
[openemr.git] / accounting / ws_server.pl
blobfd29786729b4b95e9f476b51d9542250cb1b7b7c
1 #!/usr/bin/perl
3 ######################################################################
4 # This module is compatible only with SQL-Ledger version 2.4.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::AR;
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 my $res = Frontier::Responder->new( methods => {
32 'ezybiz.add_invoice' => $add_invoice,
33 'ezybiz.add_salesman' => $add_salesman,
34 'ezybiz.customer_balance' =>$customer_balance,
35 'ezybiz.add_customer' => $add_customer
36 }, );
38 print $res->answer;
40 sub rpc_customer_balance {
41 my ($post_hash) = @_;
42 if ($$post_hash{id} > 0 ) {
43 my $myconfig = new User "$memberfile", "$oemr_username";
44 $myconfig->{dbpasswd} = unpack 'u', $myconfig->{dbpasswd};
45 my $form = new Form;
46 $form->{title} = "AR Outstanding";
47 $form->{outstanding} = "1";
48 $form->{customer_id} = $$post_hash{id};
49 $form->{sort} = "transdate" ;
50 $form->{l_due} = 1;
51 $form->{nextsub} = "ar_transaction";
52 $form->{action} = 'Continue';
54 AR::ar_transactions("",\%$myconfig, \%$form);
56 my ($paid,$amount) = 0;
58 # Exclude invoices that are not yet due (i.e. waiting for insurance).
59 # We no longer use the due date for this; instead ar.notes identifies
60 # insurances used, and ar.shipvia indicates which of those are done.
61 # If all insurances are done, it's due.
63 foreach my $resref (@{$$form{transactions}}) {
64 my $inspending = 0;
65 foreach my $tmp ('Ins1','Ins2','Ins3') {
66 ++$inspending if ($$resref{notes} =~ /$tmp/ && $$resref{shipvia} !~ /$tmp/);
68 if ($inspending == 0) {
69 $paid += $$resref{paid};
70 $amount += $$resref{amount};
74 my $retval = $amount - $paid;
75 return($retval);
79 sub rpc_add_customer
81 use lib '/usr/lib/perl5/site_perl/5.8.3';
83 my ($post_hash) = @_;
85 #take struct of data and map to post data to create the customer, return the id
86 my $myconfig = new User "$memberfile", "$oemr_username";
87 $myconfig->{dbpasswd} = unpack 'u', $myconfig->{dbpasswd};
88 my $form = new Form;
89 $form->{name} = substr($$post_hash{'firstname'} . " " . $$post_hash{'lastname'}, 0, 64);
90 $form->{discount} = "";
91 $form->{terms} = "";
92 $form->{taxincluded} = "1";
93 $form->{creditlimit} = "0";
94 $form->{id} = $$post_hash{'foreign_id'};
95 $form->{login} = "";
96 $form->{employee} = "";
97 $form->{pricegroup} = "";
98 $form->{business} = "";
99 $form->{language} = "";
100 $form->{address1} = substr($$post_hash{'address'}, 0, 32);
101 $form->{address2} = substr($$post_hash{'address'}, 32, 32);
102 $form->{city} = substr($$post_hash{'suburb'}, 0, 32);
104 if($$post_hash{'state'}){
105 $form->{state} = substr($$post_hash{'state'}, 0, 32);
106 }else{
107 $form->{state} = substr($$post_hash{'geo_zone_id'}, 0, 32);
109 $form->{zipcode} = substr($$post_hash{'postcode'}, 0, 10);
110 $form->{country} = "";
111 $form->{contact} = "";
112 $form->{phone} = substr($$post_hash{'phone1'}, 0, 20);
113 $form->{fax} = "";
114 $form->{email} = $$post_hash{'email'};
115 $form->{taxnumber} = substr($$post_hash{'ssn'}, 0, 32);
116 $form->{curr} = "USD";
117 $form->{customernumber} = $$post_hash{'customernumber'};
118 @t = localtime(time);
119 $dd = $t[3];
120 $mm = $t[4] + 1;
121 $yy = $t[5] + 1900;
123 $form->{startdate} = "$mm-$dd-$yy";
125 CT::save_customer('', \%$myconfig, \%$form);
126 my $retVal = $form->{id};
128 return($retVal);
131 sub rpc_add_employee
133 my ($post_hash) = @_;
134 my $myconfig = new User "$memberfile", "$oemr_username";
135 $myconfig->{dbpasswd} = unpack 'u', $myconfig->{dbpasswd};
136 my $form = new Form;
137 $form->{id} = $$post_hash{'foreign_id'};
138 $form->{name} = $$post_hash{'fname'} . " " . $$post_hash{'lname'};
139 $form->{sales} = $$post_hash{'authorized'};
140 @t = localtime(time);
141 $dd = $t[3];
142 $mm = $t[4] + 1;
143 $yy = $t[5] + 1900;
145 $form->{startdate} = "$mm-$dd-$yy";
146 HR::save_employee("",\%$myconfig, \%$form);
147 my $retVal = $form->{id};
148 return($retVal);
151 sub rpc_add_invoice
153 my ($post_hash) = @_;
155 my $myconfig = new User "$memberfile", "$oemr_username";
156 $myconfig->{dbpasswd} = unpack 'u', $myconfig->{dbpasswd};
157 my $form = new Form;
158 $form->{id};
159 $form->{employee} = "--" . $$post_hash{'salesman'};
160 $form->{customer_id} = $$post_hash{'customerid'};
161 $form->{invnumber} = $$post_hash{'invoicenumber'};
162 $form->{amount} = $$post_hash{'total'};
163 $form->{netamount} = $$post_hash{'total'};
164 $form->{notes} = $$post_hash{'notes'};
165 $form->{department} = "";
166 $form->{currency} = "USD";
167 $form->{defaultcurrency} = "USD";
169 # This is the AR account number, needed by IS::post_invoice.
170 $form->{AR} = $oemr_ar_acc;
172 # This will use the posting date as the billing date
173 @t = localtime(time);
175 # $dd = $t[3];
176 # $mm = $t[4] + 1;
177 # $yy = $t[5] + 1900;
179 $form->{transdate} = sprintf("%02u-%02u-%04u", $t[4] + 1, $t[3], $t[5] + 1900);
181 # This overrides the above statement to use the date of service as the
182 # invoice date, which should be preferable for most practices. Comment
183 # out the following line if you really want the billing date instead.
185 $form->{transdate} = $$post_hash{'dosdate'};
187 # If there is insurance, set a future due date so we don't bother
188 # the patient for a while.
190 if ($$post_hash{'payer_id'}) {
191 @t = localtime(60 * 60 * 24 * $oemr_due_days + time);
192 $form->{duedate} = sprintf("%02u-%02u-%04u", $t[4] + 1, $t[3], $t[5] + 1900);
193 } else {
194 $form->{duedate} = $form->{transdate};
197 # Get out if the invoice already exists.
198 my $trans_id = 0;
199 my $dbh = $form->dbconnect($myconfig);
200 my $query = qq|SELECT id FROM ar WHERE invnumber = ?|;
201 my $eth = $dbh->prepare($query) || die "Failed to prepare ar query";
202 $eth->execute($$post_hash{'invoicenumber'}) || die "Failed to execute ar query";
203 ($trans_id) = $eth->fetchrow_array;
204 $eth->finish;
206 if ($trans_id) {
207 print STDERR "Skipping invoice $trans_id = " . $$post_hash{'invoicenumber'} . "\n";
208 $dbh->disconnect;
209 return 0;
212 #loop through line items and add them to invoice
213 my $i = 1;
214 my $j = 1; #this is for copays should only be one but who knows -j
215 my $count = 0;
216 my $items = $$post_hash{'items'};
218 foreach my $line_item (@$items)
220 if ($$line_item{'itemtext'} =~ /COPAY/) {
221 $form->{"datepaid_$j"} = $form->{transdate}; # "$mm-$dd-$yy";
222 # For copays we use a dummy procedure code because it may be applicable
223 # to multiple procedures during the visit.
224 $form->{"memo_$j"} = 'Co-pay';
225 # Put the payment method and check number in the source field if they are
226 # present (i.e. from pos_checkout.php).
227 if ($$line_item{'itemtext'} =~ /^COPAY:([A-Z].*)$/) {
228 $form->{"source_$j"} = $1;
229 } else {
230 $form->{"source_$j"} = 'Co-pay';
232 # $form->{"paid_$j"} = abs($$line_item{'price'});
233 $form->{"paid_$j"} = 0 - $$line_item{'price'};
234 $form->{"AR_paid_$j"} = "$oemr_cash_acc" . "--";
235 $j++;
237 else{
238 $form->{"qty_$i"} = $$line_item{'qty'};
239 $form->{"discount_$i"} = 0;
240 $form->{"sellprice_$i"} = $$line_item{'price'};
242 $form->{taxincluded} = 1;
243 $form->{"taxaccounts_$i"} = 0;
244 $form->{"income_accno_$i"} = $$line_item{'glaccountid'};
246 $form->{"id_$i"} = add_goodsandservices(\%$myconfig, \%$form, $oemr_services_partnumber,
247 'Medical Services', '');
249 $form->{"description_$i"} = $$line_item{'itemtext'};
250 $form->{"unit_$i"} = '';
251 $form->{"serialnumber_$i"} = $$line_item{'maincode'};
253 # Save the insurance company ID as the SL project ID. This gives us a way
254 # to associate each invoice item with its insurance payer. The clinic will
255 # probably want to write some reporting software taking advantage of this.
257 $form->{"projectnumber_$i"} = "--" . $$post_hash{'payer_id'}
258 if ($$post_hash{'payer_id'});
259 $i++;
263 $dbh->disconnect;
265 $form->{paidaccounts} = $j - 1;
266 $form->{rowcount} = $i - 1;
267 IS::post_invoice("", \%$myconfig, \%$form);
268 my $retVal = $form->{id};
269 return($retVal);
272 sub get_partid
274 my ($myconfig, $form, $number) = @_;
275 my $retval = 0;
276 # connect to database
277 my $dbh = $form->dbconnect($myconfig);
279 my $query = qq|SELECT id FROM parts WHERE partnumber = ?|;
280 my $eth = $dbh->prepare($query) || die "Failed to create select id from parts query";
281 $eth->execute($number) || die "Failed to execute select id from parts query";
282 ($retval) = $eth->fetchrow_array;
283 $eth->finish;
284 $dbh->disconnect;
285 return($retval);
288 sub add_goodsandservices
290 my ($myconfig, $form, $code, $desc, $price) = @_;
291 my $retval = 0;
292 $retval = get_partid($myconfig, $form, $code);
294 if($retval == 0)
296 # connect to database, turn off autocommit
297 my $dbh = $form->dbconnect_noauto($myconfig);
298 my $query = qq|insert into parts (partnumber,description,listprice,sellprice) values(?,?,?,?)|;
299 my $eth = $dbh->prepare($query) || die "failed to create insert into parts query" . $dbh->errstr;
300 $eth->execute($code,$desc,$price,$price) || die "failed to execute insert into parts query" . $dbh->errstr;
301 $dbh->commit || die $dbh->errstr;
302 $eth->finish || die "cannot finish " . $dbh->errstr;
303 $dbh->disconnect;
304 $retval = get_partid($myconfig, $form, $code);
307 return($retval);