1 #=====================================================================
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
7 #=====================================================================
8 # SQL-Ledger Accounting
11 # Author: Dieter Simader
12 # Email: dsimader@sql-ledger.org
13 # Web: http://www.sql-ledger.org
17 # This program is free software; you can redistribute it and/or modify
18 # it under the terms of the GNU General Public License as published by
19 # the Free Software Foundation; either version 2 of the License, or
20 # (at your option) any later version.
22 # This program is distributed in the hope that it will be useful,
23 # but WITHOUT ANY WARRANTY; without even the implied warranty of
24 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 # GNU General Public License for more details.
26 # You should have received a copy of the GNU General Public License
27 # along with this program; if not, write to the Free Software
28 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 #======================================================================
31 # Accounts Payables database backend routines
33 #======================================================================
38 sub post_transaction {
39 $main::lxdebug->enter_sub();
41 my ($self, $myconfig, $form) = @_;
44 my $dbh = $form->dbconnect_noauto($myconfig);
46 my ($null, $taxrate, $amount);
49 ($null, $form->{department_id}) = split(/--/, $form->{department});
50 $form->{department_id} *= 1;
52 if ($form->{currency} eq $form->{defaultcurrency}) {
53 $form->{exchangerate} = 1;
55 $exchangerate = $form->check_exchangerate($myconfig, $form->{currency}, $form->{transdate}, 'sell');
57 $form->{exchangerate} = ($exchangerate) ? $exchangerate : $form->parse_amount($myconfig, $form->{exchangerate});
60 # reverse and parse amounts
62 $form->{"amount_$i"} = $form->round_amount($form->parse_amount($myconfig, $form->{"amount_$i"}) * $form->{exchangerate} * -1, 2);
63 $amount += ($form->{"amount_$i"} * -1);
67 $form->{amount} = $amount;
69 # taxincluded doesn't make sense if there is no amount
70 $form->{taxincluded} = 0 if ($form->{amount} == 0);
72 $query = qq| SELECT c.accno, t.rate FROM chart c, tax t where c.id=t.chart_id AND t.taxkey=$form->{taxkey}|;
73 $sth = $dbh->prepare($query);
74 $sth->execute || $form->dberror($query);
75 ($form->{AP}{"tax"}, $form->{taxrate}) = $sth->fetchrow_array;
78 $formtax = $form->parse_amount($myconfig, $form->{"tax"});
80 $form->{"tax"} = $form->{amount} * $form->{taxrate};
81 $form->{"tax"} = $form->round_amount($form->{"tax"} * $form->{exchangerate}, 2) * -1;
83 if ($form->{taxcheck}) {
84 $form->{"tax"} = $formtax * -1;
87 $form->{total_tax} += ($form->{"tax"} * -1);
91 # adjust paidaccounts if there is no date in the last row
92 $form->{paidaccounts}-- unless ($form->{"datepaid_$form->{paidaccounts}"});
96 for my $i (1 .. $form->{paidaccounts}) {
97 $form->{"paid_$i"} = $form->round_amount($form->parse_amount($myconfig, $form->{"paid_$i"}), 2);
99 $form->{invpaid} += $form->{"paid_$i"};
100 $form->{datepaid} = $form->{"datepaid_$i"};
104 $form->{invpaid} = $form->round_amount($form->{invpaid} * $form->{exchangerate}, 2);
106 if ($form->{taxincluded} *= 1) {
108 $tax = $form->{"amount_$i"} - ( $form->{"amount_$i"} / ($form->{taxrate} + 1)) ;
109 if ($form->{taxcheck}) {
110 $tax = $formtax *- 1;
112 $amount = $form->{"amount_$i"} - $tax;
113 $form->{"amount_$i"} = $form->round_amount($amount, 2);
114 $diff += $amount - $form->{"amount_$i"};
115 $form->{tax} = $form->round_amount($tax,2);
116 $form->{total_tax} = $form->{tax} * -1;
118 print (STDERR "Steuer $form->{tax}\n");
119 print (STDERR "TotalSteuer $form->{total_tax}\n");
120 # deduct taxes from amount
121 # $form->{amount} -= $form->{total_tax};
122 # deduct difference from amount_1
123 # $form->{amount_1} += $form->round_amount($diff, 2);
124 $form->{amount} = $form->{amount_1} * -1;
127 $form->{netamount} = $form->{amount};
129 # store invoice total, this goes into ap table
130 $form->{invtotal} = $form->{amount} + $form->{total_tax};
132 # amount for total AP
133 $form->{payables} = $form->{invtotal};
138 # if we have an id delete old records
141 # delete detail records
142 $query = qq|DELETE FROM acc_trans WHERE trans_id = $form->{id}|;
144 $dbh->do($query) || $form->dberror($query);
147 my $uid = rand().time;
149 $uid .= $form->{login};
151 $uid = substr($uid,2,75);
153 $query = qq|INSERT INTO ap (invnumber, employee_id)
154 VALUES ('$uid', (SELECT e.id FROM employee e
155 WHERE e.login = '$form->{login}') )|;
156 $dbh->do($query) || $form->dberror($query);
158 $query = qq|SELECT a.id FROM ap a
159 WHERE a.invnumber = '$uid'|;
160 $sth = $dbh->prepare($query);
161 $sth->execute || $form->dberror($query);
163 ($form->{id}) = $sth->fetchrow_array;
168 $form->{invnumber} = $form->{id} unless $form->{invnumber};
171 map { $form->{$_} =~ s/\'/\'\'/g } qw(invnumber ordnumber notes);
173 $form->{datepaid} = $form->{transdate} unless ($form->{datepaid});
174 my $datepaid = ($form->{invpaid} != 0) ? qq|'$form->{datepaid}'| : 'NULL';
176 $query = qq|UPDATE ap SET
177 invnumber = '$form->{invnumber}',
178 transdate = '$form->{transdate}',
179 ordnumber = '$form->{ordnumber}',
180 vendor_id = $form->{vendor_id},
181 taxincluded = '$form->{taxincluded}',
182 amount = $form->{invtotal},
183 duedate = '$form->{duedate}',
184 paid = $form->{invpaid},
185 datepaid = $datepaid,
186 netamount = $form->{netamount},
187 curr = '$form->{currency}',
188 notes = '$form->{notes}',
189 department_id = $form->{department_id}
190 WHERE id = $form->{id}
192 $dbh->do($query) || $form->dberror($query);
195 # update exchangerate
196 if (($form->{currency} ne $form->{defaultcurrency}) && !$exchangerate) {
197 $form->update_exchangerate($dbh, $form->{currency}, $form->{transdate}, 0, $form->{exchangerate});
200 # add individual transactions
201 foreach my $item (keys %{ $form->{AP} }) {
202 if ($form->{$item} != 0) {
203 $project_id = 'NULL';
204 if ($item =~ /amount_/) {
205 if ($form->{"project_id_$'"} && $form->{"projectnumber_$'"}) {
206 $project_id = $form->{"project_id_$'"};
210 # insert detail records in acc_trans
211 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate,
213 VALUES ($form->{id}, (SELECT c.id FROM chart c
214 WHERE c.accno = '$form->{AP}{$item}'),
215 $form->{$item}, '$form->{transdate}', $project_id, '$form->{taxkey}')|;
216 $dbh->do($query) || $form->dberror($query);
221 # if there is no amount but a payment record a payable
222 if ($form->{amount} == 0 && $form->{invtotal} == 0) {
223 $form->{payables} = $form->{invpaid};
226 # add paid transactions
227 for my $i (1 .. $form->{paidaccounts}) {
228 if ($form->{"paid_$i"} != 0) {
231 if ($form->{currency} eq $form->{defaultcurrency}) {
232 $form->{"exchangerate_$i"} = 1;
234 $exchangerate = $form->check_exchangerate($myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'sell');
236 $form->{"exchangerate_$i"} = ($exchangerate) ? $exchangerate : $form->parse_amount($myconfig, $form->{"exchangerate_$i"});
238 $form->{"AP_paid_$i"} =~ s/\"//g;
241 ($form->{AP}{"paid_$i"}) = split(/--/, $form->{"AP_paid_$i"});
242 $form->{"datepaid_$i"} = $form->{transdate} unless ($form->{"datepaid_$i"});
244 # if there is no amount and invtotal is zero there is no exchangerate
245 if ($form->{amount} == 0 && $form->{invtotal} == 0) {
246 $form->{exchangerate} = $form->{"exchangerate_$i"};
249 $amount = $form->round_amount($form->{"paid_$i"} * $form->{exchangerate} * -1, 2);
250 if ($form->{payables}) {
251 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,
254 (SELECT c.id FROM chart c
255 WHERE c.accno = '$form->{AP}{payables}'),
256 $amount, '$form->{"datepaid_$i"}')|;
257 $dbh->do($query) || $form->dberror($query);
259 $form->{payables} = $amount;
261 $form->{"memo_$i"} =~ s/\'/\'\'/g;
263 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,
264 transdate, source, memo)
266 (SELECT c.id FROM chart c
267 WHERE c.accno = '$form->{AP}{"paid_$i"}'),
268 $form->{"paid_$i"}, '$form->{"datepaid_$i"}',
269 '$form->{"source_$i"}', '$form->{"memo_$i"}')|;
270 $dbh->do($query) || $form->dberror($query);
272 # add exchange rate difference
273 $amount = $form->round_amount($form->{"paid_$i"} * ($form->{"exchangerate_$i"} - 1), 2);
275 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,
276 transdate, fx_transaction, cleared)
278 (SELECT c.id FROM chart c
279 WHERE c.accno = '$form->{AP}{"paid_$i"}'),
280 $amount, '$form->{"datepaid_$i"}', '1', '0')|;
282 $dbh->do($query) || $form->dberror($query);
285 # exchangerate gain/loss
286 $amount = $form->round_amount($form->{"paid_$i"} * ($form->{exchangerate} - $form->{"exchangerate_$i"}), 2);
289 $accno = ($amount > 0) ? $form->{fxgain_accno} : $form->{fxloss_accno};
290 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,
291 transdate, fx_transaction, cleared)
292 VALUES ($form->{id}, (SELECT c.id FROM chart c
293 WHERE c.accno = '$accno'),
294 $amount, '$form->{"datepaid_$i"}', '1', '0')|;
295 $dbh->do($query) || $form->dberror($query);
298 # update exchange rate record
299 if (($form->{currency} ne $form->{defaultcurrency}) && !$exchangerate) {
300 $form->update_exchangerate($dbh, $form->{currency}, $form->{"datepaid_$i"}, 0, $form->{"exchangerate_$i"});
305 my $rc = $dbh->commit;
308 $main::lxdebug->leave_sub();
316 sub delete_transaction {
317 $main::lxdebug->enter_sub();
319 my ($self, $myconfig, $form, $spool) = @_;
321 # connect to database
322 my $dbh = $form->dbconnect_noauto($myconfig);
324 my $query = qq|DELETE FROM ap WHERE id = $form->{id}|;
325 $dbh->do($query) || $form->dberror($query);
327 $query = qq|DELETE FROM acc_trans WHERE trans_id = $form->{id}|;
328 $dbh->do($query) || $form->dberror($query);
330 # commit and redirect
331 my $rc = $dbh->commit;
334 $main::lxdebug->leave_sub();
342 sub ap_transactions {
343 $main::lxdebug->enter_sub();
345 my ($self, $myconfig, $form) = @_;
347 # connect to database
348 my $dbh = $form->dbconnect($myconfig);
350 my $query = qq|SELECT a.id, a.invnumber, a.transdate, a.duedate,
351 a.amount, a.paid, a.ordnumber, v.name, a.invoice,
352 a.netamount, a.datepaid, a.notes, e.name AS employee
354 JOIN vendor v ON (a.vendor_id = v.id)
355 LEFT JOIN employee e ON (a.employee_id = e.id)|;
359 if ($form->{vendor_id}) {
360 $where .= " AND a.vendor_id = $form->{vendor_id}";
362 if ($form->{vendor}) {
363 my $vendor = $form->like(lc $form->{vendor});
364 $where .= " AND lower(v.name) LIKE '$vendor'";
367 if ($form->{department}) {
368 my ($null, $department_id) = split /--/, $form->{department};
369 $where .= " AND a.department_id = $department_id";
371 if ($form->{invnumber}) {
372 my $invnumber = $form->like(lc $form->{invnumber});
373 $where .= " AND lower(a.invnumber) LIKE '$invnumber'";
375 if ($form->{ordnumber}) {
376 my $ordnumber = $form->like(lc $form->{ordnumber});
377 $where .= " AND lower(a.ordnumber) LIKE '$ordnumber'";
379 if ($form->{notes}) {
380 my $notes = $form->like(lc $form->{notes});
381 $where .= " AND lower(a.notes) LIKE '$notes'";
384 $where .= " AND a.transdate >= '$form->{transdatefrom}'" if $form->{transdatefrom};
385 $where .= " AND a.transdate <= '$form->{transdateto}'" if $form->{transdateto};
386 if ($form->{open} || $form->{closed}) {
387 unless ($form->{open} && $form->{closed}) {
388 $where .= " AND a.amount <> a.paid" if ($form->{open});
389 $where .= " AND a.amount = a.paid" if ($form->{closed});
393 my @a = (transdate, invnumber, name);
394 push @a, "employee" if $self->{l_employee};
395 my $sortorder = join ', ', $form->sort_columns(@a);
396 $sortorder = $form->{sort} unless $sortorder;
398 $query .= "WHERE $where
399 ORDER by $sortorder";
401 my $sth = $dbh->prepare($query);
402 $sth->execute || $form->dberror($query);
404 while (my $ap = $sth->fetchrow_hashref(NAME_lc)) {
405 push @{ $form->{AP} }, $ap;
411 $main::lxdebug->leave_sub();