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 $sortorder transdate,acoid, 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};
490 $ref->{transdate}{$k} = $ref->{transdate};
493 $ref->{credit}{$l} = $ref->{amount} * 1;
494 $ref->{credit_accno}{$l} = $ref->{accno};
495 $ref->{credit_taxkey}{$l} = $ref->{taxkey};
496 $ref->{transdate}{$l} = $ref->{transdate};
502 } else { # following lines of a booking, line increasing
505 $trans_old =$trans_id2;
506 $trans_id2 = $ref2->{id};
509 (int($balance * 100000) + int(100000 * $ref2->{amount})) / 100000;
512 if ($ref2->{chart_id} > 0) { # all tax accounts, following lines
513 if ($ref2->{amount} < 0) {
514 if ($ref2->{link} =~ /AR_tax/) {
515 if ($ref->{credit_tax_accno}{$j} ne "") {
518 $ref->{credit_tax}{$j} = $ref2->{amount};
519 $ref->{credit_tax_accno}{$j} = $ref2->{accno};
521 if ($ref2->{link} =~ /AP_tax/) {
522 if ($ref->{debit_tax_accno}{$i} ne "") {
525 $ref->{debit_tax}{$i} = $ref2->{amount} * -1;
526 $ref->{debit_tax_accno}{$i} = $ref2->{accno};
529 if ($ref2->{link} =~ /AR_tax/) {
530 if ($ref->{credit_tax_accno}{$j} ne "") {
533 $ref->{credit_tax}{$j} = $ref2->{amount};
534 $ref->{credit_tax_accno}{$j} = $ref2->{accno};
536 if ($ref2->{link} =~ /AP_tax/) {
537 if ($ref->{debit_tax_accno}{$i} ne "") {
540 $ref->{debit_tax}{$i} = $ref2->{amount} * -1;
541 $ref->{debit_tax_accno}{$i} = $ref2->{accno};
544 } else { # all other accounts, following lines
545 if ($ref2->{amount} < 0) {
546 if ($ref->{debit_accno}{$k} ne "") {
549 $ref->{debit}{$k} = $ref2->{amount} * - 1;
550 $ref->{debit_accno}{$k} = $ref2->{accno};
551 $ref->{debit_taxkey}{$k} = $ref2->{taxkey};
552 $ref->{transdate}{$k} = $ref2->{transdate};
554 if ($ref->{credit_accno}{$l} ne "") {
557 $ref->{credit}{$l} = $ref2->{amount};
558 $ref->{credit_accno}{$l} = $ref2->{accno};
559 $ref->{credit_taxkey}{$l} = $ref2->{taxkey};
560 $ref->{transdate}{$l} = $ref2->{transdate};
565 push @{ $form->{GL} }, $ref;
568 if ($form->{accno}) {
570 qq|SELECT c.description FROM chart c WHERE c.accno = '$form->{accno}'|;
571 $sth = $dbh->prepare($query);
572 $sth->execute || $form->dberror($query);
574 ($form->{account_description}) = $sth->fetchrow_array;
577 if ($form->{gifi_accno}) {
579 qq|SELECT g.description FROM gifi g WHERE g.accno = '$form->{gifi_accno}'|;
580 $sth = $dbh->prepare($query);
581 $sth->execute || $form->dberror($query);
583 ($form->{gifi_account_description}) = $sth->fetchrow_array;
586 $main::lxdebug->leave_sub();
593 my ($self, $myconfig, $form) = @_;
594 $main::lxdebug->enter_sub();
596 my ($query, $sth, $ref);
598 # connect to database
599 my $dbh = $form->dbconnect($myconfig);
602 $query = "SELECT closedto, revtrans
604 $sth = $dbh->prepare($query);
605 $sth->execute || $form->dberror($query);
607 ($form->{closedto}, $form->{revtrans}) = $sth->fetchrow_array;
610 $query = "SELECT g.reference, g.description, g.notes, g.transdate,
611 d.description AS department, e.name as employee, g.taxincluded, g.gldate
613 LEFT JOIN department d ON (d.id = g.department_id)
614 LEFT JOIN employee e ON (e.id = g.employee_id)
615 WHERE g.id = $form->{id}";
616 $sth = $dbh->prepare($query);
617 $sth->execute || $form->dberror($query);
618 $ref = $sth->fetchrow_hashref(NAME_lc);
619 map { $form->{$_} = $ref->{$_} } keys %$ref;
622 # retrieve individual rows
623 $query = qq|SELECT c.accno, t.taxkey AS accnotaxkey, a.amount, a.memo,
624 a.transdate, a.cleared, a.project_id, p.projectnumber,(SELECT p.projectnumber FROM project p
625 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, (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
627 JOIN chart c ON (c.id = a.chart_id)
628 LEFT JOIN project p ON (p.id = a.project_id)
629 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))
630 WHERE a.trans_id = $form->{id}
631 AND a.fx_transaction = '0'
632 ORDER BY a.oid,a.transdate|;
634 $sth = $dbh->prepare($query);
635 $sth->execute || $form->dberror($query);
638 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
639 push @{ $form->{GL} }, $ref;
642 # get tax description
643 $query = qq| SELECT * FROM tax t order by t.taxkey|;
644 $sth = $dbh->prepare($query);
645 $sth->execute || $form->dberror($query);
647 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
648 push @{ $form->{TAX} }, $ref;
653 $query = "SELECT closedto, revtrans FROM defaults";
654 ($form->{closedto}, $form->{revtrans}) = $dbh->selectrow_array($query);
657 " (SELECT transdate FROM gl WHERE id = " .
658 " (SELECT MAX(id) FROM gl) LIMIT 1), " .
660 ($form->{transdate}) = $dbh->selectrow_array($query);
662 # get tax description
663 $query = qq| SELECT * FROM tax t order by t.taxkey|;
664 $sth = $dbh->prepare($query);
665 $sth->execute || $form->dberror($query);
667 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
668 push @{ $form->{TAX} }, $ref;
673 my $transdate = "current_date";
674 if ($form->{transdate}) {
675 $transdate = qq|'$form->{transdate}'|;
677 # get chart of accounts
678 $query = qq|SELECT c.accno, c.description, c.link, tk.taxkey_id, tk.tax_id
680 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))
682 $sth = $dbh->prepare($query);
683 $sth->execute || $form->dberror($query);
685 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
686 push @{ $form->{chart} }, $ref;
691 $main::lxdebug->leave_sub();