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 #======================================================================
43 sub delete_transaction {
44 my ($self, $myconfig, $form) = @_;
45 $main::lxdebug->enter_sub();
48 my $dbh = $form->dbconnect_noauto($myconfig);
50 my $query = qq|DELETE FROM gl WHERE id = $form->{id}|;
51 $dbh->do($query) || $form->dberror($query);
53 $query = qq|DELETE FROM acc_trans WHERE trans_id = $form->{id}|;
54 $dbh->do($query) || $form->dberror($query);
57 my $rc = $dbh->commit;
59 $main::lxdebug->leave_sub();
65 sub post_transaction {
66 my ($self, $myconfig, $form) = @_;
67 $main::lxdebug->enter_sub();
69 my ($debit, $credit) = (0, 0);
74 # check if debit and credit balances
76 if ($form->{storno}) {
78 $credit = $credit * -1;
80 $form->{reference} = "Storno-" . $form->{reference};
81 $form->{description} = "Storno-" . $form->{description};
84 # connect to database, turn off AutoCommit
85 my $dbh = $form->dbconnect_noauto($myconfig);
87 # post the transaction
88 # make up a unique handle and store in reference field
89 # then retrieve the record based on the unique handle to get the id
90 # replace the reference field with the actual variable
91 # add records to acc_trans
93 # if there is a $form->{id} replace the old transaction
94 # delete all acc_trans entries and add the new ones
97 map { $form->{$_} =~ s/\'/\'\'/g } qw(reference description notes);
99 if (!$form->{taxincluded}) {
100 $form->{taxincluded} = 0;
107 # delete individual transactions
108 $query = qq|DELETE FROM acc_trans
109 WHERE trans_id = $form->{id}|;
110 $dbh->do($query) || $form->dberror($query);
114 $uid .= $form->{login};
116 $query = qq|INSERT INTO gl (reference, employee_id)
117 VALUES ('$uid', (SELECT e.id FROM employee e
118 WHERE e.login = '$form->{login}'))|;
119 $dbh->do($query) || $form->dberror($query);
121 $query = qq|SELECT g.id FROM gl g
122 WHERE g.reference = '$uid'|;
123 $sth = $dbh->prepare($query);
124 $sth->execute || $form->dberror($query);
126 ($form->{id}) = $sth->fetchrow_array;
131 my ($null, $department_id) = split /--/, $form->{department};
134 $query = qq|UPDATE gl SET
135 reference = '$form->{reference}',
136 description = '$form->{description}',
137 notes = '$form->{notes}',
138 transdate = '$form->{transdate}',
139 department_id = $department_id,
140 taxincluded = '$form->{taxincluded}'
141 WHERE id = $form->{id}|;
143 $dbh->do($query) || $form->dberror($query);
144 ($taxkey, $rate) = split(/--/, $form->{taxkey});
146 # insert acc_trans transactions
147 for $i (1 .. $form->{rowcount}) {
151 print(STDERR $form->{"taxchart_$i"}, "TAXCHART\n");
152 my ($accno) = split(/--/, $form->{"accno_$i"});
153 my ($taxkey, $rate) = split(/--/, $form->{"taxchart_$i"});
154 ($form->{"tax_id_$i"}, $NULL) = split /--/, $form->{"taxchart_$i"};
155 if ($form->{"tax_id_$i"} ne "") {
156 $query = qq|SELECT t.taxkey, t.rate
158 WHERE t.id=$form->{"tax_id_$i"}|;
160 $sth = $dbh->prepare($query);
161 $sth->execute || $form->dberror($query);
163 $sth->fetchrow_array;
168 my $debit = $form->{"debit_$i"};
169 my $credit = $form->{"credit_$i"};
170 my $tax = $form->{"tax_$i"};
177 $amount = $debit * -1;
182 # if there is an amount, add the record
185 ($form->{"project_id_$i"}) ? $form->{"project_id_$i"} : 'NULL';
186 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate,
187 source, memo, project_id, taxkey)
189 ($form->{id}, (SELECT c.id
191 WHERE c.accno = '$accno'),
192 $amount, '$form->{transdate}', |
193 . $dbh->quote($form->{"source_$i"}) . qq|, |
194 . $dbh->quote($form->{"memo_$i"}) . qq|,
195 $project_id, $taxkey)|;
197 $dbh->do($query) || $form->dberror($query);
206 ($form->{"project_id_$i"}) ? $form->{"project_id_$i"} : 'NULL';
207 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate,
208 source, memo, project_id, taxkey)
210 ($form->{id}, (SELECT t.chart_id
212 WHERE t.id = $form->{"tax_id_$i"}),
213 $amount, '$form->{transdate}', |
214 . $dbh->quote($form->{"source_$i"}) . qq|, |
215 . $dbh->quote($form->{"memo_$i"}) . qq|,
216 $project_id, $taxkey)|;
218 $dbh->do($query) || $form->dberror($query);
222 my %audittrail = (tablename => 'gl',
223 reference => $form->{reference},
224 formname => 'transaction',
228 # $form->audittrail($dbh, "", \%audittrail);
230 # commit and redirect
231 my $rc = $dbh->commit;
233 $main::lxdebug->leave_sub();
239 sub all_transactions {
240 my ($self, $myconfig, $form) = @_;
241 $main::lxdebug->enter_sub();
243 # connect to database
244 my $dbh = $form->dbconnect($myconfig);
245 my ($query, $sth, $source, $null);
247 my ($glwhere, $arwhere, $apwhere) = ("1 = 1", "1 = 1", "1 = 1");
249 if ($form->{reference}) {
250 $source = $form->like(lc $form->{reference});
251 $glwhere .= " AND lower(g.reference) LIKE '$source'";
252 $arwhere .= " AND lower(a.invnumber) LIKE '$source'";
253 $apwhere .= " AND lower(a.invnumber) LIKE '$source'";
255 if ($form->{department}) {
256 ($null, $source) = split /--/, $form->{department};
257 $glwhere .= " AND g.department_id = $source";
258 $arwhere .= " AND a.department_id = $source";
259 $apwhere .= " AND a.department_id = $source";
262 if ($form->{source}) {
263 $source = $form->like(lc $form->{source});
264 $glwhere .= " AND lower(ac.source) LIKE '$source'";
265 $arwhere .= " AND lower(ac.source) LIKE '$source'";
266 $apwhere .= " AND lower(ac.source) LIKE '$source'";
268 if ($form->{datefrom}) {
269 $glwhere .= " AND ac.transdate >= '$form->{datefrom}'";
270 $arwhere .= " AND ac.transdate >= '$form->{datefrom}'";
271 $apwhere .= " AND ac.transdate >= '$form->{datefrom}'";
273 if ($form->{dateto}) {
274 $glwhere .= " AND ac.transdate <= '$form->{dateto}'";
275 $arwhere .= " AND ac.transdate <= '$form->{dateto}'";
276 $apwhere .= " AND ac.transdate <= '$form->{dateto}'";
278 if ($form->{description}) {
279 my $description = $form->like(lc $form->{description});
280 $glwhere .= " AND lower(g.description) LIKE '$description'";
281 $arwhere .= " AND lower(ct.name) LIKE '$description'";
282 $apwhere .= " AND lower(ct.name) LIKE '$description'";
284 if ($form->{notes}) {
285 my $notes = $form->like(lc $form->{notes});
286 $glwhere .= " AND lower(g.notes) LIKE '$notes'";
287 $arwhere .= " AND lower(a.notes) LIKE '$notes'";
288 $apwhere .= " AND lower(a.notes) LIKE '$notes'";
290 if ($form->{accno}) {
291 $glwhere .= " AND c.accno = '$form->{accno}'";
292 $arwhere .= " AND c.accno = '$form->{accno}'";
293 $apwhere .= " AND c.accno = '$form->{accno}'";
295 if ($form->{gifi_accno}) {
296 $glwhere .= " AND c.gifi_accno = '$form->{gifi_accno}'";
297 $arwhere .= " AND c.gifi_accno = '$form->{gifi_accno}'";
298 $apwhere .= " AND c.gifi_accno = '$form->{gifi_accno}'";
300 if ($form->{category} ne 'X') {
302 " 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}'))";
304 " 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}'))";
306 " 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}'))";
309 if ($form->{accno}) {
311 # get category for account
312 $query = qq|SELECT c.category
314 WHERE c.accno = '$form->{accno}'|;
315 $sth = $dbh->prepare($query);
317 $sth->execute || $form->dberror($query);
318 ($form->{ml}) = $sth->fetchrow_array;
321 if ($form->{datefrom}) {
322 $query = qq|SELECT SUM(ac.amount)
323 FROM acc_trans ac, chart c
324 WHERE ac.chart_id = c.id
325 AND c.accno = '$form->{accno}'
326 AND ac.transdate < date '$form->{datefrom}'
328 $sth = $dbh->prepare($query);
329 $sth->execute || $form->dberror($query);
331 ($form->{balance}) = $sth->fetchrow_array;
336 if ($form->{gifi_accno}) {
338 # get category for account
339 $query = qq|SELECT c.category
341 WHERE c.gifi_accno = '$form->{gifi_accno}'|;
342 $sth = $dbh->prepare($query);
344 $sth->execute || $form->dberror($query);
345 ($form->{ml}) = $sth->fetchrow_array;
348 if ($form->{datefrom}) {
349 $query = qq|SELECT SUM(ac.amount)
350 FROM acc_trans ac, chart c
351 WHERE ac.chart_id = c.id
352 AND c.gifi_accno = '$form->{gifi_accno}'
353 AND ac.transdate < date '$form->{datefrom}'
355 $sth = $dbh->prepare($query);
356 $sth->execute || $form->dberror($query);
358 ($form->{balance}) = $sth->fetchrow_array;
363 my $false = ($myconfig->{dbdriver} eq 'Pg') ? FALSE: q|'0'|;
365 my $sortorder = join ', ',
366 $form->sort_columns(qw(transdate reference source description accno));
367 my %ordinal = (transdate => 6,
371 map { $sortorder =~ s/$_/$ordinal{$_}/ } keys %ordinal;
374 $sortorder = $form->{sort} . ",";
380 qq|SELECT ac.oid AS acoid, g.id, 'gl' AS type, $false AS invoice, g.reference, ac.taxkey, c.link,
381 g.description, ac.transdate, ac.source, ac.trans_id,
382 ac.amount, c.accno, c.gifi_accno, g.notes, t.chart_id, ac.oid
383 FROM gl g, acc_trans ac, chart c LEFT JOIN tax t ON
386 AND ac.chart_id = c.id
387 AND g.id = ac.trans_id
389 SELECT ac.oid AS acoid, a.id, 'ar' AS type, a.invoice, a.invnumber, ac.taxkey, c.link,
390 ct.name, ac.transdate, ac.source, ac.trans_id,
391 ac.amount, c.accno, c.gifi_accno, a.notes, t.chart_id, ac.oid
392 FROM ar a, acc_trans ac, customer ct, chart c LEFT JOIN tax t ON
395 AND ac.chart_id = c.id
396 AND a.customer_id = ct.id
397 AND a.id = ac.trans_id
399 SELECT ac.oid AS acoid, a.id, 'ap' AS type, a.invoice, a.invnumber, ac.taxkey, c.link,
400 ct.name, ac.transdate, ac.source, ac.trans_id,
401 ac.amount, c.accno, c.gifi_accno, a.notes, t.chart_id, ac.oid
402 FROM ap a, acc_trans ac, vendor ct, chart c LEFT JOIN tax t ON
405 AND ac.chart_id = c.id
406 AND a.vendor_id = ct.id
407 AND a.id = ac.trans_id
408 ORDER BY acoid, $sortorder transdate, trans_id, taxkey DESC|;
410 # Show all $query in Debuglevel LXDebug::QUERY
411 $callingdetails = (caller (0))[3];
412 $main::lxdebug->message(LXDebug::QUERY, "$callingdetails \$query=\n $query");
414 my $sth = $dbh->prepare($query);
415 $sth->execute || $form->dberror($query);
419 while (my $ref0 = $sth->fetchrow_hashref(NAME_lc)) {
421 $trans_id = $ref0->{id};
423 if ($trans_id != $trans_id2) { # first line of a booking
426 push @{ $form->{GL} }, $ref;
431 $trans_id2 = $ref->{id};
434 if ($ref->{type} eq "gl") {
435 $ref->{module} = "gl";
439 if ($ref->{type} eq "ap") {
440 if ($ref->{invoice}) {
441 $ref->{module} = "ir";
443 $ref->{module} = "ap";
448 if ($ref->{type} eq "ar") {
449 if ($ref->{invoice}) {
450 $ref->{module} = "is";
452 $ref->{module} = "ar";
456 $balance = $ref->{amount};
458 # Linenumbers of General Ledger
459 $k = 0; # Debit # AP # Soll
460 $l = 0; # Credit # AR # Haben
461 $i = 0; # Debit Tax # AP_tax # VSt
462 $j = 0; # Credit Tax # AR_tax # USt
465 if ($ref->{chart_id} > 0) { # all tax accounts first line, no line increasing
466 if ($ref->{amount} < 0) {
467 if ($ref->{link} =~ /AR_tax/) {
468 $ref->{credit_tax}{$j} = $ref->{amount};
469 $ref->{credit_tax_accno}{$j} = $ref->{accno};
471 if ($ref->{link} =~ /AP_tax/) {
472 $ref->{debit_tax}{$i} = $ref->{amount} * -1;
473 $ref->{debit_tax_accno}{$i} = $ref->{accno};
476 if ($ref->{link} =~ /AR_tax/) {
477 $ref->{credit_tax}{$j} = $ref->{amount};
478 $ref->{credit_tax_accno}{$j} = $ref->{accno};
480 if ($ref->{link} =~ /AP_tax/) {
481 $ref->{debit_tax}{$i} = $ref->{amount} * -1;
482 $ref->{debit_tax_accno}{$i} = $ref->{accno};
485 } else { #all other accounts first line
486 if ($ref->{amount} < 0) {
487 $ref->{debit}{$k} = $ref->{amount} * -1;
488 $ref->{debit_accno}{$k} = $ref->{accno};
489 $ref->{debit_taxkey}{$k} = $ref->{taxkey};
492 $ref->{credit}{$l} = $ref->{amount} * 1;
493 $ref->{credit_accno}{$l} = $ref->{accno};
494 $ref->{credit_taxkey}{$l} = $ref->{taxkey};
500 } else { # following lines of a booking, line increasing
503 $trans_old =$trans_id2;
504 $trans_id2 = $ref2->{id};
507 (int($balance * 100000) + int(100000 * $ref2->{amount})) / 100000;
510 if ($ref2->{chart_id} > 0) { # all tax accounts, following lines
511 if ($ref2->{amount} < 0) {
512 if ($ref2->{link} =~ /AR_tax/) {
513 if ($ref->{credit_tax_accno}{$j} ne "") {
516 $ref->{credit_tax}{$j} = $ref2->{amount};
517 $ref->{credit_tax_accno}{$j} = $ref2->{accno};
519 if ($ref2->{link} =~ /AP_tax/) {
520 if ($ref->{debit_tax_accno}{$i} ne "") {
523 $ref->{debit_tax}{$i} = $ref2->{amount} * -1;
524 $ref->{debit_tax_accno}{$i} = $ref2->{accno};
527 if ($ref2->{link} =~ /AR_tax/) {
528 if ($ref->{credit_tax_accno}{$j} ne "") {
531 $ref->{credit_tax}{$j} = $ref2->{amount};
532 $ref->{credit_tax_accno}{$j} = $ref2->{accno};
534 if ($ref2->{link} =~ /AP_tax/) {
535 if ($ref->{debit_tax_accno}{$i} ne "") {
538 $ref->{debit_tax}{$i} = $ref2->{amount} * -1;
539 $ref->{debit_tax_accno}{$i} = $ref2->{accno};
542 } else { # all other accounts, following lines
543 if ($ref2->{amount} < 0) {
544 if ($ref->{debit_accno}{$k} ne "") {
547 $ref->{debit}{$k} = $ref2->{amount} * - 1;
548 $ref->{debit_accno}{$k} = $ref2->{accno};
549 $ref->{debit_taxkey}{$k} = $ref2->{taxkey};
551 if ($ref->{credit_accno}{$l} ne "") {
554 $ref->{credit}{$l} = $ref2->{amount};
555 $ref->{credit_accno}{$l} = $ref2->{accno};
556 $ref->{credit_taxkey}{$l} = $ref2->{taxkey};
561 push @{ $form->{GL} }, $ref;
564 if ($form->{accno}) {
566 qq|SELECT c.description FROM chart c WHERE c.accno = '$form->{accno}'|;
567 $sth = $dbh->prepare($query);
568 $sth->execute || $form->dberror($query);
570 ($form->{account_description}) = $sth->fetchrow_array;
573 if ($form->{gifi_accno}) {
575 qq|SELECT g.description FROM gifi g WHERE g.accno = '$form->{gifi_accno}'|;
576 $sth = $dbh->prepare($query);
577 $sth->execute || $form->dberror($query);
579 ($form->{gifi_account_description}) = $sth->fetchrow_array;
582 $main::lxdebug->leave_sub();
589 my ($self, $myconfig, $form) = @_;
590 $main::lxdebug->enter_sub();
592 my ($query, $sth, $ref);
594 # connect to database
595 my $dbh = $form->dbconnect($myconfig);
598 $query = "SELECT closedto, revtrans
600 $sth = $dbh->prepare($query);
601 $sth->execute || $form->dberror($query);
603 ($form->{closedto}, $form->{revtrans}) = $sth->fetchrow_array;
606 $query = "SELECT g.reference, g.description, g.notes, g.transdate,
607 d.description AS department, e.name as employee, g.taxincluded, g.gldate
609 LEFT JOIN department d ON (d.id = g.department_id)
610 LEFT JOIN employee e ON (e.id = g.employee_id)
611 WHERE g.id = $form->{id}";
612 $sth = $dbh->prepare($query);
613 $sth->execute || $form->dberror($query);
614 $ref = $sth->fetchrow_hashref(NAME_lc);
615 map { $form->{$_} = $ref->{$_} } keys %$ref;
618 # retrieve individual rows
619 $query = "SELECT c.accno, c.taxkey_id AS accnotaxkey, a.amount, project_id,
620 (SELECT p.projectnumber FROM project p
621 WHERE a.project_id = p.id) AS projectnumber, a.taxkey, (SELECT c1.accno FROM chart c1, tax t WHERE t.taxkey=a.taxkey AND c1.id=t.chart_id) AS taxaccno, (SELECT t1.rate FROM tax t1 WHERE t1.taxkey=a.taxkey) AS taxrate
622 FROM acc_trans a, chart c
623 WHERE a.chart_id = c.id
624 AND a.trans_id = $form->{id}
627 $query = qq|SELECT c.accno, t.taxkey AS accnotaxkey, a.amount, a.memo,
628 a.transdate, a.cleared, a.project_id, p.projectnumber,(SELECT p.projectnumber FROM project p
629 WHERE a.project_id = p.id) AS projectnumber, 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
631 JOIN chart c ON (c.id = a.chart_id)
632 LEFT JOIN project p ON (p.id = a.project_id)
633 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='%tax%')) AND startdate <=a.transdate ORDER BY startdate DESC LIMIT 1))
634 WHERE a.trans_id = $form->{id}
635 AND a.fx_transaction = '0'
636 ORDER BY a.oid,a.transdate|;
639 $sth = $dbh->prepare($query);
640 $sth->execute || $form->dberror($query);
642 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
643 push @{ $form->{GL} }, $ref;
646 # get tax description
647 $query = qq| SELECT * FROM tax t order by t.taxkey|;
648 $sth = $dbh->prepare($query);
649 $sth->execute || $form->dberror($query);
651 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
652 push @{ $form->{TAX} }, $ref;
657 $query = "SELECT current_date AS transdate, closedto, revtrans
659 $sth = $dbh->prepare($query);
660 $sth->execute || $form->dberror($query);
662 ($form->{transdate}, $form->{closedto}, $form->{revtrans}) =
663 $sth->fetchrow_array;
665 # get tax description
666 $query = qq| SELECT * FROM tax t order by t.taxkey|;
667 $sth = $dbh->prepare($query);
668 $sth->execute || $form->dberror($query);
670 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
671 push @{ $form->{TAX} }, $ref;
676 my $transdate = "current_date";
677 if ($form->{transdate}) {
678 $transdate = qq|'$form->{transdate}'|;
680 # get chart of accounts
681 $query = qq|SELECT c.accno, c.description, c.link, tk.taxkey_id, tk.tax_id
683 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))
685 $sth = $dbh->prepare($query);
686 $sth->execute || $form->dberror($query);
688 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
689 push @{ $form->{chart} }, $ref;
694 $main::lxdebug->leave_sub();