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 @values = (conv_i($form->{id}));
52 do_query($form, $dbh, qq|DELETE FROM acc_trans WHERE trans_id = ?|, @values);
53 do_query($form, $dbh, qq|DELETE FROM gl WHERE id = ?|, @values);
56 my $rc = $dbh->commit;
58 $main::lxdebug->leave_sub();
64 sub post_transaction {
65 my ($self, $myconfig, $form) = @_;
66 $main::lxdebug->enter_sub();
68 my ($debit, $credit) = (0, 0);
73 # connect to database, turn off AutoCommit
74 my $dbh = $form->dbconnect_noauto($myconfig);
76 # post the transaction
77 # make up a unique handle and store in reference field
78 # then retrieve the record based on the unique handle to get the id
79 # replace the reference field with the actual variable
80 # add records to acc_trans
82 # if there is a $form->{id} replace the old transaction
83 # delete all acc_trans entries and add the new ones
85 if (!$form->{taxincluded}) {
86 $form->{taxincluded} = 0;
93 # delete individual transactions
94 $query = qq|DELETE FROM acc_trans WHERE trans_id = ?|;
95 @values = (conv_i($form->{id}));
96 do_query($form, $dbh, $query, @values);
99 $query = qq|SELECT nextval('glid')|;
100 ($form->{id}) = selectrow_query($form, $dbh, $query);
103 qq|INSERT INTO gl (id, employee_id) | .
104 qq|VALUES (?, (SELECT id FROM employee WHERE login = ?))|;
105 @values = ($form->{id}, $form->{login});
106 do_query($form, $dbh, $query, @values);
109 my ($null, $department_id) = split(/--/, $form->{department});
112 $form->{ob_transaction} *= 1;
113 $form->{cb_transaction} *= 1;
117 reference = ?, description = ?, notes = ?,
118 transdate = ?, department_id = ?, taxincluded = ?,
119 storno = ?, storno_id = ?, ob_transaction = ?, cb_transaction = ?
122 @values = ($form->{reference}, $form->{description}, $form->{notes},
123 conv_date($form->{transdate}), $department_id, $form->{taxincluded} ? 't' : 'f',
124 $form->{storno} ? 't' : 'f', conv_i($form->{storno_id}), $form->{ob_transaction} ? 't' : 'f', $form->{cb_transaction} ? 't' : 'f',
125 conv_i($form->{id}));
126 do_query($form, $dbh, $query, @values);
128 # insert acc_trans transactions
129 for $i (1 .. $form->{rowcount}) {
131 my ($accno) = split(/--/, $form->{"accno_$i"});
132 ($form->{"tax_id_$i"}) = split(/--/, $form->{"taxchart_$i"});
133 if ($form->{"tax_id_$i"} ne "") {
134 $query = qq|SELECT taxkey, rate FROM tax WHERE id = ?|;
135 ($taxkey, $rate) = selectrow_query($form, $dbh, $query, conv_i($form->{"tax_id_$i"}));
139 my $debit = $form->{"debit_$i"};
140 my $credit = $form->{"credit_$i"};
141 my $tax = $form->{"tax_$i"};
148 $amount = $debit * -1;
153 $project_id = conv_i($form->{"project_id_$i"});
155 # if there is an amount, add the record
158 qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate,
159 source, memo, project_id, taxkey, ob_transaction, cb_transaction)
160 VALUES (?, (SELECT id FROM chart WHERE accno = ?),
161 ?, ?, ?, ?, ?, ?, ?, ?)|;
162 @values = (conv_i($form->{id}), $accno, $amount, conv_date($form->{transdate}),
163 $form->{"source_$i"}, $form->{"memo_$i"}, $project_id, $taxkey, $form->{ob_transaction} ? 't' : 'f', $form->{cb_transaction} ? 't' : 'f');
164 do_query($form, $dbh, $query, @values);
170 qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate,
171 source, memo, project_id, taxkey)
172 VALUES (?, (SELECT chart_id FROM tax WHERE id = ?),
174 @values = (conv_i($form->{id}), conv_i($form->{"tax_id_$i"}),
175 $tax, conv_date($form->{transdate}), $form->{"source_$i"},
176 $form->{"memo_$i"}, $project_id, $taxkey);
177 do_query($form, $dbh, $query, @values);
181 if ($form->{storno} && $form->{storno_id}) {
182 do_query($form, $dbh, qq|UPDATE gl SET storno = 't' WHERE id = ?|, conv_i($form->{storno_id}));
185 # commit and redirect
186 my $rc = $dbh->commit;
188 $main::lxdebug->leave_sub();
193 sub all_transactions {
194 my ($self, $myconfig, $form) = @_;
195 $main::lxdebug->enter_sub();
197 # connect to database
198 my $dbh = $form->dbconnect($myconfig);
199 my ($query, $sth, $source, $null);
201 my ($glwhere, $arwhere, $apwhere) = ("1 = 1", "1 = 1", "1 = 1");
202 my (@glvalues, @arvalues, @apvalues);
204 if ($form->{reference}) {
205 $glwhere .= qq| AND g.reference ILIKE ?|;
206 $arwhere .= qq| AND a.invnumber ILIKE ?|;
207 $apwhere .= qq| AND a.invnumber ILIKE ?|;
208 push(@glvalues, '%' . $form->{reference} . '%');
209 push(@arvalues, '%' . $form->{reference} . '%');
210 push(@apvalues, '%' . $form->{reference} . '%');
213 if ($form->{department}) {
214 my ($null, $department) = split /--/, $form->{department};
215 $glwhere .= qq| AND g.department_id = ?|;
216 $arwhere .= qq| AND a.department_id = ?|;
217 $apwhere .= qq| AND a.department_id = ?|;
218 push(@glvalues, $department);
219 push(@arvalues, $department);
220 push(@apvalues, $department);
223 if ($form->{source}) {
224 $glwhere .= " AND ac.trans_id IN (SELECT trans_id from acc_trans WHERE source ILIKE ?)";
225 $arwhere .= " AND ac.trans_id IN (SELECT trans_id from acc_trans WHERE source ILIKE ?)";
226 $apwhere .= " AND ac.trans_id IN (SELECT trans_id from acc_trans WHERE source ILIKE ?)";
227 push(@glvalues, '%' . $form->{source} . '%');
228 push(@arvalues, '%' . $form->{source} . '%');
229 push(@apvalues, '%' . $form->{source} . '%');
232 if ($form->{datefrom}) {
233 $glwhere .= " AND ac.transdate >= ?";
234 $arwhere .= " AND ac.transdate >= ?";
235 $apwhere .= " AND ac.transdate >= ?";
236 push(@glvalues, $form->{datefrom});
237 push(@arvalues, $form->{datefrom});
238 push(@apvalues, $form->{datefrom});
241 if ($form->{dateto}) {
242 $glwhere .= " AND ac.transdate <= ?";
243 $arwhere .= " AND ac.transdate <= ?";
244 $apwhere .= " AND ac.transdate <= ?";
245 push(@glvalues, $form->{dateto});
246 push(@arvalues, $form->{dateto});
247 push(@apvalues, $form->{dateto});
250 if ($form->{description}) {
251 $glwhere .= " AND g.description ILIKE ?";
252 $arwhere .= " AND ct.name ILIKE ?";
253 $apwhere .= " AND ct.name ILIKE ?";
254 push(@glvalues, '%' . $form->{description} . '%');
255 push(@arvalues, '%' . $form->{description} . '%');
256 push(@apvalues, '%' . $form->{description} . '%');
259 if ($form->{notes}) {
260 $glwhere .= " AND g.notes ILIKE ?";
261 $arwhere .= " AND a.notes ILIKE ?";
262 $apwhere .= " AND a.notes ILIKE ?";
263 push(@glvalues, '%' . $form->{notes} . '%');
264 push(@arvalues, '%' . $form->{notes} . '%');
265 push(@apvalues, '%' . $form->{notes} . '%');
268 if ($form->{accno}) {
269 $glwhere .= " AND c.accno = '$form->{accno}'";
270 $arwhere .= " AND c.accno = '$form->{accno}'";
271 $apwhere .= " AND c.accno = '$form->{accno}'";
274 if ($form->{category} ne 'X') {
275 $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 = ?))|;
276 $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 = ?))|;
277 $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 = ?))|;
278 push(@glvalues, $form->{category});
279 push(@arvalues, $form->{category});
280 push(@apvalues, $form->{category});
283 if ($form->{project_id}) {
284 $glwhere .= qq| AND g.id IN (SELECT DISTINCT trans_id FROM acc_trans WHERE project_id = ?)|;
286 qq| AND ((a.globalproject_id = ?) OR
287 (a.id IN (SELECT DISTINCT trans_id FROM acc_trans WHERE project_id = ?)))|;
289 qq| AND ((a.globalproject_id = ?) OR
290 (a.id IN (SELECT DISTINCT trans_id FROM acc_trans WHERE project_id = ?)))|;
291 my $project_id = conv_i($form->{project_id});
292 push(@glvalues, $project_id);
293 push(@arvalues, $project_id, $project_id);
294 push(@apvalues, $project_id, $project_id);
297 my ($project_columns, %project_join);
298 if ($form->{"l_projectnumbers"}) {
299 $project_columns = qq|, ac.project_id, pr.projectnumber|;
300 $project_join = qq|LEFT JOIN project pr ON (ac.project_id = pr.id)|;
303 if ($form->{accno}) {
304 # get category for account
305 $query = qq|SELECT category FROM chart WHERE accno = ?|;
306 ($form->{ml}) = selectrow_query($form, $dbh, $query, $form->{accno});
308 if ($form->{datefrom}) {
310 qq|SELECT SUM(ac.amount)
312 LEFT JOIN chart c ON (ac.chart_id = c.id)
313 WHERE (c.accno = ?) AND (ac.transdate < ?)|;
314 ($form->{balance}) = selectrow_query($form, $dbh, $query, $form->{accno}, conv_date($form->{datefrom}));
318 my $false = ($myconfig->{dbdriver} eq 'Pg') ? FALSE: q|'0'|;
322 'transdate' => [ qw(transdate id) ],
323 'reference' => [ qw(lower_reference id) ],
324 'description' => [ qw(lower_description id) ],
325 'accno' => [ qw(accno transdate id) ],
327 my %lowered_columns = (
328 'reference' => { 'gl' => 'g.reference', 'arap' => 'a.invnumber', },
329 'source' => { 'gl' => 'ac.source', 'arap' => 'ac.source', },
330 'description' => { 'gl' => 'g.description', 'arap' => 'ct.name', },
333 my $sortdir = !defined $form->{sortdir} ? 'ASC' : $form->{sortdir} ? 'ASC' : 'DESC';
334 my $sortkey = $sort_columns{$form->{sort}} ? $form->{sort} : 'transdate';
335 my $sortorder = join ', ', map { "$_ $sortdir" } @{ $sort_columns{$sortkey} };
337 my %columns_for_sorting = ( 'gl' => '', 'arap' => '', );
338 foreach my $spec (@{ $sort_columns{$sortkey} }) {
339 next if ($spec !~ m/^lower_(.*)$/);
342 map { $columns_for_sorting{$_} .= sprintf(', lower(%s) AS lower_%s', $lowered_columns{$column}->{$_}, $column) } qw(gl arap);
347 ac.acc_trans_id, 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
351 $columns_for_sorting{gl}
352 FROM gl g, acc_trans ac $project_join, chart c
353 LEFT JOIN tax t ON (t.chart_id = c.id)
355 AND (ac.chart_id = c.id)
356 AND (g.id = ac.trans_id)
360 SELECT ac.acc_trans_id, a.id, 'ar' AS type, a.invoice, a.invnumber, ac.taxkey, c.link,
361 ct.name, ac.transdate, ac.source, ac.trans_id,
362 ac.amount, c.accno, a.notes, t.chart_id
364 $columns_for_sorting{arap}
365 FROM ar a, acc_trans ac $project_join, customer ct, chart c
366 LEFT JOIN tax t ON (t.chart_id=c.id)
368 AND (ac.chart_id = c.id)
369 AND (a.customer_id = ct.id)
370 AND (a.id = ac.trans_id)
374 SELECT ac.acc_trans_id, a.id, 'ap' 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, a.notes, t.chart_id
378 $columns_for_sorting{arap}
379 FROM ap a, acc_trans ac $project_join, vendor ct, chart c
380 LEFT JOIN tax t ON (t.chart_id=c.id)
382 AND (ac.chart_id = c.id)
383 AND (a.vendor_id = ct.id)
384 AND (a.id = ac.trans_id)
386 ORDER BY $sortorder, acc_trans_id $sortdir|;
388 my @values = (@glvalues, @arvalues, @apvalues);
390 # Show all $query in Debuglevel LXDebug::QUERY
391 $callingdetails = (caller (0))[3];
392 dump_query(LXDebug::QUERY, "$callingdetails", $query, @values);
394 $sth = prepare_execute_query($form, $dbh, $query, @values);
398 my ($i, $j, $k, $l, $ref, $ref2);
401 while (my $ref0 = $sth->fetchrow_hashref(NAME_lc)) {
403 $trans_id = $ref0->{id};
405 my $source = $ref0->{source};
406 undef($ref0->{source});
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 $ref->{"projectnumbers"} = {};
442 $ref->{"projectnumbers"}->{$ref->{"projectnumber"}} = 1 if ($ref->{"projectnumber"});
444 $balance = $ref->{amount};
446 # Linenumbers of General Ledger
447 $k = 0; # Debit # AP # Soll
448 $l = 0; # Credit # AR # Haben
449 $i = 0; # Debit Tax # AP_tax # VSt
450 $j = 0; # Credit Tax # AR_tax # USt
452 if ($ref->{chart_id} > 0) { # all tax accounts first line, no line increasing
453 if ($ref->{amount} < 0) {
454 if ($ref->{link} =~ /AR_tax/) {
455 $ref->{credit_tax}{$j} = $ref->{amount};
456 $ref->{credit_tax_accno}{$j} = $ref->{accno};
458 if ($ref->{link} =~ /AP_tax/) {
459 $ref->{debit_tax}{$i} = $ref->{amount} * -1;
460 $ref->{debit_tax_accno}{$i} = $ref->{accno};
463 if ($ref->{link} =~ /AR_tax/) {
464 $ref->{credit_tax}{$j} = $ref->{amount};
465 $ref->{credit_tax_accno}{$j} = $ref->{accno};
467 if ($ref->{link} =~ /AP_tax/) {
468 $ref->{debit_tax}{$i} = $ref->{amount} * -1;
469 $ref->{debit_tax_accno}{$i} = $ref->{accno};
472 } else { #all other accounts first line
474 if ($ref->{amount} < 0) {
475 $ref->{debit}{$k} = $ref->{amount} * -1;
476 $ref->{debit_accno}{$k} = $ref->{accno};
477 $ref->{debit_taxkey}{$k} = $ref->{taxkey};
478 $ref->{ac_transdate}{$k} = $ref->{transdate};
479 $ref->{source}{$k} = $source;
481 $ref->{credit}{$l} = $ref->{amount} * 1;
482 $ref->{credit_accno}{$l} = $ref->{accno};
483 $ref->{credit_taxkey}{$l} = $ref->{taxkey};
484 $ref->{ac_transdate}{$l} = $ref->{transdate};
485 $ref->{source}{$l} = $source;
489 } else { # following lines of a booking, line increasing
492 $trans_old = $trans_id2;
493 $trans_id2 = $ref2->{id};
496 (int($balance * 100000) + int(100000 * $ref2->{amount})) / 100000;
498 $ref->{"projectnumbers"}->{$ref2->{"projectnumber"}} = 1 if ($ref2->{"projectnumber"});
500 if ($ref2->{chart_id} > 0) { # all tax accounts, following lines
501 if ($ref2->{amount} < 0) {
502 if ($ref2->{link} =~ /AR_tax/) {
503 if ($ref->{credit_tax_accno}{$j} ne "") {
506 $ref->{credit_tax}{$j} = $ref2->{amount};
507 $ref->{credit_tax_accno}{$j} = $ref2->{accno};
509 if ($ref2->{link} =~ /AP_tax/) {
510 if ($ref->{debit_tax_accno}{$i} ne "") {
513 $ref->{debit_tax}{$i} = $ref2->{amount} * -1;
514 $ref->{debit_tax_accno}{$i} = $ref2->{accno};
517 if ($ref2->{link} =~ /AR_tax/) {
518 if ($ref->{credit_tax_accno}{$j} ne "") {
521 $ref->{credit_tax}{$j} = $ref2->{amount};
522 $ref->{credit_tax_accno}{$j} = $ref2->{accno};
524 if ($ref2->{link} =~ /AP_tax/) {
525 if ($ref->{debit_tax_accno}{$i} ne "") {
528 $ref->{debit_tax}{$i} = $ref2->{amount} * -1;
529 $ref->{debit_tax_accno}{$i} = $ref2->{accno};
532 } else { # all other accounts, following lines
533 if ($ref2->{amount} < 0) {
534 if ($ref->{debit_accno}{$k} ne "") {
537 if ($ref->{source}{$k} ne "") {
542 $ref->{debit}{$k} = $ref2->{amount} * - 1;
543 $ref->{debit_accno}{$k} = $ref2->{accno};
544 $ref->{debit_taxkey}{$k} = $ref2->{taxkey};
545 $ref->{ac_transdate}{$k} = $ref2->{transdate};
546 $ref->{source}{$k} = $source . $space . $ref->{source}{$k};
548 if ($ref->{credit_accno}{$l} ne "") {
551 if ($ref->{source}{$l} ne "") {
556 $ref->{credit}{$l} = $ref2->{amount};
557 $ref->{credit_accno}{$l} = $ref2->{accno};
558 $ref->{credit_taxkey}{$l} = $ref2->{taxkey};
559 $ref->{ac_transdate}{$l} = $ref2->{transdate};
560 $ref->{source}{$l} = $ref->{source}{$l} . $space . $source;
566 push @{ $form->{GL} }, $ref;
569 if ($form->{accno}) {
570 $query = qq|SELECT c.description FROM chart c WHERE c.accno = ?|;
571 ($form->{account_description}) = selectrow_query($form, $dbh, $query, $form->{accno});
576 $main::lxdebug->leave_sub();
580 my ($self, $myconfig, $form) = @_;
581 $main::lxdebug->enter_sub();
583 my ($query, $sth, $ref, @values);
585 # connect to database
586 my $dbh = $form->dbconnect($myconfig);
588 $query = qq|SELECT closedto, revtrans FROM defaults|;
589 ($form->{closedto}, $form->{revtrans}) = selectrow_query($form, $dbh, $query);
591 $query = qq|SELECT id, gldate
593 WHERE id = (SELECT max(id) FROM gl)|;
594 ($form->{previous_id}, $form->{previous_gldate}) = selectrow_query($form, $dbh, $query);
598 qq|SELECT g.reference, g.description, g.notes, g.transdate, g.storno, g.storno_id,
599 d.description AS department, e.name AS employee, g.taxincluded, g.gldate,
600 g.ob_transaction, g.cb_transaction
602 LEFT JOIN department d ON (d.id = g.department_id)
603 LEFT JOIN employee e ON (e.id = g.employee_id)
605 $ref = selectfirst_hashref_query($form, $dbh, $query, conv_i($form->{id}));
606 map { $form->{$_} = $ref->{$_} } keys %$ref;
608 # retrieve individual rows
610 qq|SELECT c.accno, t.taxkey AS accnotaxkey, a.amount, a.memo, a.source,
611 a.transdate, a.cleared, a.project_id, p.projectnumber,
612 a.taxkey, t.rate AS taxrate, t.id,
614 FROM chart c1, tax t1
615 WHERE (t1.id = t.id) AND (c1.id = t.chart_id)) AS taxaccno,
618 WHERE (tk.chart_id = a.chart_id) AND (tk.startdate <= a.transdate)
619 ORDER BY tk.startdate desc LIMIT 1) AS tax_id
621 JOIN chart c ON (c.id = a.chart_id)
622 LEFT JOIN project p ON (p.id = a.project_id)
627 WHERE (tk.taxkey_id = a.taxkey) AND
628 ((CASE WHEN a.chart_id IN
629 (SELECT chart_id FROM taxkeys WHERE taxkey_id = a.taxkey)
630 THEN tk.chart_id = a.chart_id
633 OR (c.link LIKE '%tax%'))
634 AND (startdate <= a.transdate)
635 ORDER BY startdate DESC LIMIT 1))
636 WHERE (a.trans_id = ?)
637 AND (a.fx_transaction = '0')
638 ORDER BY a.acc_trans_id, a.transdate|;
639 $form->{GL} = selectall_hashref_query($form, $dbh, $query, conv_i($form->{id}));
646 WHERE id = (SELECT MAX(id) FROM gl)
649 ($form->{transdate}) = selectrow_query($form, $dbh, $query);
652 # get tax description
653 $query = qq|SELECT * FROM tax ORDER BY taxkey|;
654 $form->{TAX} = selectall_hashref_query($form, $dbh, $query);
656 # get chart of accounts
658 qq|SELECT c.accno, c.description, c.link, tk.taxkey_id, tk.tax_id
660 LEFT JOIN taxkeys tk ON (tk.id =
663 WHERE (taxkeys.chart_id = c.id)
665 ORDER BY startdate DESC
668 $form->{chart} = selectall_hashref_query($form, $dbh, $query, conv_date($form->{transdate}));
672 $main::lxdebug->leave_sub();
676 $main::lxdebug->enter_sub();
678 my ($self, $form, $myconfig, $id) = @_;
680 my ($query, $new_id, $storno_row, $acc_trans_rows);
681 my $dbh = $form->get_standard_dbh($myconfig);
683 $query = qq|SELECT nextval('glid')|;
684 ($new_id) = selectrow_query($form, $dbh, $query);
686 $query = qq|SELECT * FROM gl WHERE id = ?|;
687 $storno_row = selectfirst_hashref_query($form, $dbh, $query, $id);
689 $storno_row->{id} = $new_id;
690 $storno_row->{storno_id} = $id;
691 $storno_row->{storno} = 't';
692 $storno_row->{reference} = 'Storno-' . $storno_row->{reference};
694 delete @$storno_row{qw(itime mtime)};
696 $query = sprintf 'INSERT INTO gl (%s) VALUES (%s)', join(', ', keys %$storno_row), join(', ', map '?', values %$storno_row);
697 do_query($form, $dbh, $query, (values %$storno_row));
699 $query = qq|UPDATE gl SET storno = 't' WHERE id = ?|;
700 do_query($form, $dbh, $query, $id);
702 # now copy acc_trans entries
703 $query = qq|SELECT * FROM acc_trans WHERE trans_id = ?|;
704 my $rowref = selectall_hashref_query($form, $dbh, $query, $id);
706 for my $row (@$rowref) {
707 delete @$row{qw(itime mtime)};
708 $query = sprintf 'INSERT INTO acc_trans (%s) VALUES (%s)', join(', ', keys %$row), join(', ', map '?', values %$row);
709 $row->{trans_id} = $new_id;
710 $row->{amount} *= -1;
711 do_query($form, $dbh, $query, (values %$row));
716 $main::lxdebug->leave_sub();
719 sub get_chart_balances {
720 $main::lxdebug->enter_sub();
725 Common::check_params(\%params, qw(charts));
727 my $myconfig = \%main::myconfig;
728 my $form = $main::form;
730 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
732 my @ids = map { $_->{id} } @{ $params{charts} };
735 $main::lxdebug->leave_sub();
739 my $query = qq|SELECT chart_id, SUM(amount) AS sum
741 WHERE chart_id IN (| . join(', ', ('?') x scalar(@ids)) . qq|)
744 my %balances = selectall_as_map($form, $dbh, $query, 'chart_id', 'sum', @ids);
746 foreach my $chart (@{ $params{charts} }) {
747 $chart->{balance} = $balances{ $chart->{id} } || 0;
750 $main::lxdebug->leave_sub();