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 #======================================================================
42 use SL::DATEV qw(:CONSTANTS);
47 sub delete_transaction {
48 my ($self, $myconfig, $form) = @_;
49 $main::lxdebug->enter_sub();
52 my $dbh = $form->dbconnect_noauto($myconfig);
54 my @values = (conv_i($form->{id}));
55 do_query($form, $dbh, qq|DELETE FROM acc_trans WHERE trans_id = ?|, @values);
56 do_query($form, $dbh, qq|DELETE FROM gl WHERE id = ?|, @values);
59 my $rc = $dbh->commit;
61 $main::lxdebug->leave_sub();
67 sub post_transaction {
68 my ($self, $myconfig, $form) = @_;
69 $main::lxdebug->enter_sub();
71 my ($debit, $credit) = (0, 0);
76 # connect to database, turn off AutoCommit
77 my $dbh = $form->dbconnect_noauto($myconfig);
79 # post the transaction
80 # make up a unique handle and store in reference field
81 # then retrieve the record based on the unique handle to get the id
82 # replace the reference field with the actual variable
83 # add records to acc_trans
85 # if there is a $form->{id} replace the old transaction
86 # delete all acc_trans entries and add the new ones
88 if (!$form->{taxincluded}) {
89 $form->{taxincluded} = 0;
92 my ($query, $sth, @values, $taxkey, $rate, $posted);
96 # delete individual transactions
97 $query = qq|DELETE FROM acc_trans WHERE trans_id = ?|;
98 @values = (conv_i($form->{id}));
99 do_query($form, $dbh, $query, @values);
102 $query = qq|SELECT nextval('glid')|;
103 ($form->{id}) = selectrow_query($form, $dbh, $query);
106 qq|INSERT INTO gl (id, employee_id) | .
107 qq|VALUES (?, (SELECT id FROM employee WHERE login = ?))|;
108 @values = ($form->{id}, $form->{login});
109 do_query($form, $dbh, $query, @values);
112 my ($null, $department_id) = split(/--/, $form->{department});
115 $form->{ob_transaction} *= 1;
116 $form->{cb_transaction} *= 1;
120 reference = ?, description = ?, notes = ?,
121 transdate = ?, department_id = ?, taxincluded = ?,
122 storno = ?, storno_id = ?, ob_transaction = ?, cb_transaction = ?
125 @values = ($form->{reference}, $form->{description}, $form->{notes},
126 conv_date($form->{transdate}), $department_id, $form->{taxincluded} ? 't' : 'f',
127 $form->{storno} ? 't' : 'f', conv_i($form->{storno_id}), $form->{ob_transaction} ? 't' : 'f', $form->{cb_transaction} ? 't' : 'f',
128 conv_i($form->{id}));
129 do_query($form, $dbh, $query, @values);
131 # insert acc_trans transactions
132 for $i (1 .. $form->{rowcount}) {
134 my ($accno) = split(/--/, $form->{"accno_$i"});
135 ($form->{"tax_id_$i"}) = split(/--/, $form->{"taxchart_$i"});
136 if ($form->{"tax_id_$i"} ne "") {
137 $query = qq|SELECT taxkey, rate FROM tax WHERE id = ?|;
138 ($taxkey, $rate) = selectrow_query($form, $dbh, $query, conv_i($form->{"tax_id_$i"}));
142 my $debit = $form->{"debit_$i"};
143 my $credit = $form->{"credit_$i"};
144 my $tax = $form->{"tax_$i"};
151 $amount = $debit * -1;
156 $project_id = conv_i($form->{"project_id_$i"});
158 # if there is an amount, add the record
161 qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate,
162 source, memo, project_id, taxkey, ob_transaction, cb_transaction)
163 VALUES (?, (SELECT id FROM chart WHERE accno = ?),
164 ?, ?, ?, ?, ?, ?, ?, ?)|;
165 @values = (conv_i($form->{id}), $accno, $amount, conv_date($form->{transdate}),
166 $form->{"source_$i"}, $form->{"memo_$i"}, $project_id, $taxkey, $form->{ob_transaction} ? 't' : 'f', $form->{cb_transaction} ? 't' : 'f');
167 do_query($form, $dbh, $query, @values);
173 qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate,
174 source, memo, project_id, taxkey)
175 VALUES (?, (SELECT chart_id FROM tax WHERE id = ?),
177 @values = (conv_i($form->{id}), conv_i($form->{"tax_id_$i"}),
178 $tax, conv_date($form->{transdate}), $form->{"source_$i"},
179 $form->{"memo_$i"}, $project_id, $taxkey);
180 do_query($form, $dbh, $query, @values);
184 if ($form->{storno} && $form->{storno_id}) {
185 do_query($form, $dbh, qq|UPDATE gl SET storno = 't' WHERE id = ?|, conv_i($form->{storno_id}));
188 # safety check datev export
189 if ($::lx_office_conf{datev_check}{check_on_gl_transaction}) {
190 my $transdate = $::form->{transdate} ? DateTime->from_lxoffice($::form->{transdate}) : undef;
191 $transdate ||= DateTime->today;
193 my $datev = SL::DATEV->new(
194 exporttype => DATEV_ET_BUCHUNGEN,
195 format => DATEV_FORMAT_KNE,
203 if ($datev->errors) {
205 die join "\n", $::locale->text('DATEV check returned errors:'), $datev->errors;
209 # commit and redirect
210 my $rc = $dbh->commit;
212 $main::lxdebug->leave_sub();
217 sub all_transactions {
218 my ($self, $myconfig, $form) = @_;
219 $main::lxdebug->enter_sub();
221 # connect to database
222 my $dbh = $form->dbconnect($myconfig);
223 my ($query, $sth, $source, $null, $space);
225 my ($glwhere, $arwhere, $apwhere) = ("1 = 1", "1 = 1", "1 = 1");
226 my (@glvalues, @arvalues, @apvalues);
228 if ($form->{reference}) {
229 $glwhere .= qq| AND g.reference ILIKE ?|;
230 $arwhere .= qq| AND a.invnumber ILIKE ?|;
231 $apwhere .= qq| AND a.invnumber ILIKE ?|;
232 push(@glvalues, '%' . $form->{reference} . '%');
233 push(@arvalues, '%' . $form->{reference} . '%');
234 push(@apvalues, '%' . $form->{reference} . '%');
237 if ($form->{department}) {
238 my ($null, $department) = split /--/, $form->{department};
239 $glwhere .= qq| AND g.department_id = ?|;
240 $arwhere .= qq| AND a.department_id = ?|;
241 $apwhere .= qq| AND a.department_id = ?|;
242 push(@glvalues, $department);
243 push(@arvalues, $department);
244 push(@apvalues, $department);
247 if ($form->{source}) {
248 $glwhere .= " AND ac.trans_id IN (SELECT trans_id from acc_trans WHERE source ILIKE ?)";
249 $arwhere .= " AND ac.trans_id IN (SELECT trans_id from acc_trans WHERE source ILIKE ?)";
250 $apwhere .= " AND ac.trans_id IN (SELECT trans_id from acc_trans WHERE source ILIKE ?)";
251 push(@glvalues, '%' . $form->{source} . '%');
252 push(@arvalues, '%' . $form->{source} . '%');
253 push(@apvalues, '%' . $form->{source} . '%');
256 # default Datumseinschränkung falls nicht oder falsch übergeben (sollte nie passieren)
257 $form->{datesort} = 'transdate' unless $form->{datesort} =~ /^(transdate|gldate)$/;
259 if ($form->{datefrom}) {
260 $glwhere .= " AND ac.$form->{datesort} >= ?";
261 $arwhere .= " AND ac.$form->{datesort} >= ?";
262 $apwhere .= " AND ac.$form->{datesort} >= ?";
263 push(@glvalues, $form->{datefrom});
264 push(@arvalues, $form->{datefrom});
265 push(@apvalues, $form->{datefrom});
268 if ($form->{dateto}) {
269 $glwhere .= " AND ac.$form->{datesort} <= ?";
270 $arwhere .= " AND ac.$form->{datesort} <= ?";
271 $apwhere .= " AND ac.$form->{datesort} <= ?";
272 push(@glvalues, $form->{dateto});
273 push(@arvalues, $form->{dateto});
274 push(@apvalues, $form->{dateto});
277 if ($form->{description}) {
278 $glwhere .= " AND g.description ILIKE ?";
279 $arwhere .= " AND ct.name ILIKE ?";
280 $apwhere .= " AND ct.name ILIKE ?";
281 push(@glvalues, '%' . $form->{description} . '%');
282 push(@arvalues, '%' . $form->{description} . '%');
283 push(@apvalues, '%' . $form->{description} . '%');
286 if ($form->{employee} =~ /--/) {
287 ($form->{employee_id},$form->{employee_name}) = split(/--/,$form->{employee});
288 #if ($form->{employee_id}) {
289 $glwhere .= " AND g.employee_id = ? ";
290 $arwhere .= " AND a.employee_id = ? ";
291 $apwhere .= " AND a.employee_id = ? ";
292 push(@glvalues, conv_i($form->{employee_id}));
293 push(@arvalues, conv_i($form->{employee_id}));
294 push(@apvalues, conv_i($form->{employee_id}));
297 if ($form->{notes}) {
298 $glwhere .= " AND g.notes ILIKE ?";
299 $arwhere .= " AND a.notes ILIKE ?";
300 $apwhere .= " AND a.notes ILIKE ?";
301 push(@glvalues, '%' . $form->{notes} . '%');
302 push(@arvalues, '%' . $form->{notes} . '%');
303 push(@apvalues, '%' . $form->{notes} . '%');
306 if ($form->{accno}) {
307 $glwhere .= " AND c.accno = '$form->{accno}'";
308 $arwhere .= " AND c.accno = '$form->{accno}'";
309 $apwhere .= " AND c.accno = '$form->{accno}'";
312 if ($form->{category} ne 'X') {
313 $glwhere .= qq| AND g.id in (SELECT trans_id FROM acc_trans ac2 WHERE ac2.chart_id IN (SELECT id FROM chart c2 WHERE c2.category = ?))|;
314 $arwhere .= qq| AND a.id in (SELECT trans_id FROM acc_trans ac2 WHERE ac2.chart_id IN (SELECT id FROM chart c2 WHERE c2.category = ?))|;
315 $apwhere .= qq| AND a.id in (SELECT trans_id FROM acc_trans ac2 WHERE ac2.chart_id IN (SELECT id FROM chart c2 WHERE c2.category = ?))|;
316 push(@glvalues, $form->{category});
317 push(@arvalues, $form->{category});
318 push(@apvalues, $form->{category});
321 if ($form->{project_id}) {
322 $glwhere .= qq| AND g.id IN (SELECT DISTINCT trans_id FROM acc_trans WHERE project_id = ?)|;
324 qq| AND ((a.globalproject_id = ?) OR
325 (a.id IN (SELECT DISTINCT trans_id FROM acc_trans WHERE project_id = ?)))|;
327 qq| AND ((a.globalproject_id = ?) OR
328 (a.id IN (SELECT DISTINCT trans_id FROM acc_trans WHERE project_id = ?)))|;
329 my $project_id = conv_i($form->{project_id});
330 push(@glvalues, $project_id);
331 push(@arvalues, $project_id, $project_id);
332 push(@apvalues, $project_id, $project_id);
335 my ($project_columns, $project_join);
336 if ($form->{"l_projectnumbers"}) {
337 $project_columns = qq|, ac.project_id, pr.projectnumber|;
338 $project_join = qq|LEFT JOIN project pr ON (ac.project_id = pr.id)|;
341 if ($form->{accno}) {
342 # get category for account
343 $query = qq|SELECT category FROM chart WHERE accno = ?|;
344 ($form->{ml}) = selectrow_query($form, $dbh, $query, $form->{accno});
346 if ($form->{datefrom}) {
348 qq|SELECT SUM(ac.amount)
350 LEFT JOIN chart c ON (ac.chart_id = c.id)
351 WHERE (c.accno = ?) AND (ac.$form->{datesort} < ?)|;
352 ($form->{balance}) = selectrow_query($form, $dbh, $query, $form->{accno}, conv_date($form->{datefrom}));
356 my $false = ($myconfig->{dbdriver} eq 'Pg') ? "FALSE" : q|'0'|;
360 'transdate' => [ qw(transdate id) ],
361 'gldate' => [ qw(gldate id) ],
362 'reference' => [ qw(lower_reference id) ],
363 'description' => [ qw(lower_description id) ],
364 'accno' => [ qw(accno transdate id) ],
366 my %lowered_columns = (
367 'reference' => { 'gl' => 'g.reference', 'arap' => 'a.invnumber', },
368 'source' => { 'gl' => 'ac.source', 'arap' => 'ac.source', },
369 'description' => { 'gl' => 'g.description', 'arap' => 'ct.name', },
372 # sortdir = sort direction (ascending or descending)
373 my $sortdir = !defined $form->{sortdir} ? 'ASC' : $form->{sortdir} ? 'ASC' : 'DESC';
374 my $sortkey = $sort_columns{$form->{sort}} ? $form->{sort} : $form->{datesort}; # default used to be transdate
375 my $sortorder = join ', ', map { "$_ $sortdir" } @{ $sort_columns{$sortkey} };
377 my %columns_for_sorting = ( 'gl' => '', 'arap' => '', );
378 foreach my $spec (@{ $sort_columns{$sortkey} }) {
379 next if ($spec !~ m/^lower_(.*)$/);
382 map { $columns_for_sorting{$_} .= sprintf(', lower(%s) AS lower_%s', $lowered_columns{$column}->{$_}, $column) } qw(gl arap);
387 ac.acc_trans_id, g.id, 'gl' AS type, $false AS invoice, g.reference, ac.taxkey, c.link,
388 g.description, ac.transdate, ac.gldate, ac.source, ac.trans_id,
389 ac.amount, c.accno, g.notes, t.chart_id,
390 CASE WHEN (COALESCE(e.name, '') = '') THEN e.login ELSE e.name END AS employee
392 $columns_for_sorting{gl}
394 LEFT JOIN employee e ON (g.employee_id = e.id),
395 acc_trans ac $project_join, chart c
396 LEFT JOIN tax t ON (t.chart_id = c.id)
398 AND (ac.chart_id = c.id)
399 AND (g.id = ac.trans_id)
403 SELECT ac.acc_trans_id, a.id, 'ar' AS type, a.invoice, a.invnumber, ac.taxkey, c.link,
404 ct.name, ac.transdate, ac.gldate, ac.source, ac.trans_id,
405 ac.amount, c.accno, a.notes, t.chart_id,
406 CASE WHEN (COALESCE(e.name, '') = '') THEN e.login ELSE e.name END AS employee
408 $columns_for_sorting{arap}
410 LEFT JOIN employee e ON (a.employee_id = e.id),
411 acc_trans ac $project_join, customer ct, chart c
412 LEFT JOIN tax t ON (t.chart_id=c.id)
414 AND (ac.chart_id = c.id)
415 AND (a.customer_id = ct.id)
416 AND (a.id = ac.trans_id)
420 SELECT ac.acc_trans_id, a.id, 'ap' AS type, a.invoice, a.invnumber, ac.taxkey, c.link,
421 ct.name, ac.transdate, ac.gldate, ac.source, ac.trans_id,
422 ac.amount, c.accno, a.notes, t.chart_id,
423 CASE WHEN (COALESCE(e.name, '') = '') THEN e.login ELSE e.name END AS employee
425 $columns_for_sorting{arap}
427 LEFT JOIN employee e ON (a.employee_id = e.id),
428 acc_trans ac $project_join, vendor ct, chart c
429 LEFT JOIN tax t ON (t.chart_id=c.id)
431 AND (ac.chart_id = c.id)
432 AND (a.vendor_id = ct.id)
433 AND (a.id = ac.trans_id)
435 ORDER BY $sortorder, acc_trans_id $sortdir|;
436 # ORDER BY gldate DESC, id DESC, acc_trans_id DESC
438 my @values = (@glvalues, @arvalues, @apvalues);
440 # Show all $query in Debuglevel LXDebug::QUERY
441 my $callingdetails = (caller (0))[3];
442 dump_query(LXDebug->QUERY(), "$callingdetails", $query, @values);
444 $sth = prepare_execute_query($form, $dbh, $query, @values);
449 my ($i, $j, $k, $l, $ref, $ref2);
452 while (my $ref0 = $sth->fetchrow_hashref("NAME_lc")) {
454 $trans_id = $ref0->{id};
456 my $source = $ref0->{source};
457 undef($ref0->{source});
459 if ($trans_id != $trans_id2) { # first line of a booking
462 push(@{ $form->{GL} }, $ref);
467 $trans_id2 = $ref->{id};
470 if ($ref->{type} eq "gl") {
471 $ref->{module} = "gl";
475 if ($ref->{type} eq "ap") {
476 if ($ref->{invoice}) {
477 $ref->{module} = "ir";
479 $ref->{module} = "ap";
484 if ($ref->{type} eq "ar") {
485 if ($ref->{invoice}) {
486 $ref->{module} = "is";
488 $ref->{module} = "ar";
492 $ref->{"projectnumbers"} = {};
493 $ref->{"projectnumbers"}->{$ref->{"projectnumber"}} = 1 if ($ref->{"projectnumber"});
495 $balance = $ref->{amount};
497 # Linenumbers of General Ledger
498 $k = 0; # Debit # AP # Soll
499 $l = 0; # Credit # AR # Haben
500 $i = 0; # Debit Tax # AP_tax # VSt
501 $j = 0; # Credit Tax # AR_tax # USt
503 if ($ref->{chart_id} > 0) { # all tax accounts first line, no line increasing
504 if ($ref->{amount} < 0) {
505 if ($ref->{link} =~ /AR_tax/) {
506 $ref->{credit_tax}{$j} = $ref->{amount};
507 $ref->{credit_tax_accno}{$j} = $ref->{accno};
509 if ($ref->{link} =~ /AP_tax/) {
510 $ref->{debit_tax}{$i} = $ref->{amount} * -1;
511 $ref->{debit_tax_accno}{$i} = $ref->{accno};
514 if ($ref->{link} =~ /AR_tax/) {
515 $ref->{credit_tax}{$j} = $ref->{amount};
516 $ref->{credit_tax_accno}{$j} = $ref->{accno};
518 if ($ref->{link} =~ /AP_tax/) {
519 $ref->{debit_tax}{$i} = $ref->{amount} * -1;
520 $ref->{debit_tax_accno}{$i} = $ref->{accno};
523 } else { #all other accounts first line
525 if ($ref->{amount} < 0) {
526 $ref->{debit}{$k} = $ref->{amount} * -1;
527 $ref->{debit_accno}{$k} = $ref->{accno};
528 $ref->{debit_taxkey}{$k} = $ref->{taxkey};
529 $ref->{ac_transdate}{$k} = $ref->{transdate};
530 $ref->{source}{$k} = $source;
532 $ref->{credit}{$l} = $ref->{amount} * 1;
533 $ref->{credit_accno}{$l} = $ref->{accno};
534 $ref->{credit_taxkey}{$l} = $ref->{taxkey};
535 $ref->{ac_transdate}{$l} = $ref->{transdate};
536 $ref->{source}{$l} = $source;
540 } else { # following lines of a booking, line increasing
543 # $trans_old = $trans_id2; # doesn't seem to be used anymore
544 $trans_id2 = $ref2->{id};
547 (int($balance * 100000) + int(100000 * $ref2->{amount})) / 100000;
549 $ref->{"projectnumbers"}->{$ref2->{"projectnumber"}} = 1 if ($ref2->{"projectnumber"});
551 if ($ref2->{chart_id} > 0) { # all tax accounts, following lines
552 if ($ref2->{amount} < 0) {
553 if ($ref2->{link} =~ /AR_tax/) {
554 if ($ref->{credit_tax_accno}{$j} ne "") {
557 $ref->{credit_tax}{$j} = $ref2->{amount};
558 $ref->{credit_tax_accno}{$j} = $ref2->{accno};
560 if ($ref2->{link} =~ /AP_tax/) {
561 if ($ref->{debit_tax_accno}{$i} ne "") {
564 $ref->{debit_tax}{$i} = $ref2->{amount} * -1;
565 $ref->{debit_tax_accno}{$i} = $ref2->{accno};
568 if ($ref2->{link} =~ /AR_tax/) {
569 if ($ref->{credit_tax_accno}{$j} ne "") {
572 $ref->{credit_tax}{$j} = $ref2->{amount};
573 $ref->{credit_tax_accno}{$j} = $ref2->{accno};
575 if ($ref2->{link} =~ /AP_tax/) {
576 if ($ref->{debit_tax_accno}{$i} ne "") {
579 $ref->{debit_tax}{$i} = $ref2->{amount} * -1;
580 $ref->{debit_tax_accno}{$i} = $ref2->{accno};
583 } else { # all other accounts, following lines
584 if ($ref2->{amount} < 0) {
585 if ($ref->{debit_accno}{$k} ne "") {
588 if ($ref->{source}{$k} ne "") {
593 $ref->{debit}{$k} = $ref2->{amount} * - 1;
594 $ref->{debit_accno}{$k} = $ref2->{accno};
595 $ref->{debit_taxkey}{$k} = $ref2->{taxkey};
596 $ref->{ac_transdate}{$k} = $ref2->{transdate};
597 $ref->{source}{$k} = $source . $space . $ref->{source}{$k};
599 if ($ref->{credit_accno}{$l} ne "") {
602 if ($ref->{source}{$l} ne "") {
607 $ref->{credit}{$l} = $ref2->{amount};
608 $ref->{credit_accno}{$l} = $ref2->{accno};
609 $ref->{credit_taxkey}{$l} = $ref2->{taxkey};
610 $ref->{ac_transdate}{$l} = $ref2->{transdate};
611 $ref->{source}{$l} = $ref->{source}{$l} . $space . $source;
617 push @{ $form->{GL} }, $ref;
620 if ($form->{accno}) {
621 $query = qq|SELECT c.description FROM chart c WHERE c.accno = ?|;
622 ($form->{account_description}) = selectrow_query($form, $dbh, $query, $form->{accno});
627 $main::lxdebug->leave_sub();
631 my ($self, $myconfig, $form) = @_;
632 $main::lxdebug->enter_sub();
634 my ($query, $sth, $ref, @values);
636 # connect to database
637 my $dbh = $form->dbconnect($myconfig);
639 $query = qq|SELECT closedto, revtrans FROM defaults|;
640 ($form->{closedto}, $form->{revtrans}) = selectrow_query($form, $dbh, $query);
642 $query = qq|SELECT id, gldate
644 WHERE id = (SELECT max(id) FROM gl)|;
645 ($form->{previous_id}, $form->{previous_gldate}) = selectrow_query($form, $dbh, $query);
649 qq|SELECT g.reference, g.description, g.notes, g.transdate, g.storno, g.storno_id,
650 d.description AS department, e.name AS employee, g.taxincluded, g.gldate,
651 g.ob_transaction, g.cb_transaction
653 LEFT JOIN department d ON (d.id = g.department_id)
654 LEFT JOIN employee e ON (e.id = g.employee_id)
656 $ref = selectfirst_hashref_query($form, $dbh, $query, conv_i($form->{id}));
657 map { $form->{$_} = $ref->{$_} } keys %$ref;
659 # retrieve individual rows
661 qq|SELECT c.accno, t.taxkey AS accnotaxkey, a.amount, a.memo, a.source,
662 a.transdate, a.cleared, a.project_id, p.projectnumber,
663 a.taxkey, t.rate AS taxrate, t.id,
665 FROM chart c1, tax t1
666 WHERE (t1.id = t.id) AND (c1.id = t.chart_id)) AS taxaccno,
669 WHERE (tk.chart_id = a.chart_id) AND (tk.startdate <= a.transdate)
670 ORDER BY tk.startdate desc LIMIT 1) AS tax_id
672 JOIN chart c ON (c.id = a.chart_id)
673 LEFT JOIN project p ON (p.id = a.project_id)
678 WHERE (tk.taxkey_id = a.taxkey) AND
679 ((CASE WHEN a.chart_id IN
680 (SELECT chart_id FROM taxkeys WHERE taxkey_id = a.taxkey)
681 THEN tk.chart_id = a.chart_id
684 OR (c.link LIKE '%tax%'))
685 AND (startdate <= a.transdate)
686 ORDER BY startdate DESC LIMIT 1))
687 WHERE (a.trans_id = ?)
688 AND (a.fx_transaction = '0')
689 ORDER BY a.acc_trans_id, a.transdate|;
690 $form->{GL} = selectall_hashref_query($form, $dbh, $query, conv_i($form->{id}));
697 WHERE id = (SELECT MAX(id) FROM gl)
700 ($form->{transdate}) = selectrow_query($form, $dbh, $query);
703 # get tax description
704 $query = qq|SELECT * FROM tax ORDER BY taxkey|;
705 $form->{TAX} = selectall_hashref_query($form, $dbh, $query);
707 # get chart of accounts
709 qq|SELECT c.accno, c.description, c.link, tk.taxkey_id, tk.tax_id
711 LEFT JOIN taxkeys tk ON (tk.id =
714 WHERE (taxkeys.chart_id = c.id)
716 ORDER BY startdate DESC
719 $form->{chart} = selectall_hashref_query($form, $dbh, $query, conv_date($form->{transdate}));
723 $main::lxdebug->leave_sub();
727 $main::lxdebug->enter_sub();
729 my ($self, $form, $myconfig, $id) = @_;
731 my ($query, $new_id, $storno_row, $acc_trans_rows);
732 my $dbh = $form->get_standard_dbh($myconfig);
734 $query = qq|SELECT nextval('glid')|;
735 ($new_id) = selectrow_query($form, $dbh, $query);
737 $query = qq|SELECT * FROM gl WHERE id = ?|;
738 $storno_row = selectfirst_hashref_query($form, $dbh, $query, $id);
740 $storno_row->{id} = $new_id;
741 $storno_row->{storno_id} = $id;
742 $storno_row->{storno} = 't';
743 $storno_row->{reference} = 'Storno-' . $storno_row->{reference};
745 delete @$storno_row{qw(itime mtime gldate)};
747 $query = sprintf 'INSERT INTO gl (%s) VALUES (%s)', join(', ', keys %$storno_row), join(', ', map '?', values %$storno_row);
748 do_query($form, $dbh, $query, (values %$storno_row));
750 $query = qq|UPDATE gl SET storno = 't' WHERE id = ?|;
751 do_query($form, $dbh, $query, $id);
753 # now copy acc_trans entries
754 $query = qq|SELECT * FROM acc_trans WHERE trans_id = ?|;
755 my $rowref = selectall_hashref_query($form, $dbh, $query, $id);
757 for my $row (@$rowref) {
758 delete @$row{qw(itime mtime acc_trans_id gldate)};
759 $query = sprintf 'INSERT INTO acc_trans (%s) VALUES (%s)', join(', ', keys %$row), join(', ', map '?', values %$row);
760 $row->{trans_id} = $new_id;
761 $row->{amount} *= -1;
762 do_query($form, $dbh, $query, (values %$row));
767 $main::lxdebug->leave_sub();
770 sub get_chart_balances {
771 $main::lxdebug->enter_sub();
776 Common::check_params(\%params, qw(charts));
778 my $myconfig = \%main::myconfig;
779 my $form = $main::form;
781 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
783 my @ids = map { $_->{id} } @{ $params{charts} };
786 $main::lxdebug->leave_sub();
790 my $query = qq|SELECT chart_id, SUM(amount) AS sum
792 WHERE chart_id IN (| . join(', ', ('?') x scalar(@ids)) . qq|)
795 my %balances = selectall_as_map($form, $dbh, $query, 'chart_id', 'sum', @ids);
797 foreach my $chart (@{ $params{charts} }) {
798 $chart->{balance} = $balances{ $chart->{id} } || 0;
801 $main::lxdebug->leave_sub();