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 # commit and redirect
215 my $rc = $dbh->commit;
217 $main::lxdebug->leave_sub();
223 sub all_transactions {
224 my ($self, $myconfig, $form) = @_;
225 $main::lxdebug->enter_sub();
227 # connect to database
228 my $dbh = $form->dbconnect($myconfig);
229 my ($query, $sth, $source, $null);
231 my ($glwhere, $arwhere, $apwhere) = ("1 = 1", "1 = 1", "1 = 1");
233 if ($form->{reference}) {
234 $source = $form->like(lc $form->{reference});
235 $glwhere .= " AND lower(g.reference) LIKE '$source'";
236 $arwhere .= " AND lower(a.invnumber) LIKE '$source'";
237 $apwhere .= " AND lower(a.invnumber) LIKE '$source'";
239 if ($form->{department}) {
240 ($null, $source) = split /--/, $form->{department};
241 $glwhere .= " AND g.department_id = $source";
242 $arwhere .= " AND a.department_id = $source";
243 $apwhere .= " AND a.department_id = $source";
246 if ($form->{source}) {
247 $source = $form->like(lc $form->{source});
248 $glwhere .= " AND lower(ac.source) LIKE '$source'";
249 $arwhere .= " AND lower(ac.source) LIKE '$source'";
250 $apwhere .= " AND lower(ac.source) LIKE '$source'";
252 if ($form->{datefrom}) {
253 $glwhere .= " AND ac.transdate >= '$form->{datefrom}'";
254 $arwhere .= " AND ac.transdate >= '$form->{datefrom}'";
255 $apwhere .= " AND ac.transdate >= '$form->{datefrom}'";
257 if ($form->{dateto}) {
258 $glwhere .= " AND ac.transdate <= '$form->{dateto}'";
259 $arwhere .= " AND ac.transdate <= '$form->{dateto}'";
260 $apwhere .= " AND ac.transdate <= '$form->{dateto}'";
262 if ($form->{description}) {
263 my $description = $form->like(lc $form->{description});
264 $glwhere .= " AND lower(g.description) LIKE '$description'";
265 $arwhere .= " AND lower(ct.name) LIKE '$description'";
266 $apwhere .= " AND lower(ct.name) LIKE '$description'";
268 if ($form->{notes}) {
269 my $notes = $form->like(lc $form->{notes});
270 $glwhere .= " AND lower(g.notes) LIKE '$notes'";
271 $arwhere .= " AND lower(a.notes) LIKE '$notes'";
272 $apwhere .= " AND lower(a.notes) LIKE '$notes'";
274 if ($form->{accno}) {
275 $glwhere .= " AND c.accno = '$form->{accno}'";
276 $arwhere .= " AND c.accno = '$form->{accno}'";
277 $apwhere .= " AND c.accno = '$form->{accno}'";
279 if ($form->{category} ne 'X') {
281 " 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}'))";
283 " 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}'))";
285 " 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}'))";
287 if ($form->{project_id}) {
288 $glwhere .= " AND g.id IN (SELECT DISTINCT trans_id FROM acc_trans WHERE project_id = " . conv_i($form->{project_id}, 'NULL') . ")";
290 " AND ((a.globalproject_id = " . conv_i($form->{project_id}, 'NULL') . ") OR " .
291 " (a.id IN (SELECT DISTINCT trans_id FROM acc_trans WHERE project_id = " . conv_i($form->{project_id}, 'NULL') . ")))";
293 " AND ((a.globalproject_id = " . conv_i($form->{project_id}, 'NULL') . ") OR " .
294 " (a.id IN (SELECT DISTINCT trans_id FROM acc_trans WHERE project_id = " . conv_i($form->{project_id}, 'NULL') . ")))";
297 my ($project_columns, %project_join);
298 if ($form->{"l_projectnumbers"}) {
299 $project_columns = ", ac.project_id, pr.projectnumber";
300 $project_join = "LEFT JOIN project pr ON (ac.project_id = pr.id)";
303 if ($form->{accno}) {
305 # get category for account
306 $query = qq|SELECT c.category
308 WHERE c.accno = '$form->{accno}'|;
309 $sth = $dbh->prepare($query);
311 $sth->execute || $form->dberror($query);
312 ($form->{ml}) = $sth->fetchrow_array;
315 if ($form->{datefrom}) {
316 $query = qq|SELECT SUM(ac.amount)
317 FROM acc_trans ac, chart c
318 WHERE ac.chart_id = c.id
319 AND c.accno = '$form->{accno}'
320 AND ac.transdate < date '$form->{datefrom}'
322 $sth = $dbh->prepare($query);
323 $sth->execute || $form->dberror($query);
325 ($form->{balance}) = $sth->fetchrow_array;
330 my $false = ($myconfig->{dbdriver} eq 'Pg') ? FALSE: q|'0'|;
332 my $sortorder = join ', ',
333 $form->sort_columns(qw(transdate reference source description accno));
334 my %ordinal = (transdate => 6,
338 map { $sortorder =~ s/$_/$ordinal{$_}/ } keys %ordinal;
341 $sortorder = $form->{sort} . ",";
347 qq|SELECT ac.oid AS acoid, g.id, 'gl' AS type, $false AS invoice, g.reference, ac.taxkey, c.link,
348 g.description, ac.transdate, ac.source, ac.trans_id,
349 ac.amount, c.accno, g.notes, t.chart_id, ac.oid
351 FROM gl g, acc_trans ac $project_join, chart c LEFT JOIN tax t ON
354 AND ac.chart_id = c.id
355 AND g.id = ac.trans_id
357 SELECT ac.oid AS acoid, a.id, 'ar' AS type, a.invoice, a.invnumber, ac.taxkey, c.link,
358 ct.name, ac.transdate, ac.source, ac.trans_id,
359 ac.amount, c.accno, a.notes, t.chart_id, ac.oid
361 FROM ar a, acc_trans ac $project_join, customer ct, chart c LEFT JOIN tax t ON
364 AND ac.chart_id = c.id
365 AND a.customer_id = ct.id
366 AND a.id = ac.trans_id
368 SELECT ac.oid AS acoid, a.id, 'ap' AS type, a.invoice, a.invnumber, ac.taxkey, c.link,
369 ct.name, ac.transdate, ac.source, ac.trans_id,
370 ac.amount, c.accno, a.notes, t.chart_id, ac.oid
372 FROM ap a, acc_trans ac $project_join, vendor ct, chart c LEFT JOIN tax t ON
375 AND ac.chart_id = c.id
376 AND a.vendor_id = ct.id
377 AND a.id = ac.trans_id
378 ORDER BY $sortorder transdate, trans_id, acoid, taxkey DESC|;
380 # Show all $query in Debuglevel LXDebug::QUERY
381 $callingdetails = (caller (0))[3];
382 $main::lxdebug->message(LXDebug::QUERY, "$callingdetails \$query=\n $query");
384 my $sth = $dbh->prepare($query);
385 $sth->execute || $form->dberror($query);
389 while (my $ref0 = $sth->fetchrow_hashref(NAME_lc)) {
391 $trans_id = $ref0->{id};
393 if ($trans_id != $trans_id2) { # first line of a booking
396 push @{ $form->{GL} }, $ref;
401 $trans_id2 = $ref->{id};
404 if ($ref->{type} eq "gl") {
405 $ref->{module} = "gl";
409 if ($ref->{type} eq "ap") {
410 if ($ref->{invoice}) {
411 $ref->{module} = "ir";
413 $ref->{module} = "ap";
418 if ($ref->{type} eq "ar") {
419 if ($ref->{invoice}) {
420 $ref->{module} = "is";
422 $ref->{module} = "ar";
426 $ref->{"projectnumbers"} = {};
427 $ref->{"projectnumbers"}->{$ref->{"projectnumber"}} = 1 if ($ref->{"projectnumber"});
429 $balance = $ref->{amount};
431 # Linenumbers of General Ledger
432 $k = 0; # Debit # AP # Soll
433 $l = 0; # Credit # AR # Haben
434 $i = 0; # Debit Tax # AP_tax # VSt
435 $j = 0; # Credit Tax # AR_tax # USt
438 if ($ref->{chart_id} > 0) { # all tax accounts first line, no line increasing
439 if ($ref->{amount} < 0) {
440 if ($ref->{link} =~ /AR_tax/) {
441 $ref->{credit_tax}{$j} = $ref->{amount};
442 $ref->{credit_tax_accno}{$j} = $ref->{accno};
444 if ($ref->{link} =~ /AP_tax/) {
445 $ref->{debit_tax}{$i} = $ref->{amount} * -1;
446 $ref->{debit_tax_accno}{$i} = $ref->{accno};
449 if ($ref->{link} =~ /AR_tax/) {
450 $ref->{credit_tax}{$j} = $ref->{amount};
451 $ref->{credit_tax_accno}{$j} = $ref->{accno};
453 if ($ref->{link} =~ /AP_tax/) {
454 $ref->{debit_tax}{$i} = $ref->{amount} * -1;
455 $ref->{debit_tax_accno}{$i} = $ref->{accno};
458 } else { #all other accounts first line
459 if ($ref->{amount} < 0) {
460 $ref->{debit}{$k} = $ref->{amount} * -1;
461 $ref->{debit_accno}{$k} = $ref->{accno};
462 $ref->{debit_taxkey}{$k} = $ref->{taxkey};
463 $ref->{ac_transdate}{$k} = $ref->{transdate};
466 $ref->{credit}{$l} = $ref->{amount} * 1;
467 $ref->{credit_accno}{$l} = $ref->{accno};
468 $ref->{credit_taxkey}{$l} = $ref->{taxkey};
469 $ref->{ac_transdate}{$l} = $ref->{transdate};
475 } else { # following lines of a booking, line increasing
478 $trans_old =$trans_id2;
479 $trans_id2 = $ref2->{id};
482 (int($balance * 100000) + int(100000 * $ref2->{amount})) / 100000;
484 $ref->{"projectnumbers"}->{$ref2->{"projectnumber"}} = 1 if ($ref2->{"projectnumber"});
486 if ($ref2->{chart_id} > 0) { # all tax accounts, following lines
487 if ($ref2->{amount} < 0) {
488 if ($ref2->{link} =~ /AR_tax/) {
489 if ($ref->{credit_tax_accno}{$j} ne "") {
492 $ref->{credit_tax}{$j} = $ref2->{amount};
493 $ref->{credit_tax_accno}{$j} = $ref2->{accno};
495 if ($ref2->{link} =~ /AP_tax/) {
496 if ($ref->{debit_tax_accno}{$i} ne "") {
499 $ref->{debit_tax}{$i} = $ref2->{amount} * -1;
500 $ref->{debit_tax_accno}{$i} = $ref2->{accno};
503 if ($ref2->{link} =~ /AR_tax/) {
504 if ($ref->{credit_tax_accno}{$j} ne "") {
507 $ref->{credit_tax}{$j} = $ref2->{amount};
508 $ref->{credit_tax_accno}{$j} = $ref2->{accno};
510 if ($ref2->{link} =~ /AP_tax/) {
511 if ($ref->{debit_tax_accno}{$i} ne "") {
514 $ref->{debit_tax}{$i} = $ref2->{amount} * -1;
515 $ref->{debit_tax_accno}{$i} = $ref2->{accno};
518 } else { # all other accounts, following lines
519 if ($ref2->{amount} < 0) {
520 if ($ref->{debit_accno}{$k} ne "") {
523 $ref->{debit}{$k} = $ref2->{amount} * - 1;
524 $ref->{debit_accno}{$k} = $ref2->{accno};
525 $ref->{debit_taxkey}{$k} = $ref2->{taxkey};
526 $ref->{ac_transdate}{$k} = $ref2->{transdate};
528 if ($ref->{credit_accno}{$l} ne "") {
531 $ref->{credit}{$l} = $ref2->{amount};
532 $ref->{credit_accno}{$l} = $ref2->{accno};
533 $ref->{credit_taxkey}{$l} = $ref2->{taxkey};
534 $ref->{ac_transdate}{$l} = $ref2->{transdate};
539 push @{ $form->{GL} }, $ref;
542 if ($form->{accno}) {
544 qq|SELECT c.description FROM chart c WHERE c.accno = '$form->{accno}'|;
545 $sth = $dbh->prepare($query);
546 $sth->execute || $form->dberror($query);
548 ($form->{account_description}) = $sth->fetchrow_array;
552 $main::lxdebug->leave_sub();
559 my ($self, $myconfig, $form) = @_;
560 $main::lxdebug->enter_sub();
562 my ($query, $sth, $ref);
564 # connect to database
565 my $dbh = $form->dbconnect($myconfig);
568 $query = "SELECT closedto, revtrans
570 $sth = $dbh->prepare($query);
571 $sth->execute || $form->dberror($query);
573 ($form->{closedto}, $form->{revtrans}) = $sth->fetchrow_array;
576 $query = "SELECT g.reference, g.description, g.notes, g.transdate,
577 d.description AS department, e.name as employee, g.taxincluded, g.gldate
579 LEFT JOIN department d ON (d.id = g.department_id)
580 LEFT JOIN employee e ON (e.id = g.employee_id)
581 WHERE g.id = $form->{id}";
582 $sth = $dbh->prepare($query);
583 $sth->execute || $form->dberror($query);
584 $ref = $sth->fetchrow_hashref(NAME_lc);
585 map { $form->{$_} = $ref->{$_} } keys %$ref;
588 # retrieve individual rows
589 $query = qq|SELECT c.accno, t.taxkey AS accnotaxkey, a.amount, a.memo,
590 a.transdate, a.cleared, a.project_id, p.projectnumber,
591 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
593 JOIN chart c ON (c.id = a.chart_id)
594 LEFT JOIN project p ON (p.id = a.project_id)
595 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))
596 WHERE a.trans_id = $form->{id}
597 AND a.fx_transaction = '0'
598 ORDER BY a.oid,a.transdate|;
600 $sth = $dbh->prepare($query);
601 $sth->execute || $form->dberror($query);
604 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
605 push @{ $form->{GL} }, $ref;
608 # get tax description
609 $query = qq| SELECT * FROM tax t order by t.taxkey|;
610 $sth = $dbh->prepare($query);
611 $sth->execute || $form->dberror($query);
613 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
614 push @{ $form->{TAX} }, $ref;
619 $query = "SELECT closedto, revtrans FROM defaults";
620 ($form->{closedto}, $form->{revtrans}) = $dbh->selectrow_array($query);
623 " (SELECT transdate FROM gl WHERE id = " .
624 " (SELECT MAX(id) FROM gl) LIMIT 1), " .
626 ($form->{transdate}) = $dbh->selectrow_array($query);
628 # get tax description
629 $query = qq| SELECT * FROM tax t order by t.taxkey|;
630 $sth = $dbh->prepare($query);
631 $sth->execute || $form->dberror($query);
633 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
634 push @{ $form->{TAX} }, $ref;
639 my $transdate = "current_date";
640 if ($form->{transdate}) {
641 $transdate = qq|'$form->{transdate}'|;
643 # get chart of accounts
644 $query = qq|SELECT c.accno, c.description, c.link, tk.taxkey_id, tk.tax_id
646 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))
648 $sth = $dbh->prepare($query);
649 $sth->execute || $form->dberror($query);
651 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
652 push @{ $form->{chart} }, $ref;
657 $main::lxdebug->leave_sub();