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}) {
150 my ($accno) = split(/--/, $form->{"accno_$i"});
151 my ($taxkey, $rate) = split(/--/, $form->{"taxchart_$i"});
153 my $debit = $form->{"debit_$i"};
154 my $credit = $form->{"credit_$i"};
155 my $tax = $form->{"tax_$i"};
162 $amount = $debit * -1;
167 # if there is an amount, add the record
170 ($form->{"project_id_$i"}) ? $form->{"project_id_$i"} : 'NULL';
171 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate,
172 source, memo, project_id, taxkey)
174 ($form->{id}, (SELECT c.id
176 WHERE c.accno = '$accno'),
177 $amount, '$form->{transdate}', |
178 . $dbh->quote($form->{"source_$i"}) . qq|, |
179 . $dbh->quote($form->{"memo_$i"}) . qq|,
180 $project_id, $taxkey)|;
182 $dbh->do($query) || $form->dberror($query);
191 ($form->{"project_id_$i"}) ? $form->{"project_id_$i"} : 'NULL';
192 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate,
193 source, memo, project_id, taxkey)
195 ($form->{id}, (SELECT t.chart_id
197 WHERE t.taxkey = $taxkey),
198 $amount, '$form->{transdate}', |
199 . $dbh->quote($form->{"source_$i"}) . qq|, |
200 . $dbh->quote($form->{"memo_$i"}) . qq|,
201 $project_id, $taxkey)|;
203 $dbh->do($query) || $form->dberror($query);
207 my %audittrail = (tablename => 'gl',
208 reference => $form->{reference},
209 formname => 'transaction',
213 # $form->audittrail($dbh, "", \%audittrail);
215 # commit and redirect
216 my $rc = $dbh->commit;
218 $main::lxdebug->leave_sub();
224 sub all_transactions {
225 my ($self, $myconfig, $form) = @_;
226 $main::lxdebug->enter_sub();
228 # connect to database
229 my $dbh = $form->dbconnect($myconfig);
230 my ($query, $sth, $source, $null);
232 my ($glwhere, $arwhere, $apwhere) = ("1 = 1", "1 = 1", "1 = 1");
234 if ($form->{reference}) {
235 $source = $form->like(lc $form->{reference});
236 $glwhere .= " AND lower(g.reference) LIKE '$source'";
237 $arwhere .= " AND lower(a.invnumber) LIKE '$source'";
238 $apwhere .= " AND lower(a.invnumber) LIKE '$source'";
240 if ($form->{department}) {
241 ($null, $source) = split /--/, $form->{department};
242 $glwhere .= " AND g.department_id = $source";
243 $arwhere .= " AND a.department_id = $source";
244 $apwhere .= " AND a.department_id = $source";
247 if ($form->{source}) {
248 $source = $form->like(lc $form->{source});
249 $glwhere .= " AND lower(ac.source) LIKE '$source'";
250 $arwhere .= " AND lower(ac.source) LIKE '$source'";
251 $apwhere .= " AND lower(ac.source) LIKE '$source'";
253 if ($form->{datefrom}) {
254 $glwhere .= " AND ac.transdate >= '$form->{datefrom}'";
255 $arwhere .= " AND ac.transdate >= '$form->{datefrom}'";
256 $apwhere .= " AND ac.transdate >= '$form->{datefrom}'";
258 if ($form->{dateto}) {
259 $glwhere .= " AND ac.transdate <= '$form->{dateto}'";
260 $arwhere .= " AND ac.transdate <= '$form->{dateto}'";
261 $apwhere .= " AND ac.transdate <= '$form->{dateto}'";
263 if ($form->{description}) {
264 my $description = $form->like(lc $form->{description});
265 $glwhere .= " AND lower(g.description) LIKE '$description'";
266 $arwhere .= " AND lower(ct.name) LIKE '$description'";
267 $apwhere .= " AND lower(ct.name) LIKE '$description'";
269 if ($form->{notes}) {
270 my $notes = $form->like(lc $form->{notes});
271 $glwhere .= " AND lower(g.notes) LIKE '$notes'";
272 $arwhere .= " AND lower(a.notes) LIKE '$notes'";
273 $apwhere .= " AND lower(a.notes) LIKE '$notes'";
275 if ($form->{accno}) {
276 $glwhere .= " AND c.accno = '$form->{accno}'";
277 $arwhere .= " AND c.accno = '$form->{accno}'";
278 $apwhere .= " AND c.accno = '$form->{accno}'";
280 if ($form->{gifi_accno}) {
281 $glwhere .= " AND c.gifi_accno = '$form->{gifi_accno}'";
282 $arwhere .= " AND c.gifi_accno = '$form->{gifi_accno}'";
283 $apwhere .= " AND c.gifi_accno = '$form->{gifi_accno}'";
285 if ($form->{category} ne 'X') {
287 " 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}'))";
289 " 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}'))";
291 " 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}'))";
294 if ($form->{accno}) {
296 # get category for account
297 $query = qq|SELECT c.category
299 WHERE c.accno = '$form->{accno}'|;
300 $sth = $dbh->prepare($query);
302 $sth->execute || $form->dberror($query);
303 ($form->{ml}) = $sth->fetchrow_array;
306 if ($form->{datefrom}) {
307 $query = qq|SELECT SUM(ac.amount)
308 FROM acc_trans ac, chart c
309 WHERE ac.chart_id = c.id
310 AND c.accno = '$form->{accno}'
311 AND ac.transdate < date '$form->{datefrom}'
313 $sth = $dbh->prepare($query);
314 $sth->execute || $form->dberror($query);
316 ($form->{balance}) = $sth->fetchrow_array;
321 if ($form->{gifi_accno}) {
323 # get category for account
324 $query = qq|SELECT c.category
326 WHERE c.gifi_accno = '$form->{gifi_accno}'|;
327 $sth = $dbh->prepare($query);
329 $sth->execute || $form->dberror($query);
330 ($form->{ml}) = $sth->fetchrow_array;
333 if ($form->{datefrom}) {
334 $query = qq|SELECT SUM(ac.amount)
335 FROM acc_trans ac, chart c
336 WHERE ac.chart_id = c.id
337 AND c.gifi_accno = '$form->{gifi_accno}'
338 AND ac.transdate < date '$form->{datefrom}'
340 $sth = $dbh->prepare($query);
341 $sth->execute || $form->dberror($query);
343 ($form->{balance}) = $sth->fetchrow_array;
348 my $false = ($myconfig->{dbdriver} eq 'Pg') ? FALSE: q|'0'|;
350 my $sortorder = join ', ',
351 $form->sort_columns(qw(transdate reference source description accno));
352 my %ordinal = (transdate => 6,
356 map { $sortorder =~ s/$_/$ordinal{$_}/ } keys %ordinal;
359 $sortorder = $form->{sort} . ",";
365 qq|SELECT g.id, 'gl' AS type, $false AS invoice, g.reference, ac.taxkey, c.link,
366 g.description, ac.transdate, ac.source, ac.trans_id,
367 ac.amount, c.accno, c.gifi_accno, g.notes, t.chart_id, ac.oid
368 FROM gl g, acc_trans ac, chart c LEFT JOIN tax t ON
371 AND ac.chart_id = c.id
372 AND g.id = ac.trans_id
374 SELECT a.id, 'ar' AS type, a.invoice, a.invnumber, ac.taxkey, c.link,
375 ct.name, ac.transdate, ac.source, ac.trans_id,
376 ac.amount, c.accno, c.gifi_accno, a.notes, t.chart_id, ac.oid
377 FROM ar a, acc_trans ac, customer ct, chart c LEFT JOIN tax t ON
380 AND ac.chart_id = c.id
381 AND a.customer_id = ct.id
382 AND a.id = ac.trans_id
384 SELECT a.id, 'ap' AS type, a.invoice, a.invnumber, ac.taxkey, c.link,
385 ct.name, ac.transdate, ac.source, ac.trans_id,
386 ac.amount, c.accno, c.gifi_accno, a.notes, t.chart_id, ac.oid
387 FROM ap a, acc_trans ac, vendor ct, chart c LEFT JOIN tax t ON
390 AND ac.chart_id = c.id
391 AND a.vendor_id = ct.id
392 AND a.id = ac.trans_id
393 ORDER BY $sortorder transdate, trans_id, taxkey DESC, oid|;
395 # Show all $query in Debuglevel LXDebug::QUERY
396 $callingdetails = (caller (0))[3];
397 $main::lxdebug->message(LXDebug::QUERY, "$callingdetails \$query=\n $query");
399 my $sth = $dbh->prepare($query);
400 $sth->execute || $form->dberror($query);
404 while (my $ref0 = $sth->fetchrow_hashref(NAME_lc)) {
406 $trans_id = $ref0->{id};
408 if ($trans_id != $trans_id2) { # first line of a booking
411 push @{ $form->{GL} }, $ref;
416 $trans_id2 = $ref->{id};
419 if ($ref->{type} eq "gl") {
420 $ref->{module} = "gl";
424 if ($ref->{type} eq "ap") {
425 if ($ref->{invoice}) {
426 $ref->{module} = "ir";
428 $ref->{module} = "ap";
433 if ($ref->{type} eq "ar") {
434 if ($ref->{invoice}) {
435 $ref->{module} = "is";
437 $ref->{module} = "ar";
441 $balance = $ref->{amount};
443 # Linenumbers of General Ledger
444 $k = 0; # Debit # AP # Soll
445 $l = 0; # Credit # AR # Haben
446 $i = 0; # Debit Tax # AP_tax # VSt
447 $j = 0; # Credit Tax # AR_tax # USt
450 if ($ref->{chart_id} > 0) { # all tax accounts first line, no line increasing
451 if ($ref->{amount} < 0) {
452 if ($ref->{link} =~ /AR_tax/) {
453 $ref->{credit_tax}{$j} = $ref->{amount};
454 $ref->{credit_tax_accno}{$j} = $ref->{accno};
456 if ($ref->{link} =~ /AP_tax/) {
457 $ref->{debit_tax}{$i} = $ref->{amount} * -1;
458 $ref->{debit_tax_accno}{$i} = $ref->{accno};
461 if ($ref->{link} =~ /AR_tax/) {
462 $ref->{credit_tax}{$j} = $ref->{amount};
463 $ref->{credit_tax_accno}{$j} = $ref->{accno};
465 if ($ref->{link} =~ /AP_tax/) {
466 $ref->{debit_tax}{$i} = $ref->{amount} * -1;
467 $ref->{debit_tax_accno}{$i} = $ref->{accno};
470 } else { #all other accounts first line
471 if ($ref->{amount} < 0) {
472 $ref->{debit}{$k} = $ref->{amount} * -1;
473 $ref->{debit_accno}{$k} = $ref->{accno};
474 $ref->{debit_taxkey}{$k} = $ref->{taxkey};
477 $ref->{credit}{$l} = $ref->{amount} * 1;
478 $ref->{credit_accno}{$l} = $ref->{accno};
479 $ref->{credit_taxkey}{$l} = $ref->{taxkey};
485 } else { # following lines of a booking, line increasing
488 $trans_old =$trans_id2;
489 $trans_id2 = $ref2->{id};
492 (int($balance * 100000) + int(100000 * $ref2->{amount})) / 100000;
495 if ($ref2->{chart_id} > 0) { # all tax accounts, following lines
496 if ($ref2->{amount} < 0) {
497 if ($ref2->{link} =~ /AR_tax/) {
498 if ($ref->{credit_tax_accno}{$j} ne "") {
501 $ref->{credit_tax}{$j} = $ref2->{amount};
502 $ref->{credit_tax_accno}{$j} = $ref2->{accno};
504 if ($ref2->{link} =~ /AP_tax/) {
505 if ($ref->{debit_tax_accno}{$i} ne "") {
508 $ref->{debit_tax}{$i} = $ref2->{amount} * -1;
509 $ref->{debit_tax_accno}{$i} = $ref2->{accno};
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 } else { # all other accounts, following lines
528 if ($ref2->{amount} < 0) {
529 if ($ref->{debit_accno}{$k} ne "") {
532 $ref->{debit}{$k} = $ref2->{amount} * - 1;
533 $ref->{debit_accno}{$k} = $ref2->{accno};
534 $ref->{debit_taxkey}{$k} = $ref2->{taxkey};
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};
546 push @{ $form->{GL} }, $ref;
549 if ($form->{accno}) {
551 qq|SELECT c.description FROM chart c WHERE c.accno = '$form->{accno}'|;
552 $sth = $dbh->prepare($query);
553 $sth->execute || $form->dberror($query);
555 ($form->{account_description}) = $sth->fetchrow_array;
558 if ($form->{gifi_accno}) {
560 qq|SELECT g.description FROM gifi g WHERE g.accno = '$form->{gifi_accno}'|;
561 $sth = $dbh->prepare($query);
562 $sth->execute || $form->dberror($query);
564 ($form->{gifi_account_description}) = $sth->fetchrow_array;
567 $main::lxdebug->leave_sub();
574 my ($self, $myconfig, $form) = @_;
575 $main::lxdebug->enter_sub();
577 my ($query, $sth, $ref);
579 # connect to database
580 my $dbh = $form->dbconnect($myconfig);
583 $query = "SELECT closedto, revtrans
585 $sth = $dbh->prepare($query);
586 $sth->execute || $form->dberror($query);
588 ($form->{closedto}, $form->{revtrans}) = $sth->fetchrow_array;
591 $query = "SELECT g.reference, g.description, g.notes, g.transdate,
592 d.description AS department, e.name as employee, g.taxincluded, g.gldate
594 LEFT JOIN department d ON (d.id = g.department_id)
595 LEFT JOIN employee e ON (e.id = g.employee_id)
596 WHERE g.id = $form->{id}";
597 $sth = $dbh->prepare($query);
598 $sth->execute || $form->dberror($query);
599 $ref = $sth->fetchrow_hashref(NAME_lc);
600 map { $form->{$_} = $ref->{$_} } keys %$ref;
603 # retrieve individual rows
604 $query = "SELECT c.accno, c.taxkey_id AS accnotaxkey, a.amount, project_id,
605 (SELECT p.projectnumber FROM project p
606 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
607 FROM acc_trans a, chart c
608 WHERE a.chart_id = c.id
609 AND a.trans_id = $form->{id}
611 $sth = $dbh->prepare($query);
612 $sth->execute || $form->dberror($query);
614 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
615 push @{ $form->{GL} }, $ref;
618 # get tax description
619 $query = qq| SELECT * FROM tax t order by t.taxkey|;
620 $sth = $dbh->prepare($query);
621 $sth->execute || $form->dberror($query);
623 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
624 push @{ $form->{TAX} }, $ref;
629 $query = "SELECT current_date AS transdate, closedto, revtrans
631 $sth = $dbh->prepare($query);
632 $sth->execute || $form->dberror($query);
634 ($form->{transdate}, $form->{closedto}, $form->{revtrans}) =
635 $sth->fetchrow_array;
637 # get tax description
638 $query = qq| SELECT * FROM tax t order by t.taxkey|;
639 $sth = $dbh->prepare($query);
640 $sth->execute || $form->dberror($query);
642 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
643 push @{ $form->{TAX} }, $ref;
649 # get chart of accounts
650 $query = qq|SELECT c.accno, c.description, c.taxkey_id
652 WHERE c.charttype = 'A'
654 $sth = $dbh->prepare($query);
655 $sth->execute || $form->dberror($query);
657 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
658 push @{ $form->{chart} }, $ref;
663 $main::lxdebug->leave_sub();