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 # acc_trans entries are deleted by database triggers.
55 do_query($form, $dbh, qq|DELETE FROM gl WHERE id = ?|, conv_i($form->{id}));
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});
113 $form->{ob_transaction} *= 1;
114 $form->{cb_transaction} *= 1;
118 reference = ?, description = ?, notes = ?,
119 transdate = ?, department_id = ?, taxincluded = ?,
120 storno = ?, storno_id = ?, ob_transaction = ?, cb_transaction = ?
123 @values = ($form->{reference}, $form->{description}, $form->{notes},
124 conv_date($form->{transdate}), conv_i($department_id), $form->{taxincluded} ? 't' : 'f',
125 $form->{storno} ? 't' : 'f', conv_i($form->{storno_id}), $form->{ob_transaction} ? 't' : 'f', $form->{cb_transaction} ? 't' : 'f',
126 conv_i($form->{id}));
127 do_query($form, $dbh, $query, @values);
129 # insert acc_trans transactions
130 for $i (1 .. $form->{rowcount}) {
132 my ($accno) = split(/--/, $form->{"accno_$i"});
133 ($form->{"tax_id_$i"}) = split(/--/, $form->{"taxchart_$i"});
134 if ($form->{"tax_id_$i"} ne "") {
135 $query = qq|SELECT taxkey, rate FROM tax WHERE id = ?|;
136 ($taxkey, $rate) = selectrow_query($form, $dbh, $query, conv_i($form->{"tax_id_$i"}));
140 my $debit = $form->{"debit_$i"};
141 my $credit = $form->{"credit_$i"};
142 my $tax = $form->{"tax_$i"};
149 $amount = $debit * -1;
154 $project_id = conv_i($form->{"project_id_$i"});
156 # if there is an amount, add the record
159 qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate,
160 source, memo, project_id, taxkey, ob_transaction, cb_transaction, tax_id, chart_link)
161 VALUES (?, (SELECT id FROM chart WHERE accno = ?),
162 ?, ?, ?, ?, ?, ?, ?, ?, ?, (SELECT link FROM chart WHERE accno = ?))|;
163 @values = (conv_i($form->{id}), $accno, $amount, conv_date($form->{transdate}),
164 $form->{"source_$i"}, $form->{"memo_$i"}, $project_id, $taxkey, $form->{ob_transaction} ? 't' : 'f', $form->{cb_transaction} ? 't' : 'f', conv_i($form->{"tax_id_$i"}), $accno);
165 do_query($form, $dbh, $query, @values);
171 qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate,
172 source, memo, project_id, taxkey, tax_id, chart_link)
173 VALUES (?, (SELECT chart_id FROM tax WHERE id = ?),
174 ?, ?, ?, ?, ?, ?, ?, (SELECT link
176 WHERE id = (SELECT chart_id
179 @values = (conv_i($form->{id}), conv_i($form->{"tax_id_$i"}),
180 $tax, conv_date($form->{transdate}), $form->{"source_$i"},
181 $form->{"memo_$i"}, $project_id, $taxkey, conv_i($form->{"tax_id_$i"}), conv_i($form->{"tax_id_$i"}));
182 do_query($form, $dbh, $query, @values);
186 if ($form->{storno} && $form->{storno_id}) {
187 do_query($form, $dbh, qq|UPDATE gl SET storno = 't' WHERE id = ?|, conv_i($form->{storno_id}));
190 # safety check datev export
191 if ($::instance_conf->get_datev_check_on_gl_transaction) {
192 my $transdate = $::form->{transdate} ? DateTime->from_lxoffice($::form->{transdate}) : undef;
193 $transdate ||= DateTime->today;
195 my $datev = SL::DATEV->new(
196 exporttype => DATEV_ET_BUCHUNGEN,
197 format => DATEV_FORMAT_KNE,
201 trans_id => $form->{id},
206 if ($datev->errors) {
208 die join "\n", $::locale->text('DATEV check returned errors:'), $datev->errors;
212 # commit and redirect
213 my $rc = $dbh->commit;
215 $main::lxdebug->leave_sub();
220 sub all_transactions {
221 my ($self, $myconfig, $form) = @_;
222 $main::lxdebug->enter_sub();
224 # connect to database
225 my $dbh = $form->dbconnect($myconfig);
226 my ($query, $sth, $source, $null, $space);
228 my ($glwhere, $arwhere, $apwhere) = ("1 = 1", "1 = 1", "1 = 1");
229 my (@glvalues, @arvalues, @apvalues);
231 if ($form->{reference}) {
232 $glwhere .= qq| AND g.reference ILIKE ?|;
233 $arwhere .= qq| AND a.invnumber ILIKE ?|;
234 $apwhere .= qq| AND a.invnumber ILIKE ?|;
235 push(@glvalues, '%' . $form->{reference} . '%');
236 push(@arvalues, '%' . $form->{reference} . '%');
237 push(@apvalues, '%' . $form->{reference} . '%');
240 if ($form->{department}) {
241 my ($null, $department) = split /--/, $form->{department};
242 $glwhere .= qq| AND g.department_id = ?|;
243 $arwhere .= qq| AND a.department_id = ?|;
244 $apwhere .= qq| AND a.department_id = ?|;
245 push(@glvalues, $department);
246 push(@arvalues, $department);
247 push(@apvalues, $department);
250 if ($form->{source}) {
251 $glwhere .= " AND ac.trans_id IN (SELECT trans_id from acc_trans WHERE source ILIKE ?)";
252 $arwhere .= " AND ac.trans_id IN (SELECT trans_id from acc_trans WHERE source ILIKE ?)";
253 $apwhere .= " AND ac.trans_id IN (SELECT trans_id from acc_trans WHERE source ILIKE ?)";
254 push(@glvalues, '%' . $form->{source} . '%');
255 push(@arvalues, '%' . $form->{source} . '%');
256 push(@apvalues, '%' . $form->{source} . '%');
259 # default Datumseinschränkung falls nicht oder falsch Ă¼bergeben (sollte nie passieren)
260 $form->{datesort} = 'transdate' unless $form->{datesort} =~ /^(transdate|gldate)$/;
262 if ($form->{datefrom}) {
263 $glwhere .= " AND ac.$form->{datesort} >= ?";
264 $arwhere .= " AND ac.$form->{datesort} >= ?";
265 $apwhere .= " AND ac.$form->{datesort} >= ?";
266 push(@glvalues, $form->{datefrom});
267 push(@arvalues, $form->{datefrom});
268 push(@apvalues, $form->{datefrom});
271 if ($form->{dateto}) {
272 $glwhere .= " AND ac.$form->{datesort} <= ?";
273 $arwhere .= " AND ac.$form->{datesort} <= ?";
274 $apwhere .= " AND ac.$form->{datesort} <= ?";
275 push(@glvalues, $form->{dateto});
276 push(@arvalues, $form->{dateto});
277 push(@apvalues, $form->{dateto});
280 if ($form->{description}) {
281 $glwhere .= " AND g.description ILIKE ?";
282 $arwhere .= " AND ct.name ILIKE ?";
283 $apwhere .= " AND ct.name ILIKE ?";
284 push(@glvalues, '%' . $form->{description} . '%');
285 push(@arvalues, '%' . $form->{description} . '%');
286 push(@apvalues, '%' . $form->{description} . '%');
289 if ($form->{employee_id}) {
290 $glwhere .= " AND g.employee_id = ? ";
291 $arwhere .= " AND a.employee_id = ? ";
292 $apwhere .= " AND a.employee_id = ? ";
293 push(@glvalues, conv_i($form->{employee_id}));
294 push(@arvalues, conv_i($form->{employee_id}));
295 push(@apvalues, conv_i($form->{employee_id}));
298 if ($form->{notes}) {
299 $glwhere .= " AND g.notes ILIKE ?";
300 $arwhere .= " AND a.notes ILIKE ?";
301 $apwhere .= " AND a.notes ILIKE ?";
302 push(@glvalues, '%' . $form->{notes} . '%');
303 push(@arvalues, '%' . $form->{notes} . '%');
304 push(@apvalues, '%' . $form->{notes} . '%');
307 if ($form->{accno}) {
308 $glwhere .= " AND c.accno = '$form->{accno}'";
309 $arwhere .= " AND c.accno = '$form->{accno}'";
310 $apwhere .= " AND c.accno = '$form->{accno}'";
313 if ($form->{category} ne 'X') {
314 $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 = ?))|;
315 $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 = ?))|;
316 $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 = ?))|;
317 push(@glvalues, $form->{category});
318 push(@arvalues, $form->{category});
319 push(@apvalues, $form->{category});
322 if ($form->{project_id}) {
323 $glwhere .= qq| AND g.id IN (SELECT DISTINCT trans_id FROM acc_trans WHERE project_id = ?)|;
325 qq| AND ((a.globalproject_id = ?) OR
326 (a.id IN (SELECT DISTINCT trans_id FROM acc_trans WHERE project_id = ?)))|;
328 qq| AND ((a.globalproject_id = ?) OR
329 (a.id IN (SELECT DISTINCT trans_id FROM acc_trans WHERE project_id = ?)))|;
330 my $project_id = conv_i($form->{project_id});
331 push(@glvalues, $project_id);
332 push(@arvalues, $project_id, $project_id);
333 push(@apvalues, $project_id, $project_id);
336 my ($project_columns, $project_join);
337 if ($form->{"l_projectnumbers"}) {
338 $project_columns = qq|, ac.project_id, pr.projectnumber|;
339 $project_join = qq|LEFT JOIN project pr ON (ac.project_id = pr.id)|;
342 if ($form->{accno}) {
343 # get category for account
344 $query = qq|SELECT category FROM chart WHERE accno = ?|;
345 ($form->{ml}) = selectrow_query($form, $dbh, $query, $form->{accno});
347 if ($form->{datefrom}) {
349 qq|SELECT SUM(ac.amount)
351 LEFT JOIN chart c ON (ac.chart_id = c.id)
352 WHERE (c.accno = ?) AND (ac.$form->{datesort} < ?)|;
353 ($form->{balance}) = selectrow_query($form, $dbh, $query, $form->{accno}, conv_date($form->{datefrom}));
359 'transdate' => [ qw(transdate id) ],
360 'gldate' => [ qw(gldate id) ],
361 'reference' => [ qw(lower_reference id) ],
362 'description' => [ qw(lower_description id) ],
363 'accno' => [ qw(accno transdate id) ],
365 my %lowered_columns = (
366 'reference' => { 'gl' => 'g.reference', 'arap' => 'a.invnumber', },
367 'source' => { 'gl' => 'ac.source', 'arap' => 'ac.source', },
368 'description' => { 'gl' => 'g.description', 'arap' => 'ct.name', },
371 # sortdir = sort direction (ascending or descending)
372 my $sortdir = !defined $form->{sortdir} ? 'ASC' : $form->{sortdir} ? 'ASC' : 'DESC';
373 my $sortkey = $sort_columns{$form->{sort}} ? $form->{sort} : $form->{datesort}; # default used to be transdate
374 my $sortorder = join ', ', map { "$_ $sortdir" } @{ $sort_columns{$sortkey} };
376 my %columns_for_sorting = ( 'gl' => '', 'arap' => '', );
377 foreach my $spec (@{ $sort_columns{$sortkey} }) {
378 next if ($spec !~ m/^lower_(.*)$/);
381 map { $columns_for_sorting{$_} .= sprintf(', lower(%s) AS lower_%s', $lowered_columns{$column}->{$_}, $column) } qw(gl arap);
386 ac.acc_trans_id, g.id, 'gl' AS type, FALSE AS invoice, g.reference, ac.taxkey, c.link,
387 g.description, ac.transdate, ac.gldate, ac.source, ac.trans_id,
388 ac.amount, c.accno, g.notes, t.chart_id,
389 CASE WHEN (COALESCE(e.name, '') = '') THEN e.login ELSE e.name END AS employee
391 $columns_for_sorting{gl}
393 LEFT JOIN employee e ON (g.employee_id = e.id),
394 acc_trans ac $project_join, chart c
395 LEFT JOIN tax t ON (t.chart_id = c.id)
397 AND (ac.chart_id = c.id)
398 AND (g.id = ac.trans_id)
402 SELECT ac.acc_trans_id, a.id, 'ar' AS type, a.invoice, a.invnumber, ac.taxkey, c.link,
403 ct.name, ac.transdate, ac.gldate, ac.source, ac.trans_id,
404 ac.amount, c.accno, a.notes, t.chart_id,
405 CASE WHEN (COALESCE(e.name, '') = '') THEN e.login ELSE e.name END AS employee
407 $columns_for_sorting{arap}
409 LEFT JOIN employee e ON (a.employee_id = e.id),
410 acc_trans ac $project_join, customer ct, chart c
411 LEFT JOIN tax t ON (t.chart_id=c.id)
413 AND (ac.chart_id = c.id)
414 AND (a.customer_id = ct.id)
415 AND (a.id = ac.trans_id)
419 SELECT ac.acc_trans_id, a.id, 'ap' AS type, a.invoice, a.invnumber, ac.taxkey, c.link,
420 ct.name, ac.transdate, ac.gldate, ac.source, ac.trans_id,
421 ac.amount, c.accno, a.notes, t.chart_id,
422 CASE WHEN (COALESCE(e.name, '') = '') THEN e.login ELSE e.name END AS employee
424 $columns_for_sorting{arap}
426 LEFT JOIN employee e ON (a.employee_id = e.id),
427 acc_trans ac $project_join, vendor ct, chart c
428 LEFT JOIN tax t ON (t.chart_id=c.id)
430 AND (ac.chart_id = c.id)
431 AND (a.vendor_id = ct.id)
432 AND (a.id = ac.trans_id)
434 ORDER BY $sortorder, acc_trans_id $sortdir|;
435 # ORDER BY gldate DESC, id DESC, acc_trans_id DESC
437 my @values = (@glvalues, @arvalues, @apvalues);
439 # Show all $query in Debuglevel LXDebug::QUERY
440 my $callingdetails = (caller (0))[3];
441 dump_query(LXDebug->QUERY(), "$callingdetails", $query, @values);
443 $sth = prepare_execute_query($form, $dbh, $query, @values);
448 my ($i, $j, $k, $l, $ref, $ref2);
451 while (my $ref0 = $sth->fetchrow_hashref("NAME_lc")) {
453 $trans_id = $ref0->{id};
455 my $source = $ref0->{source};
456 undef($ref0->{source});
458 if ($trans_id != $trans_id2) { # first line of a booking
461 push(@{ $form->{GL} }, $ref);
466 $trans_id2 = $ref->{id};
469 if ($ref->{type} eq "gl") {
470 $ref->{module} = "gl";
474 if ($ref->{type} eq "ap") {
475 if ($ref->{invoice}) {
476 $ref->{module} = "ir";
478 $ref->{module} = "ap";
483 if ($ref->{type} eq "ar") {
484 if ($ref->{invoice}) {
485 $ref->{module} = "is";
487 $ref->{module} = "ar";
491 $ref->{"projectnumbers"} = {};
492 $ref->{"projectnumbers"}->{$ref->{"projectnumber"}} = 1 if ($ref->{"projectnumber"});
494 $balance = $ref->{amount};
496 # Linenumbers of General Ledger
497 $k = 0; # Debit # AP # Soll
498 $l = 0; # Credit # AR # Haben
499 $i = 0; # Debit Tax # AP_tax # VSt
500 $j = 0; # Credit Tax # AR_tax # USt
502 if ($ref->{chart_id} > 0) { # all tax accounts first line, no line increasing
503 if ($ref->{amount} < 0) {
504 if ($ref->{link} =~ /AR_tax/) {
505 $ref->{credit_tax}{$j} = $ref->{amount};
506 $ref->{credit_tax_accno}{$j} = $ref->{accno};
508 if ($ref->{link} =~ /AP_tax/) {
509 $ref->{debit_tax}{$i} = $ref->{amount} * -1;
510 $ref->{debit_tax_accno}{$i} = $ref->{accno};
513 if ($ref->{link} =~ /AR_tax/) {
514 $ref->{credit_tax}{$j} = $ref->{amount};
515 $ref->{credit_tax_accno}{$j} = $ref->{accno};
517 if ($ref->{link} =~ /AP_tax/) {
518 $ref->{debit_tax}{$i} = $ref->{amount} * -1;
519 $ref->{debit_tax_accno}{$i} = $ref->{accno};
522 } else { #all other accounts first line
524 if ($ref->{amount} < 0) {
525 $ref->{debit}{$k} = $ref->{amount} * -1;
526 $ref->{debit_accno}{$k} = $ref->{accno};
527 $ref->{debit_taxkey}{$k} = $ref->{taxkey};
528 $ref->{ac_transdate}{$k} = $ref->{transdate};
529 $ref->{source}{$k} = $source;
531 $ref->{credit}{$l} = $ref->{amount} * 1;
532 $ref->{credit_accno}{$l} = $ref->{accno};
533 $ref->{credit_taxkey}{$l} = $ref->{taxkey};
534 $ref->{ac_transdate}{$l} = $ref->{transdate};
535 $ref->{source}{$l} = $source;
539 } else { # following lines of a booking, line increasing
542 # $trans_old = $trans_id2; # doesn't seem to be used anymore
543 $trans_id2 = $ref2->{id};
546 (int($balance * 100000) + int(100000 * $ref2->{amount})) / 100000;
548 $ref->{"projectnumbers"}->{$ref2->{"projectnumber"}} = 1 if ($ref2->{"projectnumber"});
550 if ($ref2->{chart_id} > 0) { # all tax accounts, following lines
551 if ($ref2->{amount} < 0) {
552 if ($ref2->{link} =~ /AR_tax/) {
553 if ($ref->{credit_tax_accno}{$j} ne "") {
556 $ref->{credit_tax}{$j} = $ref2->{amount};
557 $ref->{credit_tax_accno}{$j} = $ref2->{accno};
559 if ($ref2->{link} =~ /AP_tax/) {
560 if ($ref->{debit_tax_accno}{$i} ne "") {
563 $ref->{debit_tax}{$i} = $ref2->{amount} * -1;
564 $ref->{debit_tax_accno}{$i} = $ref2->{accno};
567 if ($ref2->{link} =~ /AR_tax/) {
568 if ($ref->{credit_tax_accno}{$j} ne "") {
571 $ref->{credit_tax}{$j} = $ref2->{amount};
572 $ref->{credit_tax_accno}{$j} = $ref2->{accno};
574 if ($ref2->{link} =~ /AP_tax/) {
575 if ($ref->{debit_tax_accno}{$i} ne "") {
578 $ref->{debit_tax}{$i} = $ref2->{amount} * -1;
579 $ref->{debit_tax_accno}{$i} = $ref2->{accno};
582 } else { # all other accounts, following lines
583 if ($ref2->{amount} < 0) {
584 if ($ref->{debit_accno}{$k} ne "") {
587 if ($ref->{source}{$k} ne "") {
592 $ref->{debit}{$k} = $ref2->{amount} * - 1;
593 $ref->{debit_accno}{$k} = $ref2->{accno};
594 $ref->{debit_taxkey}{$k} = $ref2->{taxkey};
595 $ref->{ac_transdate}{$k} = $ref2->{transdate};
596 $ref->{source}{$k} = $source . $space . $ref->{source}{$k};
598 if ($ref->{credit_accno}{$l} ne "") {
601 if ($ref->{source}{$l} ne "") {
606 $ref->{credit}{$l} = $ref2->{amount};
607 $ref->{credit_accno}{$l} = $ref2->{accno};
608 $ref->{credit_taxkey}{$l} = $ref2->{taxkey};
609 $ref->{ac_transdate}{$l} = $ref2->{transdate};
610 $ref->{source}{$l} = $ref->{source}{$l} . $space . $source;
616 push @{ $form->{GL} }, $ref;
619 if ($form->{accno}) {
620 $query = qq|SELECT c.description FROM chart c WHERE c.accno = ?|;
621 ($form->{account_description}) = selectrow_query($form, $dbh, $query, $form->{accno});
626 $main::lxdebug->leave_sub();
630 my ($self, $myconfig, $form) = @_;
631 $main::lxdebug->enter_sub();
633 my ($query, $sth, $ref, @values);
635 # connect to database
636 my $dbh = $form->dbconnect($myconfig);
638 $query = qq|SELECT closedto, revtrans FROM defaults|;
639 ($form->{closedto}, $form->{revtrans}) = selectrow_query($form, $dbh, $query);
641 $query = qq|SELECT id, gldate
643 WHERE id = (SELECT max(id) FROM gl)|;
644 ($form->{previous_id}, $form->{previous_gldate}) = selectrow_query($form, $dbh, $query);
648 qq|SELECT g.reference, g.description, g.notes, g.transdate, g.storno, g.storno_id,
649 d.description AS department, e.name AS employee, g.taxincluded, g.gldate,
650 g.ob_transaction, g.cb_transaction
652 LEFT JOIN department d ON (d.id = g.department_id)
653 LEFT JOIN employee e ON (e.id = g.employee_id)
655 $ref = selectfirst_hashref_query($form, $dbh, $query, conv_i($form->{id}));
656 map { $form->{$_} = $ref->{$_} } keys %$ref;
658 # retrieve individual rows
660 qq|SELECT c.accno, t.taxkey AS accnotaxkey, a.amount, a.memo, a.source,
661 a.transdate, a.cleared, a.project_id, p.projectnumber,
662 a.taxkey, t.rate AS taxrate, t.id,
664 FROM chart c1, tax t1
665 WHERE (t1.id = t.id) AND (c1.id = t.chart_id)) AS taxaccno,
668 WHERE (tk.chart_id = a.chart_id) AND (tk.startdate <= a.transdate)
669 ORDER BY tk.startdate desc LIMIT 1) AS tax_id
671 JOIN chart c ON (c.id = a.chart_id)
672 LEFT JOIN project p ON (p.id = a.project_id)
673 LEFT JOIN tax t ON (t.id = a.tax_id)
674 WHERE (a.trans_id = ?)
675 AND (a.fx_transaction = '0')
676 ORDER BY a.acc_trans_id, a.transdate|;
677 $form->{GL} = selectall_hashref_query($form, $dbh, $query, conv_i($form->{id}));
684 WHERE id = (SELECT MAX(id) FROM gl)
687 ($form->{transdate}) = selectrow_query($form, $dbh, $query);
690 # get tax description
691 $query = qq|SELECT * FROM tax ORDER BY taxkey|;
692 $form->{TAX} = selectall_hashref_query($form, $dbh, $query);
694 # get chart of accounts
696 qq|SELECT c.accno, c.description, c.link, tk.taxkey_id, tk.tax_id
698 LEFT JOIN taxkeys tk ON (tk.id =
701 WHERE (taxkeys.chart_id = c.id)
703 ORDER BY startdate DESC
706 $form->{chart} = selectall_hashref_query($form, $dbh, $query, conv_date($form->{transdate}));
710 $main::lxdebug->leave_sub();
714 $main::lxdebug->enter_sub();
716 my ($self, $form, $myconfig, $id) = @_;
718 my ($query, $new_id, $storno_row, $acc_trans_rows);
719 my $dbh = $form->get_standard_dbh($myconfig);
721 $query = qq|SELECT nextval('glid')|;
722 ($new_id) = selectrow_query($form, $dbh, $query);
724 $query = qq|SELECT * FROM gl WHERE id = ?|;
725 $storno_row = selectfirst_hashref_query($form, $dbh, $query, $id);
727 $storno_row->{id} = $new_id;
728 $storno_row->{storno_id} = $id;
729 $storno_row->{storno} = 't';
730 $storno_row->{reference} = 'Storno-' . $storno_row->{reference};
732 delete @$storno_row{qw(itime mtime gldate)};
734 $query = sprintf 'INSERT INTO gl (%s) VALUES (%s)', join(', ', keys %$storno_row), join(', ', map '?', values %$storno_row);
735 do_query($form, $dbh, $query, (values %$storno_row));
737 $query = qq|UPDATE gl SET storno = 't' WHERE id = ?|;
738 do_query($form, $dbh, $query, $id);
740 # now copy acc_trans entries
741 $query = qq|SELECT * FROM acc_trans WHERE trans_id = ?|;
742 my $rowref = selectall_hashref_query($form, $dbh, $query, $id);
744 for my $row (@$rowref) {
745 delete @$row{qw(itime mtime acc_trans_id gldate)};
746 $query = sprintf 'INSERT INTO acc_trans (%s) VALUES (%s)', join(', ', keys %$row), join(', ', map '?', values %$row);
747 $row->{trans_id} = $new_id;
748 $row->{amount} *= -1;
749 do_query($form, $dbh, $query, (values %$row));
754 $main::lxdebug->leave_sub();
757 sub get_chart_balances {
758 $main::lxdebug->enter_sub();
763 Common::check_params(\%params, qw(charts));
765 my $myconfig = \%main::myconfig;
766 my $form = $main::form;
768 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
770 my @ids = map { $_->{id} } @{ $params{charts} };
773 $main::lxdebug->leave_sub();
777 my $query = qq|SELECT chart_id, SUM(amount) AS sum
779 WHERE chart_id IN (| . join(', ', ('?') x scalar(@ids)) . qq|)
782 my %balances = selectall_as_map($form, $dbh, $query, 'chart_id', 'sum', @ids);
784 foreach my $chart (@{ $params{charts} }) {
785 $chart->{balance} = $balances{ $chart->{id} } || 0;
788 $main::lxdebug->leave_sub();
791 sub get_tax_dropdown {
792 my ($self, $accno) = @_;
794 my $myconfig = \%main::myconfig;
795 my $form = $main::form;
797 my $dbh = $form->get_standard_dbh($myconfig);
799 my $query = qq|SELECT category FROM chart WHERE accno = ?|;
800 my ($category) = selectrow_query($form, $dbh, $query, $accno);
802 $query = qq|SELECT * FROM tax WHERE chart_categories like '%$category%' order by taxkey, rate|;
804 my $sth = prepare_execute_query($form, $dbh, $query);
806 my @tax_accounts = ();
807 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
808 push(@tax_accounts, $ref);
811 return @tax_accounts;