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 #======================================================================
46 sub delete_transaction {
47 my ($self, $myconfig, $form) = @_;
48 $main::lxdebug->enter_sub();
51 my $dbh = $form->dbconnect_noauto($myconfig);
53 my @values = (conv_i($form->{id}));
54 do_query($form, $dbh, qq|DELETE FROM acc_trans WHERE trans_id = ?|, @values);
55 do_query($form, $dbh, qq|DELETE FROM gl WHERE id = ?|, @values);
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 # connect to database, turn off AutoCommit
76 my $dbh = $form->dbconnect_noauto($myconfig);
78 # post the transaction
79 # make up a unique handle and store in reference field
80 # then retrieve the record based on the unique handle to get the id
81 # replace the reference field with the actual variable
82 # add records to acc_trans
84 # if there is a $form->{id} replace the old transaction
85 # delete all acc_trans entries and add the new ones
87 if (!$form->{taxincluded}) {
88 $form->{taxincluded} = 0;
91 my ($query, $sth, @values, $taxkey, $rate, $posted);
95 # delete individual transactions
96 $query = qq|DELETE FROM acc_trans WHERE trans_id = ?|;
97 @values = (conv_i($form->{id}));
98 do_query($form, $dbh, $query, @values);
101 $query = qq|SELECT nextval('glid')|;
102 ($form->{id}) = selectrow_query($form, $dbh, $query);
105 qq|INSERT INTO gl (id, employee_id) | .
106 qq|VALUES (?, (SELECT id FROM employee WHERE login = ?))|;
107 @values = ($form->{id}, $form->{login});
108 do_query($form, $dbh, $query, @values);
111 my ($null, $department_id) = split(/--/, $form->{department});
114 $form->{ob_transaction} *= 1;
115 $form->{cb_transaction} *= 1;
119 reference = ?, description = ?, notes = ?,
120 transdate = ?, department_id = ?, taxincluded = ?,
121 storno = ?, storno_id = ?, ob_transaction = ?, cb_transaction = ?
124 @values = ($form->{reference}, $form->{description}, $form->{notes},
125 conv_date($form->{transdate}), $department_id, $form->{taxincluded} ? 't' : 'f',
126 $form->{storno} ? 't' : 'f', conv_i($form->{storno_id}), $form->{ob_transaction} ? 't' : 'f', $form->{cb_transaction} ? 't' : 'f',
127 conv_i($form->{id}));
128 do_query($form, $dbh, $query, @values);
130 # insert acc_trans transactions
131 for $i (1 .. $form->{rowcount}) {
133 my ($accno) = split(/--/, $form->{"accno_$i"});
134 ($form->{"tax_id_$i"}) = split(/--/, $form->{"taxchart_$i"});
135 if ($form->{"tax_id_$i"} ne "") {
136 $query = qq|SELECT taxkey, rate FROM tax WHERE id = ?|;
137 ($taxkey, $rate) = selectrow_query($form, $dbh, $query, conv_i($form->{"tax_id_$i"}));
141 my $debit = $form->{"debit_$i"};
142 my $credit = $form->{"credit_$i"};
143 my $tax = $form->{"tax_$i"};
150 $amount = $debit * -1;
155 $project_id = conv_i($form->{"project_id_$i"});
157 # if there is an amount, add the record
160 qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate,
161 source, memo, project_id, taxkey, ob_transaction, cb_transaction)
162 VALUES (?, (SELECT id FROM chart WHERE accno = ?),
163 ?, ?, ?, ?, ?, ?, ?, ?)|;
164 @values = (conv_i($form->{id}), $accno, $amount, conv_date($form->{transdate}),
165 $form->{"source_$i"}, $form->{"memo_$i"}, $project_id, $taxkey, $form->{ob_transaction} ? 't' : 'f', $form->{cb_transaction} ? 't' : 'f');
166 do_query($form, $dbh, $query, @values);
172 qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate,
173 source, memo, project_id, taxkey)
174 VALUES (?, (SELECT chart_id FROM tax WHERE id = ?),
176 @values = (conv_i($form->{id}), conv_i($form->{"tax_id_$i"}),
177 $tax, conv_date($form->{transdate}), $form->{"source_$i"},
178 $form->{"memo_$i"}, $project_id, $taxkey);
179 do_query($form, $dbh, $query, @values);
183 if ($form->{storno} && $form->{storno_id}) {
184 do_query($form, $dbh, qq|UPDATE gl SET storno = 't' WHERE id = ?|, conv_i($form->{storno_id}));
187 # commit and redirect
188 my $rc = $dbh->commit;
190 $main::lxdebug->leave_sub();
195 sub all_transactions {
196 my ($self, $myconfig, $form) = @_;
197 $main::lxdebug->enter_sub();
199 # connect to database
200 my $dbh = $form->dbconnect($myconfig);
201 my ($query, $sth, $source, $null, $space);
203 my ($glwhere, $arwhere, $apwhere) = ("1 = 1", "1 = 1", "1 = 1");
204 my (@glvalues, @arvalues, @apvalues);
206 if ($form->{reference}) {
207 $glwhere .= qq| AND g.reference ILIKE ?|;
208 $arwhere .= qq| AND a.invnumber ILIKE ?|;
209 $apwhere .= qq| AND a.invnumber ILIKE ?|;
210 push(@glvalues, '%' . $form->{reference} . '%');
211 push(@arvalues, '%' . $form->{reference} . '%');
212 push(@apvalues, '%' . $form->{reference} . '%');
215 if ($form->{department}) {
216 my ($null, $department) = split /--/, $form->{department};
217 $glwhere .= qq| AND g.department_id = ?|;
218 $arwhere .= qq| AND a.department_id = ?|;
219 $apwhere .= qq| AND a.department_id = ?|;
220 push(@glvalues, $department);
221 push(@arvalues, $department);
222 push(@apvalues, $department);
225 if ($form->{source}) {
226 $glwhere .= " AND ac.trans_id IN (SELECT trans_id from acc_trans WHERE source ILIKE ?)";
227 $arwhere .= " AND ac.trans_id IN (SELECT trans_id from acc_trans WHERE source ILIKE ?)";
228 $apwhere .= " AND ac.trans_id IN (SELECT trans_id from acc_trans WHERE source ILIKE ?)";
229 push(@glvalues, '%' . $form->{source} . '%');
230 push(@arvalues, '%' . $form->{source} . '%');
231 push(@apvalues, '%' . $form->{source} . '%');
234 # default Datumseinschränkung falls nicht oder falsch übergeben (sollte nie passieren)
235 $form->{datesort} = 'transdate' unless $form->{datesort} =~ /^(transdate|gldate)$/;
237 if ($form->{datefrom}) {
238 $glwhere .= " AND ac.$form->{datesort} >= ?";
239 $arwhere .= " AND ac.$form->{datesort} >= ?";
240 $apwhere .= " AND ac.$form->{datesort} >= ?";
241 push(@glvalues, $form->{datefrom});
242 push(@arvalues, $form->{datefrom});
243 push(@apvalues, $form->{datefrom});
246 if ($form->{dateto}) {
247 $glwhere .= " AND ac.$form->{datesort} <= ?";
248 $arwhere .= " AND ac.$form->{datesort} <= ?";
249 $apwhere .= " AND ac.$form->{datesort} <= ?";
250 push(@glvalues, $form->{dateto});
251 push(@arvalues, $form->{dateto});
252 push(@apvalues, $form->{dateto});
255 if ($form->{description}) {
256 $glwhere .= " AND g.description ILIKE ?";
257 $arwhere .= " AND ct.name ILIKE ?";
258 $apwhere .= " AND ct.name ILIKE ?";
259 push(@glvalues, '%' . $form->{description} . '%');
260 push(@arvalues, '%' . $form->{description} . '%');
261 push(@apvalues, '%' . $form->{description} . '%');
264 if ($form->{employee} =~ /--/) {
265 ($form->{employee_id},$form->{employee_name}) = split(/--/,$form->{employee});
266 $query .= " AND o.employee_id = ?";
267 push @values, conv_i($form->{employee_id});
268 #if ($form->{employee_id}) {
269 $glwhere .= " AND g.employee_id = ? ";
270 $arwhere .= " AND a.employee_id = ? ";
271 $apwhere .= " AND a.employee_id = ? ";
272 push(@glvalues, conv_i($form->{employee_id}));
273 push(@arvalues, conv_i($form->{employee_id}));
274 push(@apvalues, conv_i($form->{employee_id}));
277 if ($form->{notes}) {
278 $glwhere .= " AND g.notes ILIKE ?";
279 $arwhere .= " AND a.notes ILIKE ?";
280 $apwhere .= " AND a.notes ILIKE ?";
281 push(@glvalues, '%' . $form->{notes} . '%');
282 push(@arvalues, '%' . $form->{notes} . '%');
283 push(@apvalues, '%' . $form->{notes} . '%');
286 if ($form->{accno}) {
287 $glwhere .= " AND c.accno = '$form->{accno}'";
288 $arwhere .= " AND c.accno = '$form->{accno}'";
289 $apwhere .= " AND c.accno = '$form->{accno}'";
292 if ($form->{category} ne 'X') {
293 $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 = ?))|;
294 $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 = ?))|;
295 $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 = ?))|;
296 push(@glvalues, $form->{category});
297 push(@arvalues, $form->{category});
298 push(@apvalues, $form->{category});
301 if ($form->{project_id}) {
302 $glwhere .= qq| AND g.id IN (SELECT DISTINCT trans_id FROM acc_trans WHERE project_id = ?)|;
304 qq| AND ((a.globalproject_id = ?) OR
305 (a.id IN (SELECT DISTINCT trans_id FROM acc_trans WHERE project_id = ?)))|;
307 qq| AND ((a.globalproject_id = ?) OR
308 (a.id IN (SELECT DISTINCT trans_id FROM acc_trans WHERE project_id = ?)))|;
309 my $project_id = conv_i($form->{project_id});
310 push(@glvalues, $project_id);
311 push(@arvalues, $project_id, $project_id);
312 push(@apvalues, $project_id, $project_id);
315 my ($project_columns, $project_join);
316 if ($form->{"l_projectnumbers"}) {
317 $project_columns = qq|, ac.project_id, pr.projectnumber|;
318 $project_join = qq|LEFT JOIN project pr ON (ac.project_id = pr.id)|;
321 if ($form->{accno}) {
322 # get category for account
323 $query = qq|SELECT category FROM chart WHERE accno = ?|;
324 ($form->{ml}) = selectrow_query($form, $dbh, $query, $form->{accno});
326 if ($form->{datefrom}) {
328 qq|SELECT SUM(ac.amount)
330 LEFT JOIN chart c ON (ac.chart_id = c.id)
331 WHERE (c.accno = ?) AND (ac.$form->{datesort} < ?)|;
332 ($form->{balance}) = selectrow_query($form, $dbh, $query, $form->{accno}, conv_date($form->{datefrom}));
336 my $false = ($myconfig->{dbdriver} eq 'Pg') ? "FALSE" : q|'0'|;
340 'transdate' => [ qw(transdate id) ],
341 'gldate' => [ qw(gldate id) ],
342 'reference' => [ qw(lower_reference id) ],
343 'description' => [ qw(lower_description id) ],
344 'accno' => [ qw(accno transdate id) ],
346 my %lowered_columns = (
347 'reference' => { 'gl' => 'g.reference', 'arap' => 'a.invnumber', },
348 'source' => { 'gl' => 'ac.source', 'arap' => 'ac.source', },
349 'description' => { 'gl' => 'g.description', 'arap' => 'ct.name', },
352 # sortdir = sort direction (ascending or descending)
353 my $sortdir = !defined $form->{sortdir} ? 'ASC' : $form->{sortdir} ? 'ASC' : 'DESC';
354 my $sortkey = $sort_columns{$form->{sort}} ? $form->{sort} : $form->{datesort}; # default used to be transdate
355 my $sortorder = join ', ', map { "$_ $sortdir" } @{ $sort_columns{$sortkey} };
357 my %columns_for_sorting = ( 'gl' => '', 'arap' => '', );
358 foreach my $spec (@{ $sort_columns{$sortkey} }) {
359 next if ($spec !~ m/^lower_(.*)$/);
362 map { $columns_for_sorting{$_} .= sprintf(', lower(%s) AS lower_%s', $lowered_columns{$column}->{$_}, $column) } qw(gl arap);
367 ac.acc_trans_id, g.id, 'gl' AS type, $false AS invoice, g.reference, ac.taxkey, c.link,
368 g.description, ac.transdate, ac.gldate, ac.source, ac.trans_id,
369 ac.amount, c.accno, g.notes, t.chart_id,
370 CASE WHEN (COALESCE(e.name, '') = '') THEN e.login ELSE e.name END AS employee
372 $columns_for_sorting{gl}
373 FROM gl g, acc_trans ac $project_join, chart c
374 LEFT JOIN tax t ON (t.chart_id = c.id)
376 AND (ac.chart_id = c.id)
377 AND (g.id = ac.trans_id)
381 SELECT ac.acc_trans_id, a.id, 'ar' AS type, a.invoice, a.invnumber, ac.taxkey, c.link,
382 ct.name, ac.transdate, ac.gldate, ac.source, ac.trans_id,
383 ac.amount, c.accno, a.notes, t.chart_id,
384 CASE WHEN (COALESCE(e.name, '') = '') THEN e.login ELSE e.name END AS employee
386 $columns_for_sorting{arap}
387 FROM ar a, acc_trans ac $project_join, customer ct, chart c
388 LEFT JOIN tax t ON (t.chart_id=c.id)
390 AND (ac.chart_id = c.id)
391 AND (a.customer_id = ct.id)
392 AND (a.id = ac.trans_id)
396 SELECT ac.acc_trans_id, a.id, 'ap' AS type, a.invoice, a.invnumber, ac.taxkey, c.link,
397 ct.name, ac.transdate, ac.gldate, ac.source, ac.trans_id,
398 ac.amount, c.accno, a.notes, t.chart_id,
399 CASE WHEN (COALESCE(e.name, '') = '') THEN e.login ELSE e.name END AS employee
401 $columns_for_sorting{arap}
402 FROM ap a, acc_trans ac $project_join, vendor ct, chart c
403 LEFT JOIN tax t ON (t.chart_id=c.id)
405 AND (ac.chart_id = c.id)
406 AND (a.vendor_id = ct.id)
407 AND (a.id = ac.trans_id)
409 ORDER BY $sortorder, acc_trans_id $sortdir|;
410 # ORDER BY gldate DESC, id DESC, acc_trans_id DESC
412 my @values = (@glvalues, @arvalues, @apvalues);
414 # Show all $query in Debuglevel LXDebug::QUERY
415 my $callingdetails = (caller (0))[3];
416 dump_query(LXDebug->QUERY(), "$callingdetails", $query, @values);
418 $sth = prepare_execute_query($form, $dbh, $query, @values);
423 my ($i, $j, $k, $l, $ref, $ref2);
426 while (my $ref0 = $sth->fetchrow_hashref("NAME_lc")) {
428 $trans_id = $ref0->{id};
430 my $source = $ref0->{source};
431 undef($ref0->{source});
433 if ($trans_id != $trans_id2) { # first line of a booking
436 push(@{ $form->{GL} }, $ref);
441 $trans_id2 = $ref->{id};
444 if ($ref->{type} eq "gl") {
445 $ref->{module} = "gl";
449 if ($ref->{type} eq "ap") {
450 if ($ref->{invoice}) {
451 $ref->{module} = "ir";
453 $ref->{module} = "ap";
458 if ($ref->{type} eq "ar") {
459 if ($ref->{invoice}) {
460 $ref->{module} = "is";
462 $ref->{module} = "ar";
466 $ref->{"projectnumbers"} = {};
467 $ref->{"projectnumbers"}->{$ref->{"projectnumber"}} = 1 if ($ref->{"projectnumber"});
469 $balance = $ref->{amount};
471 # Linenumbers of General Ledger
472 $k = 0; # Debit # AP # Soll
473 $l = 0; # Credit # AR # Haben
474 $i = 0; # Debit Tax # AP_tax # VSt
475 $j = 0; # Credit Tax # AR_tax # USt
477 if ($ref->{chart_id} > 0) { # all tax accounts first line, no line increasing
478 if ($ref->{amount} < 0) {
479 if ($ref->{link} =~ /AR_tax/) {
480 $ref->{credit_tax}{$j} = $ref->{amount};
481 $ref->{credit_tax_accno}{$j} = $ref->{accno};
483 if ($ref->{link} =~ /AP_tax/) {
484 $ref->{debit_tax}{$i} = $ref->{amount} * -1;
485 $ref->{debit_tax_accno}{$i} = $ref->{accno};
488 if ($ref->{link} =~ /AR_tax/) {
489 $ref->{credit_tax}{$j} = $ref->{amount};
490 $ref->{credit_tax_accno}{$j} = $ref->{accno};
492 if ($ref->{link} =~ /AP_tax/) {
493 $ref->{debit_tax}{$i} = $ref->{amount} * -1;
494 $ref->{debit_tax_accno}{$i} = $ref->{accno};
497 } else { #all other accounts first line
499 if ($ref->{amount} < 0) {
500 $ref->{debit}{$k} = $ref->{amount} * -1;
501 $ref->{debit_accno}{$k} = $ref->{accno};
502 $ref->{debit_taxkey}{$k} = $ref->{taxkey};
503 $ref->{ac_transdate}{$k} = $ref->{transdate};
504 $ref->{source}{$k} = $source;
506 $ref->{credit}{$l} = $ref->{amount} * 1;
507 $ref->{credit_accno}{$l} = $ref->{accno};
508 $ref->{credit_taxkey}{$l} = $ref->{taxkey};
509 $ref->{ac_transdate}{$l} = $ref->{transdate};
510 $ref->{source}{$l} = $source;
514 } else { # following lines of a booking, line increasing
517 # $trans_old = $trans_id2; # doesn't seem to be used anymore
518 $trans_id2 = $ref2->{id};
521 (int($balance * 100000) + int(100000 * $ref2->{amount})) / 100000;
523 $ref->{"projectnumbers"}->{$ref2->{"projectnumber"}} = 1 if ($ref2->{"projectnumber"});
525 if ($ref2->{chart_id} > 0) { # all tax accounts, following lines
526 if ($ref2->{amount} < 0) {
527 if ($ref2->{link} =~ /AR_tax/) {
528 if ($ref->{credit_tax_accno}{$j} ne "") {
531 $ref->{credit_tax}{$j} = $ref2->{amount};
532 $ref->{credit_tax_accno}{$j} = $ref2->{accno};
534 if ($ref2->{link} =~ /AP_tax/) {
535 if ($ref->{debit_tax_accno}{$i} ne "") {
538 $ref->{debit_tax}{$i} = $ref2->{amount} * -1;
539 $ref->{debit_tax_accno}{$i} = $ref2->{accno};
542 if ($ref2->{link} =~ /AR_tax/) {
543 if ($ref->{credit_tax_accno}{$j} ne "") {
546 $ref->{credit_tax}{$j} = $ref2->{amount};
547 $ref->{credit_tax_accno}{$j} = $ref2->{accno};
549 if ($ref2->{link} =~ /AP_tax/) {
550 if ($ref->{debit_tax_accno}{$i} ne "") {
553 $ref->{debit_tax}{$i} = $ref2->{amount} * -1;
554 $ref->{debit_tax_accno}{$i} = $ref2->{accno};
557 } else { # all other accounts, following lines
558 if ($ref2->{amount} < 0) {
559 if ($ref->{debit_accno}{$k} ne "") {
562 if ($ref->{source}{$k} ne "") {
567 $ref->{debit}{$k} = $ref2->{amount} * - 1;
568 $ref->{debit_accno}{$k} = $ref2->{accno};
569 $ref->{debit_taxkey}{$k} = $ref2->{taxkey};
570 $ref->{ac_transdate}{$k} = $ref2->{transdate};
571 $ref->{source}{$k} = $source . $space . $ref->{source}{$k};
573 if ($ref->{credit_accno}{$l} ne "") {
576 if ($ref->{source}{$l} ne "") {
581 $ref->{credit}{$l} = $ref2->{amount};
582 $ref->{credit_accno}{$l} = $ref2->{accno};
583 $ref->{credit_taxkey}{$l} = $ref2->{taxkey};
584 $ref->{ac_transdate}{$l} = $ref2->{transdate};
585 $ref->{source}{$l} = $ref->{source}{$l} . $space . $source;
591 push @{ $form->{GL} }, $ref;
594 if ($form->{accno}) {
595 $query = qq|SELECT c.description FROM chart c WHERE c.accno = ?|;
596 ($form->{account_description}) = selectrow_query($form, $dbh, $query, $form->{accno});
601 $main::lxdebug->leave_sub();
605 my ($self, $myconfig, $form) = @_;
606 $main::lxdebug->enter_sub();
608 my ($query, $sth, $ref, @values);
610 # connect to database
611 my $dbh = $form->dbconnect($myconfig);
613 $query = qq|SELECT closedto, revtrans FROM defaults|;
614 ($form->{closedto}, $form->{revtrans}) = selectrow_query($form, $dbh, $query);
616 $query = qq|SELECT id, gldate
618 WHERE id = (SELECT max(id) FROM gl)|;
619 ($form->{previous_id}, $form->{previous_gldate}) = selectrow_query($form, $dbh, $query);
623 qq|SELECT g.reference, g.description, g.notes, g.transdate, g.storno, g.storno_id,
624 d.description AS department, e.name AS employee, g.taxincluded, g.gldate,
625 g.ob_transaction, g.cb_transaction
627 LEFT JOIN department d ON (d.id = g.department_id)
628 LEFT JOIN employee e ON (e.id = g.employee_id)
630 $ref = selectfirst_hashref_query($form, $dbh, $query, conv_i($form->{id}));
631 map { $form->{$_} = $ref->{$_} } keys %$ref;
633 # retrieve individual rows
635 qq|SELECT c.accno, t.taxkey AS accnotaxkey, a.amount, a.memo, a.source,
636 a.transdate, a.cleared, a.project_id, p.projectnumber,
637 a.taxkey, t.rate AS taxrate, t.id,
639 FROM chart c1, tax t1
640 WHERE (t1.id = t.id) AND (c1.id = t.chart_id)) AS taxaccno,
643 WHERE (tk.chart_id = a.chart_id) AND (tk.startdate <= a.transdate)
644 ORDER BY tk.startdate desc LIMIT 1) AS tax_id
646 JOIN chart c ON (c.id = a.chart_id)
647 LEFT JOIN project p ON (p.id = a.project_id)
652 WHERE (tk.taxkey_id = a.taxkey) AND
653 ((CASE WHEN a.chart_id IN
654 (SELECT chart_id FROM taxkeys WHERE taxkey_id = a.taxkey)
655 THEN tk.chart_id = a.chart_id
658 OR (c.link LIKE '%tax%'))
659 AND (startdate <= a.transdate)
660 ORDER BY startdate DESC LIMIT 1))
661 WHERE (a.trans_id = ?)
662 AND (a.fx_transaction = '0')
663 ORDER BY a.acc_trans_id, a.transdate|;
664 $form->{GL} = selectall_hashref_query($form, $dbh, $query, conv_i($form->{id}));
671 WHERE id = (SELECT MAX(id) FROM gl)
674 ($form->{transdate}) = selectrow_query($form, $dbh, $query);
677 # get tax description
678 $query = qq|SELECT * FROM tax ORDER BY taxkey|;
679 $form->{TAX} = selectall_hashref_query($form, $dbh, $query);
681 # get chart of accounts
683 qq|SELECT c.accno, c.description, c.link, tk.taxkey_id, tk.tax_id
685 LEFT JOIN taxkeys tk ON (tk.id =
688 WHERE (taxkeys.chart_id = c.id)
690 ORDER BY startdate DESC
693 $form->{chart} = selectall_hashref_query($form, $dbh, $query, conv_date($form->{transdate}));
697 $main::lxdebug->leave_sub();
701 $main::lxdebug->enter_sub();
703 my ($self, $form, $myconfig, $id) = @_;
705 my ($query, $new_id, $storno_row, $acc_trans_rows);
706 my $dbh = $form->get_standard_dbh($myconfig);
708 $query = qq|SELECT nextval('glid')|;
709 ($new_id) = selectrow_query($form, $dbh, $query);
711 $query = qq|SELECT * FROM gl WHERE id = ?|;
712 $storno_row = selectfirst_hashref_query($form, $dbh, $query, $id);
714 $storno_row->{id} = $new_id;
715 $storno_row->{storno_id} = $id;
716 $storno_row->{storno} = 't';
717 $storno_row->{reference} = 'Storno-' . $storno_row->{reference};
719 delete @$storno_row{qw(itime mtime)};
721 $query = sprintf 'INSERT INTO gl (%s) VALUES (%s)', join(', ', keys %$storno_row), join(', ', map '?', values %$storno_row);
722 do_query($form, $dbh, $query, (values %$storno_row));
724 $query = qq|UPDATE gl SET storno = 't' WHERE id = ?|;
725 do_query($form, $dbh, $query, $id);
727 # now copy acc_trans entries
728 $query = qq|SELECT * FROM acc_trans WHERE trans_id = ?|;
729 my $rowref = selectall_hashref_query($form, $dbh, $query, $id);
731 for my $row (@$rowref) {
732 delete @$row{qw(itime mtime acc_trans_id)};
733 $query = sprintf 'INSERT INTO acc_trans (%s) VALUES (%s)', join(', ', keys %$row), join(', ', map '?', values %$row);
734 $row->{trans_id} = $new_id;
735 $row->{amount} *= -1;
736 do_query($form, $dbh, $query, (values %$row));
741 $main::lxdebug->leave_sub();
744 sub get_chart_balances {
745 $main::lxdebug->enter_sub();
750 Common::check_params(\%params, qw(charts));
752 my $myconfig = \%main::myconfig;
753 my $form = $main::form;
755 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
757 my @ids = map { $_->{id} } @{ $params{charts} };
760 $main::lxdebug->leave_sub();
764 my $query = qq|SELECT chart_id, SUM(amount) AS sum
766 WHERE chart_id IN (| . join(', ', ('?') x scalar(@ids)) . qq|)
769 my %balances = selectall_as_map($form, $dbh, $query, 'chart_id', 'sum', @ids);
771 foreach my $chart (@{ $params{charts} }) {
772 $chart->{balance} = $balances{ $chart->{id} } || 0;
775 $main::lxdebug->leave_sub();