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 # General ledger backend code
34 # DS. 2000-07-04 Created
35 # DS. 2001-06-12 Changed relations from accno to chart_id
37 #======================================================================
44 sub delete_transaction {
45 my ($self, $myconfig, $form) = @_;
46 $main::lxdebug->enter_sub();
49 my $dbh = $form->dbconnect_noauto($myconfig);
51 my $query = qq|DELETE FROM gl WHERE id = $form->{id}|;
52 $dbh->do($query) || $form->dberror($query);
54 $query = qq|DELETE FROM acc_trans WHERE trans_id = $form->{id}|;
55 $dbh->do($query) || $form->dberror($query);
58 my $rc = $dbh->commit;
60 $main::lxdebug->leave_sub();
66 sub post_transaction {
67 my ($self, $myconfig, $form) = @_;
68 $main::lxdebug->enter_sub();
70 my ($debit, $credit) = (0, 0);
75 # check if debit and credit balances
77 if ($form->{storno}) {
78 $form->{reference} = "Storno-" . $form->{reference};
79 $form->{description} = "Storno-" . $form->{description};
82 # connect to database, turn off AutoCommit
83 my $dbh = $form->dbconnect_noauto($myconfig);
85 # post the transaction
86 # make up a unique handle and store in reference field
87 # then retrieve the record based on the unique handle to get the id
88 # replace the reference field with the actual variable
89 # add records to acc_trans
91 # if there is a $form->{id} replace the old transaction
92 # delete all acc_trans entries and add the new ones
95 map { $form->{$_} =~ s/\'/\'\'/g } qw(reference description notes);
97 if (!$form->{taxincluded}) {
98 $form->{taxincluded} = 0;
105 # delete individual transactions
106 $query = qq|DELETE FROM acc_trans
107 WHERE trans_id = $form->{id}|;
108 $dbh->do($query) || $form->dberror($query);
112 $uid .= $form->{login};
114 $query = qq|INSERT INTO gl (reference, employee_id)
115 VALUES ('$uid', (SELECT e.id FROM employee e
116 WHERE e.login = '$form->{login}'))|;
117 $dbh->do($query) || $form->dberror($query);
119 $query = qq|SELECT g.id FROM gl g
120 WHERE g.reference = '$uid'|;
121 $sth = $dbh->prepare($query);
122 $sth->execute || $form->dberror($query);
124 ($form->{id}) = $sth->fetchrow_array;
129 my ($null, $department_id) = split /--/, $form->{department};
132 $query = qq|UPDATE gl SET
133 reference = '$form->{reference}',
134 description = '$form->{description}',
135 notes = '$form->{notes}',
136 transdate = '$form->{transdate}',
137 department_id = $department_id,
138 taxincluded = '$form->{taxincluded}'
139 WHERE id = $form->{id}|;
141 $dbh->do($query) || $form->dberror($query);
142 ($taxkey, $rate) = split(/--/, $form->{taxkey});
144 # insert acc_trans transactions
145 for $i (1 .. $form->{rowcount}) {
149 print(STDERR $form->{"taxchart_$i"}, "TAXCHART\n");
150 my ($accno) = split(/--/, $form->{"accno_$i"});
151 my ($taxkey, $rate) = split(/--/, $form->{"taxchart_$i"});
152 ($form->{"tax_id_$i"}, $NULL) = split /--/, $form->{"taxchart_$i"};
153 if ($form->{"tax_id_$i"} ne "") {
154 $query = qq|SELECT t.taxkey, t.rate
156 WHERE t.id=$form->{"tax_id_$i"}|;
158 $sth = $dbh->prepare($query);
159 $sth->execute || $form->dberror($query);
161 $sth->fetchrow_array;
166 my $debit = $form->{"debit_$i"};
167 my $credit = $form->{"credit_$i"};
168 my $tax = $form->{"tax_$i"};
175 $amount = $debit * -1;
180 $project_id = conv_i($form->{"project_id_$i"});
182 # if there is an amount, add the record
184 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate,
185 source, memo, project_id, taxkey)
187 ($form->{id}, (SELECT c.id
189 WHERE c.accno = '$accno'),
190 $amount, '$form->{transdate}', |
191 . $dbh->quote($form->{"source_$i"}) . qq|, |
192 . $dbh->quote($form->{"memo_$i"}) . qq|,
195 do_query($form, $dbh, $query, $project_id);
200 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate,
201 source, memo, project_id, taxkey)
203 ($form->{id}, (SELECT t.chart_id
205 WHERE t.id = $form->{"tax_id_$i"}),
206 $tax, '$form->{transdate}', |
207 . $dbh->quote($form->{"source_$i"}) . qq|, |
208 . $dbh->quote($form->{"memo_$i"}) . qq|, ?, $taxkey)|;
210 do_query($form, $dbh, $query, $project_id);
214 my %audittrail = (tablename => 'gl',
215 reference => $form->{reference},
216 formname => 'transaction',
220 # $form->audittrail($dbh, "", \%audittrail);
222 # commit and redirect
223 my $rc = $dbh->commit;
225 $main::lxdebug->leave_sub();
231 sub all_transactions {
232 my ($self, $myconfig, $form) = @_;
233 $main::lxdebug->enter_sub();
235 # connect to database
236 my $dbh = $form->dbconnect($myconfig);
237 my ($query, $sth, $source, $null);
239 my ($glwhere, $arwhere, $apwhere) = ("1 = 1", "1 = 1", "1 = 1");
241 if ($form->{reference}) {
242 $source = $form->like(lc $form->{reference});
243 $glwhere .= " AND lower(g.reference) LIKE '$source'";
244 $arwhere .= " AND lower(a.invnumber) LIKE '$source'";
245 $apwhere .= " AND lower(a.invnumber) LIKE '$source'";
247 if ($form->{department}) {
248 ($null, $source) = split /--/, $form->{department};
249 $glwhere .= " AND g.department_id = $source";
250 $arwhere .= " AND a.department_id = $source";
251 $apwhere .= " AND a.department_id = $source";
254 if ($form->{source}) {
255 $source = $form->like(lc $form->{source});
256 $glwhere .= " AND lower(ac.source) LIKE '$source'";
257 $arwhere .= " AND lower(ac.source) LIKE '$source'";
258 $apwhere .= " AND lower(ac.source) LIKE '$source'";
260 if ($form->{datefrom}) {
261 $glwhere .= " AND ac.transdate >= '$form->{datefrom}'";
262 $arwhere .= " AND ac.transdate >= '$form->{datefrom}'";
263 $apwhere .= " AND ac.transdate >= '$form->{datefrom}'";
265 if ($form->{dateto}) {
266 $glwhere .= " AND ac.transdate <= '$form->{dateto}'";
267 $arwhere .= " AND ac.transdate <= '$form->{dateto}'";
268 $apwhere .= " AND ac.transdate <= '$form->{dateto}'";
270 if ($form->{description}) {
271 my $description = $form->like(lc $form->{description});
272 $glwhere .= " AND lower(g.description) LIKE '$description'";
273 $arwhere .= " AND lower(ct.name) LIKE '$description'";
274 $apwhere .= " AND lower(ct.name) LIKE '$description'";
276 if ($form->{notes}) {
277 my $notes = $form->like(lc $form->{notes});
278 $glwhere .= " AND lower(g.notes) LIKE '$notes'";
279 $arwhere .= " AND lower(a.notes) LIKE '$notes'";
280 $apwhere .= " AND lower(a.notes) LIKE '$notes'";
282 if ($form->{accno}) {
283 $glwhere .= " AND c.accno = '$form->{accno}'";
284 $arwhere .= " AND c.accno = '$form->{accno}'";
285 $apwhere .= " AND c.accno = '$form->{accno}'";
287 if ($form->{category} ne 'X') {
289 " AND gl.id in (SELECT trans_id FROM acc_trans ac2 WHERE ac2.chart_id IN (SELECT id FROM chart c2 WHERE c2.category = '$form->{category}'))";
291 " AND ar.id in (SELECT trans_id FROM acc_trans ac2 WHERE ac2.chart_id IN (SELECT id FROM chart c2 WHERE c2.category = '$form->{category}'))";
293 " AND ap.id in (SELECT trans_id FROM acc_trans ac2 WHERE ac2.chart_id IN (SELECT id FROM chart c2 WHERE c2.category = '$form->{category}'))";
295 if ($form->{project_id}) {
296 $glwhere .= " AND g.id IN (SELECT DISTINCT trans_id FROM acc_trans WHERE project_id = " . conv_i($form->{project_id}, 'NULL') . ")";
298 " AND ((a.globalproject_id = " . conv_i($form->{project_id}, 'NULL') . ") OR " .
299 " (a.id IN (SELECT DISTINCT trans_id FROM acc_trans WHERE project_id = " . conv_i($form->{project_id}, 'NULL') . ")))";
301 " AND ((a.globalproject_id = " . conv_i($form->{project_id}, 'NULL') . ") OR " .
302 " (a.id IN (SELECT DISTINCT trans_id FROM acc_trans WHERE project_id = " . conv_i($form->{project_id}, 'NULL') . ")))";
305 my ($project_columns, %project_join);
306 if ($form->{"l_projectnumbers"}) {
307 $project_columns = ", ac.project_id, pr.projectnumber";
308 $project_join = "LEFT JOIN project pr ON (ac.project_id = pr.id)";
311 if ($form->{accno}) {
313 # get category for account
314 $query = qq|SELECT c.category
316 WHERE c.accno = '$form->{accno}'|;
317 $sth = $dbh->prepare($query);
319 $sth->execute || $form->dberror($query);
320 ($form->{ml}) = $sth->fetchrow_array;
323 if ($form->{datefrom}) {
324 $query = qq|SELECT SUM(ac.amount)
325 FROM acc_trans ac, chart c
326 WHERE ac.chart_id = c.id
327 AND c.accno = '$form->{accno}'
328 AND ac.transdate < date '$form->{datefrom}'
330 $sth = $dbh->prepare($query);
331 $sth->execute || $form->dberror($query);
333 ($form->{balance}) = $sth->fetchrow_array;
338 my $false = ($myconfig->{dbdriver} eq 'Pg') ? FALSE: q|'0'|;
340 my $sortorder = join ', ',
341 $form->sort_columns(qw(transdate reference source description accno));
342 my %ordinal = (transdate => 6,
346 map { $sortorder =~ s/$_/$ordinal{$_}/ } keys %ordinal;
349 $sortorder = $form->{sort} . ",";
355 qq|SELECT ac.oid AS acoid, g.id, 'gl' AS type, $false AS invoice, g.reference, ac.taxkey, c.link,
356 g.description, ac.transdate, ac.source, ac.trans_id,
357 ac.amount, c.accno, g.notes, t.chart_id, ac.oid
359 FROM gl g, acc_trans ac $project_join, chart c LEFT JOIN tax t ON
362 AND ac.chart_id = c.id
363 AND g.id = ac.trans_id
365 SELECT ac.oid AS acoid, a.id, 'ar' AS type, a.invoice, a.invnumber, ac.taxkey, c.link,
366 ct.name, ac.transdate, ac.source, ac.trans_id,
367 ac.amount, c.accno, a.notes, t.chart_id, ac.oid
369 FROM ar a, acc_trans ac $project_join, customer ct, chart c LEFT JOIN tax t ON
372 AND ac.chart_id = c.id
373 AND a.customer_id = ct.id
374 AND a.id = ac.trans_id
376 SELECT ac.oid AS acoid, a.id, 'ap' AS type, a.invoice, a.invnumber, ac.taxkey, c.link,
377 ct.name, ac.transdate, ac.source, ac.trans_id,
378 ac.amount, c.accno, a.notes, t.chart_id, ac.oid
380 FROM ap a, acc_trans ac $project_join, vendor ct, chart c LEFT JOIN tax t ON
383 AND ac.chart_id = c.id
384 AND a.vendor_id = ct.id
385 AND a.id = ac.trans_id
386 ORDER BY $sortorder transdate, trans_id, acoid, taxkey DESC|;
388 # Show all $query in Debuglevel LXDebug::QUERY
389 $callingdetails = (caller (0))[3];
390 $main::lxdebug->message(LXDebug::QUERY, "$callingdetails \$query=\n $query");
392 my $sth = $dbh->prepare($query);
393 $sth->execute || $form->dberror($query);
397 while (my $ref0 = $sth->fetchrow_hashref(NAME_lc)) {
399 $trans_id = $ref0->{id};
401 if ($trans_id != $trans_id2) { # first line of a booking
404 push @{ $form->{GL} }, $ref;
409 $trans_id2 = $ref->{id};
412 if ($ref->{type} eq "gl") {
413 $ref->{module} = "gl";
417 if ($ref->{type} eq "ap") {
418 if ($ref->{invoice}) {
419 $ref->{module} = "ir";
421 $ref->{module} = "ap";
426 if ($ref->{type} eq "ar") {
427 if ($ref->{invoice}) {
428 $ref->{module} = "is";
430 $ref->{module} = "ar";
434 $ref->{"projectnumbers"} = {};
435 $ref->{"projectnumbers"}->{$ref->{"projectnumber"}} = 1 if ($ref->{"projectnumber"});
437 $balance = $ref->{amount};
439 # Linenumbers of General Ledger
440 $k = 0; # Debit # AP # Soll
441 $l = 0; # Credit # AR # Haben
442 $i = 0; # Debit Tax # AP_tax # VSt
443 $j = 0; # Credit Tax # AR_tax # USt
446 if ($ref->{chart_id} > 0) { # all tax accounts first line, no line increasing
447 if ($ref->{amount} < 0) {
448 if ($ref->{link} =~ /AR_tax/) {
449 $ref->{credit_tax}{$j} = $ref->{amount};
450 $ref->{credit_tax_accno}{$j} = $ref->{accno};
452 if ($ref->{link} =~ /AP_tax/) {
453 $ref->{debit_tax}{$i} = $ref->{amount} * -1;
454 $ref->{debit_tax_accno}{$i} = $ref->{accno};
457 if ($ref->{link} =~ /AR_tax/) {
458 $ref->{credit_tax}{$j} = $ref->{amount};
459 $ref->{credit_tax_accno}{$j} = $ref->{accno};
461 if ($ref->{link} =~ /AP_tax/) {
462 $ref->{debit_tax}{$i} = $ref->{amount} * -1;
463 $ref->{debit_tax_accno}{$i} = $ref->{accno};
466 } else { #all other accounts first line
467 if ($ref->{amount} < 0) {
468 $ref->{debit}{$k} = $ref->{amount} * -1;
469 $ref->{debit_accno}{$k} = $ref->{accno};
470 $ref->{debit_taxkey}{$k} = $ref->{taxkey};
471 $ref->{ac_transdate}{$k} = $ref->{transdate};
474 $ref->{credit}{$l} = $ref->{amount} * 1;
475 $ref->{credit_accno}{$l} = $ref->{accno};
476 $ref->{credit_taxkey}{$l} = $ref->{taxkey};
477 $ref->{ac_transdate}{$l} = $ref->{transdate};
483 } else { # following lines of a booking, line increasing
486 $trans_old =$trans_id2;
487 $trans_id2 = $ref2->{id};
490 (int($balance * 100000) + int(100000 * $ref2->{amount})) / 100000;
492 $ref->{"projectnumbers"}->{$ref2->{"projectnumber"}} = 1 if ($ref2->{"projectnumber"});
494 if ($ref2->{chart_id} > 0) { # all tax accounts, following lines
495 if ($ref2->{amount} < 0) {
496 if ($ref2->{link} =~ /AR_tax/) {
497 if ($ref->{credit_tax_accno}{$j} ne "") {
500 $ref->{credit_tax}{$j} = $ref2->{amount};
501 $ref->{credit_tax_accno}{$j} = $ref2->{accno};
503 if ($ref2->{link} =~ /AP_tax/) {
504 if ($ref->{debit_tax_accno}{$i} ne "") {
507 $ref->{debit_tax}{$i} = $ref2->{amount} * -1;
508 $ref->{debit_tax_accno}{$i} = $ref2->{accno};
511 if ($ref2->{link} =~ /AR_tax/) {
512 if ($ref->{credit_tax_accno}{$j} ne "") {
515 $ref->{credit_tax}{$j} = $ref2->{amount};
516 $ref->{credit_tax_accno}{$j} = $ref2->{accno};
518 if ($ref2->{link} =~ /AP_tax/) {
519 if ($ref->{debit_tax_accno}{$i} ne "") {
522 $ref->{debit_tax}{$i} = $ref2->{amount} * -1;
523 $ref->{debit_tax_accno}{$i} = $ref2->{accno};
526 } else { # all other accounts, following lines
527 if ($ref2->{amount} < 0) {
528 if ($ref->{debit_accno}{$k} ne "") {
531 $ref->{debit}{$k} = $ref2->{amount} * - 1;
532 $ref->{debit_accno}{$k} = $ref2->{accno};
533 $ref->{debit_taxkey}{$k} = $ref2->{taxkey};
534 $ref->{ac_transdate}{$k} = $ref2->{transdate};
536 if ($ref->{credit_accno}{$l} ne "") {
539 $ref->{credit}{$l} = $ref2->{amount};
540 $ref->{credit_accno}{$l} = $ref2->{accno};
541 $ref->{credit_taxkey}{$l} = $ref2->{taxkey};
542 $ref->{ac_transdate}{$l} = $ref2->{transdate};
547 push @{ $form->{GL} }, $ref;
550 if ($form->{accno}) {
552 qq|SELECT c.description FROM chart c WHERE c.accno = '$form->{accno}'|;
553 $sth = $dbh->prepare($query);
554 $sth->execute || $form->dberror($query);
556 ($form->{account_description}) = $sth->fetchrow_array;
560 $main::lxdebug->leave_sub();
567 my ($self, $myconfig, $form) = @_;
568 $main::lxdebug->enter_sub();
570 my ($query, $sth, $ref);
572 # connect to database
573 my $dbh = $form->dbconnect($myconfig);
576 $query = "SELECT closedto, revtrans
578 $sth = $dbh->prepare($query);
579 $sth->execute || $form->dberror($query);
581 ($form->{closedto}, $form->{revtrans}) = $sth->fetchrow_array;
584 $query = "SELECT g.reference, g.description, g.notes, g.transdate,
585 d.description AS department, e.name as employee, g.taxincluded, g.gldate
587 LEFT JOIN department d ON (d.id = g.department_id)
588 LEFT JOIN employee e ON (e.id = g.employee_id)
589 WHERE g.id = $form->{id}";
590 $sth = $dbh->prepare($query);
591 $sth->execute || $form->dberror($query);
592 $ref = $sth->fetchrow_hashref(NAME_lc);
593 map { $form->{$_} = $ref->{$_} } keys %$ref;
596 # retrieve individual rows
597 $query = qq|SELECT c.accno, t.taxkey AS accnotaxkey, a.amount, a.memo,
598 a.transdate, a.cleared, a.project_id, p.projectnumber,
599 a.taxkey, t.rate AS taxrate, t.id, (SELECT c1.accno FROM chart c1, tax t1 WHERE t1.id=t.id AND c1.id=t.chart_id) AS taxaccno, (SELECT tk.tax_id FROM taxkeys tk WHERE tk.chart_id =a.chart_id AND tk.startdate<=a.transdate ORDER BY tk.startdate desc LIMIT 1) AS tax_id
601 JOIN chart c ON (c.id = a.chart_id)
602 LEFT JOIN project p ON (p.id = a.project_id)
603 LEFT JOIN tax t ON (t.id=(SELECT tk.tax_id from taxkeys tk WHERE (tk.taxkey_id=a.taxkey) AND ((CASE WHEN a.chart_id IN (SELECT chart_id FROM taxkeys WHERE taxkey_id=a.taxkey) THEN tk.chart_id=a.chart_id ELSE 1=1 END) OR (c.link LIKE '%tax%')) AND startdate <=a.transdate ORDER BY startdate DESC LIMIT 1))
604 WHERE a.trans_id = $form->{id}
605 AND a.fx_transaction = '0'
606 ORDER BY a.oid,a.transdate|;
608 $sth = $dbh->prepare($query);
609 $sth->execute || $form->dberror($query);
612 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
613 push @{ $form->{GL} }, $ref;
616 # get tax description
617 $query = qq| SELECT * FROM tax t order by t.taxkey|;
618 $sth = $dbh->prepare($query);
619 $sth->execute || $form->dberror($query);
621 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
622 push @{ $form->{TAX} }, $ref;
627 $query = "SELECT closedto, revtrans FROM defaults";
628 ($form->{closedto}, $form->{revtrans}) = $dbh->selectrow_array($query);
631 " (SELECT transdate FROM gl WHERE id = " .
632 " (SELECT MAX(id) FROM gl) LIMIT 1), " .
634 ($form->{transdate}) = $dbh->selectrow_array($query);
636 # get tax description
637 $query = qq| SELECT * FROM tax t order by t.taxkey|;
638 $sth = $dbh->prepare($query);
639 $sth->execute || $form->dberror($query);
641 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
642 push @{ $form->{TAX} }, $ref;
647 my $transdate = "current_date";
648 if ($form->{transdate}) {
649 $transdate = qq|'$form->{transdate}'|;
651 # get chart of accounts
652 $query = qq|SELECT c.accno, c.description, c.link, tk.taxkey_id, tk.tax_id
654 LEFT JOIN taxkeys tk ON (tk.id = (SELECT id from taxkeys where taxkeys.chart_id =c.id AND startdate<=$transdate ORDER BY startdate desc LIMIT 1))
656 $sth = $dbh->prepare($query);
657 $sth->execute || $form->dberror($query);
659 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
660 push @{ $form->{chart} }, $ref;
665 $main::lxdebug->leave_sub();