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->{gifi_accno}) {
288 $glwhere .= " AND c.gifi_accno = '$form->{gifi_accno}'";
289 $arwhere .= " AND c.gifi_accno = '$form->{gifi_accno}'";
290 $apwhere .= " AND c.gifi_accno = '$form->{gifi_accno}'";
292 if ($form->{category} ne 'X') {
294 " 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}'))";
296 " 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}'))";
298 " 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}'))";
300 if ($form->{project_id}) {
301 $glwhere .= " AND g.id IN (SELECT DISTINCT trans_id FROM acc_trans WHERE project_id = " . conv_i($form->{project_id}, 'NULL') . ")";
303 " AND ((a.globalproject_id = " . conv_i($form->{project_id}, 'NULL') . ") OR " .
304 " (a.id IN (SELECT DISTINCT trans_id FROM acc_trans WHERE project_id = " . conv_i($form->{project_id}, 'NULL') . ")))";
306 " AND ((a.globalproject_id = " . conv_i($form->{project_id}, 'NULL') . ") OR " .
307 " (a.id IN (SELECT DISTINCT trans_id FROM acc_trans WHERE project_id = " . conv_i($form->{project_id}, 'NULL') . ")))";
310 my ($project_columns, %project_join);
311 if ($form->{"l_projectnumbers"}) {
312 $project_columns = ", ac.project_id, pr.projectnumber";
313 $project_join = "LEFT JOIN project pr ON (ac.project_id = pr.id)";
316 if ($form->{accno}) {
318 # get category for account
319 $query = qq|SELECT c.category
321 WHERE c.accno = '$form->{accno}'|;
322 $sth = $dbh->prepare($query);
324 $sth->execute || $form->dberror($query);
325 ($form->{ml}) = $sth->fetchrow_array;
328 if ($form->{datefrom}) {
329 $query = qq|SELECT SUM(ac.amount)
330 FROM acc_trans ac, chart c
331 WHERE ac.chart_id = c.id
332 AND c.accno = '$form->{accno}'
333 AND ac.transdate < date '$form->{datefrom}'
335 $sth = $dbh->prepare($query);
336 $sth->execute || $form->dberror($query);
338 ($form->{balance}) = $sth->fetchrow_array;
343 if ($form->{gifi_accno}) {
345 # get category for account
346 $query = qq|SELECT c.category
348 WHERE c.gifi_accno = '$form->{gifi_accno}'|;
349 $sth = $dbh->prepare($query);
351 $sth->execute || $form->dberror($query);
352 ($form->{ml}) = $sth->fetchrow_array;
355 if ($form->{datefrom}) {
356 $query = qq|SELECT SUM(ac.amount)
357 FROM acc_trans ac, chart c
358 WHERE ac.chart_id = c.id
359 AND c.gifi_accno = '$form->{gifi_accno}'
360 AND ac.transdate < date '$form->{datefrom}'
362 $sth = $dbh->prepare($query);
363 $sth->execute || $form->dberror($query);
365 ($form->{balance}) = $sth->fetchrow_array;
370 my $false = ($myconfig->{dbdriver} eq 'Pg') ? FALSE: q|'0'|;
372 my $sortorder = join ', ',
373 $form->sort_columns(qw(transdate reference source description accno));
374 my %ordinal = (transdate => 6,
378 map { $sortorder =~ s/$_/$ordinal{$_}/ } keys %ordinal;
381 $sortorder = $form->{sort} . ",";
387 qq|SELECT ac.oid AS acoid, g.id, 'gl' AS type, $false AS invoice, g.reference, ac.taxkey, c.link,
388 g.description, ac.transdate, ac.source, ac.trans_id,
389 ac.amount, c.accno, c.gifi_accno, g.notes, t.chart_id, ac.oid
391 FROM gl g, acc_trans ac $project_join, chart c LEFT JOIN tax t ON
394 AND ac.chart_id = c.id
395 AND g.id = ac.trans_id
397 SELECT ac.oid AS acoid, a.id, 'ar' AS type, a.invoice, a.invnumber, ac.taxkey, c.link,
398 ct.name, ac.transdate, ac.source, ac.trans_id,
399 ac.amount, c.accno, c.gifi_accno, a.notes, t.chart_id, ac.oid
401 FROM ar a, acc_trans ac $project_join, customer ct, chart c LEFT JOIN tax t ON
404 AND ac.chart_id = c.id
405 AND a.customer_id = ct.id
406 AND a.id = ac.trans_id
408 SELECT ac.oid AS acoid, a.id, 'ap' AS type, a.invoice, a.invnumber, ac.taxkey, c.link,
409 ct.name, ac.transdate, ac.source, ac.trans_id,
410 ac.amount, c.accno, c.gifi_accno, a.notes, t.chart_id, ac.oid
412 FROM ap a, acc_trans ac $project_join, vendor ct, chart c LEFT JOIN tax t ON
415 AND ac.chart_id = c.id
416 AND a.vendor_id = ct.id
417 AND a.id = ac.trans_id
418 ORDER BY $sortorder transdate, trans_id, acoid, taxkey DESC|;
420 # Show all $query in Debuglevel LXDebug::QUERY
421 $callingdetails = (caller (0))[3];
422 $main::lxdebug->message(LXDebug::QUERY, "$callingdetails \$query=\n $query");
424 my $sth = $dbh->prepare($query);
425 $sth->execute || $form->dberror($query);
429 while (my $ref0 = $sth->fetchrow_hashref(NAME_lc)) {
431 $trans_id = $ref0->{id};
433 if ($trans_id != $trans_id2) { # first line of a booking
436 push @{ $form->{GL} }, $ref;
441 $trans_id2 = $ref->{id};
444 if ($ref->{type} eq "gl") {
445 $ref->{module} = "gl";
449 if ($ref->{type} eq "ap") {
450 if ($ref->{invoice}) {
451 $ref->{module} = "ir";
453 $ref->{module} = "ap";
458 if ($ref->{type} eq "ar") {
459 if ($ref->{invoice}) {
460 $ref->{module} = "is";
462 $ref->{module} = "ar";
466 $ref->{"projectnumbers"} = {};
467 $ref->{"projectnumbers"}->{$ref->{"projectnumber"}} = 1 if ($ref->{"projectnumber"});
469 $balance = $ref->{amount};
471 # Linenumbers of General Ledger
472 $k = 0; # Debit # AP # Soll
473 $l = 0; # Credit # AR # Haben
474 $i = 0; # Debit Tax # AP_tax # VSt
475 $j = 0; # Credit Tax # AR_tax # USt
478 if ($ref->{chart_id} > 0) { # all tax accounts first line, no line increasing
479 if ($ref->{amount} < 0) {
480 if ($ref->{link} =~ /AR_tax/) {
481 $ref->{credit_tax}{$j} = $ref->{amount};
482 $ref->{credit_tax_accno}{$j} = $ref->{accno};
484 if ($ref->{link} =~ /AP_tax/) {
485 $ref->{debit_tax}{$i} = $ref->{amount} * -1;
486 $ref->{debit_tax_accno}{$i} = $ref->{accno};
489 if ($ref->{link} =~ /AR_tax/) {
490 $ref->{credit_tax}{$j} = $ref->{amount};
491 $ref->{credit_tax_accno}{$j} = $ref->{accno};
493 if ($ref->{link} =~ /AP_tax/) {
494 $ref->{debit_tax}{$i} = $ref->{amount} * -1;
495 $ref->{debit_tax_accno}{$i} = $ref->{accno};
498 } else { #all other accounts first line
499 if ($ref->{amount} < 0) {
500 $ref->{debit}{$k} = $ref->{amount} * -1;
501 $ref->{debit_accno}{$k} = $ref->{accno};
502 $ref->{debit_taxkey}{$k} = $ref->{taxkey};
503 $ref->{ac_transdate}{$k} = $ref->{transdate};
506 $ref->{credit}{$l} = $ref->{amount} * 1;
507 $ref->{credit_accno}{$l} = $ref->{accno};
508 $ref->{credit_taxkey}{$l} = $ref->{taxkey};
509 $ref->{ac_transdate}{$l} = $ref->{transdate};
515 } else { # following lines of a booking, line increasing
518 $trans_old =$trans_id2;
519 $trans_id2 = $ref2->{id};
522 (int($balance * 100000) + int(100000 * $ref2->{amount})) / 100000;
524 $ref->{"projectnumbers"}->{$ref2->{"projectnumber"}} = 1 if ($ref2->{"projectnumber"});
526 if ($ref2->{chart_id} > 0) { # all tax accounts, following lines
527 if ($ref2->{amount} < 0) {
528 if ($ref2->{link} =~ /AR_tax/) {
529 if ($ref->{credit_tax_accno}{$j} ne "") {
532 $ref->{credit_tax}{$j} = $ref2->{amount};
533 $ref->{credit_tax_accno}{$j} = $ref2->{accno};
535 if ($ref2->{link} =~ /AP_tax/) {
536 if ($ref->{debit_tax_accno}{$i} ne "") {
539 $ref->{debit_tax}{$i} = $ref2->{amount} * -1;
540 $ref->{debit_tax_accno}{$i} = $ref2->{accno};
543 if ($ref2->{link} =~ /AR_tax/) {
544 if ($ref->{credit_tax_accno}{$j} ne "") {
547 $ref->{credit_tax}{$j} = $ref2->{amount};
548 $ref->{credit_tax_accno}{$j} = $ref2->{accno};
550 if ($ref2->{link} =~ /AP_tax/) {
551 if ($ref->{debit_tax_accno}{$i} ne "") {
554 $ref->{debit_tax}{$i} = $ref2->{amount} * -1;
555 $ref->{debit_tax_accno}{$i} = $ref2->{accno};
558 } else { # all other accounts, following lines
559 if ($ref2->{amount} < 0) {
560 if ($ref->{debit_accno}{$k} ne "") {
563 $ref->{debit}{$k} = $ref2->{amount} * - 1;
564 $ref->{debit_accno}{$k} = $ref2->{accno};
565 $ref->{debit_taxkey}{$k} = $ref2->{taxkey};
566 $ref->{ac_transdate}{$k} = $ref2->{transdate};
568 if ($ref->{credit_accno}{$l} ne "") {
571 $ref->{credit}{$l} = $ref2->{amount};
572 $ref->{credit_accno}{$l} = $ref2->{accno};
573 $ref->{credit_taxkey}{$l} = $ref2->{taxkey};
574 $ref->{ac_transdate}{$l} = $ref2->{transdate};
579 push @{ $form->{GL} }, $ref;
582 if ($form->{accno}) {
584 qq|SELECT c.description FROM chart c WHERE c.accno = '$form->{accno}'|;
585 $sth = $dbh->prepare($query);
586 $sth->execute || $form->dberror($query);
588 ($form->{account_description}) = $sth->fetchrow_array;
591 if ($form->{gifi_accno}) {
593 qq|SELECT g.description FROM gifi g WHERE g.accno = '$form->{gifi_accno}'|;
594 $sth = $dbh->prepare($query);
595 $sth->execute || $form->dberror($query);
597 ($form->{gifi_account_description}) = $sth->fetchrow_array;
600 $main::lxdebug->leave_sub();
607 my ($self, $myconfig, $form) = @_;
608 $main::lxdebug->enter_sub();
610 my ($query, $sth, $ref);
612 # connect to database
613 my $dbh = $form->dbconnect($myconfig);
616 $query = "SELECT closedto, revtrans
618 $sth = $dbh->prepare($query);
619 $sth->execute || $form->dberror($query);
621 ($form->{closedto}, $form->{revtrans}) = $sth->fetchrow_array;
624 $query = "SELECT g.reference, g.description, g.notes, g.transdate,
625 d.description AS department, e.name as employee, g.taxincluded, g.gldate
627 LEFT JOIN department d ON (d.id = g.department_id)
628 LEFT JOIN employee e ON (e.id = g.employee_id)
629 WHERE g.id = $form->{id}";
630 $sth = $dbh->prepare($query);
631 $sth->execute || $form->dberror($query);
632 $ref = $sth->fetchrow_hashref(NAME_lc);
633 map { $form->{$_} = $ref->{$_} } keys %$ref;
636 # retrieve individual rows
637 $query = qq|SELECT c.accno, t.taxkey AS accnotaxkey, a.amount, a.memo,
638 a.transdate, a.cleared, a.project_id, p.projectnumber,
639 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
641 JOIN chart c ON (c.id = a.chart_id)
642 LEFT JOIN project p ON (p.id = a.project_id)
643 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))
644 WHERE a.trans_id = $form->{id}
645 AND a.fx_transaction = '0'
646 ORDER BY a.oid,a.transdate|;
648 $sth = $dbh->prepare($query);
649 $sth->execute || $form->dberror($query);
652 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
653 push @{ $form->{GL} }, $ref;
656 # get tax description
657 $query = qq| SELECT * FROM tax t order by t.taxkey|;
658 $sth = $dbh->prepare($query);
659 $sth->execute || $form->dberror($query);
661 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
662 push @{ $form->{TAX} }, $ref;
667 $query = "SELECT closedto, revtrans FROM defaults";
668 ($form->{closedto}, $form->{revtrans}) = $dbh->selectrow_array($query);
671 " (SELECT transdate FROM gl WHERE id = " .
672 " (SELECT MAX(id) FROM gl) LIMIT 1), " .
674 ($form->{transdate}) = $dbh->selectrow_array($query);
676 # get tax description
677 $query = qq| SELECT * FROM tax t order by t.taxkey|;
678 $sth = $dbh->prepare($query);
679 $sth->execute || $form->dberror($query);
681 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
682 push @{ $form->{TAX} }, $ref;
687 my $transdate = "current_date";
688 if ($form->{transdate}) {
689 $transdate = qq|'$form->{transdate}'|;
691 # get chart of accounts
692 $query = qq|SELECT c.accno, c.description, c.link, tk.taxkey_id, tk.tax_id
694 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))
696 $sth = $dbh->prepare($query);
697 $sth->execute || $form->dberror($query);
699 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
700 push @{ $form->{chart} }, $ref;
705 $main::lxdebug->leave_sub();