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 # check if debit and credit balances
75 if ($form->{storno}) {
76 $form->{reference} = "Storno-" . $form->{reference};
77 $form->{description} = "Storno-" . $form->{description};
80 # connect to database, turn off AutoCommit
81 my $dbh = $form->dbconnect_noauto($myconfig);
83 # post the transaction
84 # make up a unique handle and store in reference field
85 # then retrieve the record based on the unique handle to get the id
86 # replace the reference field with the actual variable
87 # add records to acc_trans
89 # if there is a $form->{id} replace the old transaction
90 # delete all acc_trans entries and add the new ones
92 if (!$form->{taxincluded}) {
93 $form->{taxincluded} = 0;
100 # delete individual transactions
101 $query = qq|DELETE FROM acc_trans WHERE trans_id = ?|;
102 @values = (conv_i($form->{id}));
103 do_query($form, $dbh, $query, @values);
106 $query = qq|SELECT nextval('glid')|;
107 ($form->{id}) = selectrow_query($form, $dbh, $query);
110 qq|INSERT INTO gl (id, employee_id) | .
111 qq|VALUES (?, (SELECT id FROM employee WHERE login = ?))|;
112 @values = ($form->{id}, $form->{login});
113 do_query($form, $dbh, $query, @values);
116 my ($null, $department_id) = split(/--/, $form->{department});
121 reference = ?, description = ?, notes = ?,
122 transdate = ?, department_id = ?, taxincluded = ?
125 @values = ($form->{reference}, $form->{description}, $form->{notes},
126 conv_date($form->{transdate}), $department_id, $form->{taxincluded},
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)
162 VALUES (?, (SELECT id FROM chart WHERE accno = ?),
164 @values = (conv_i($form->{id}), $accno, $amount, conv_date($form->{transdate}),
165 $form->{"source_$i"}, $form->{"memo_$i"}, $project_id, $taxkey);
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 # commit and redirect
184 my $rc = $dbh->commit;
186 $main::lxdebug->leave_sub();
192 sub all_transactions {
193 my ($self, $myconfig, $form) = @_;
194 $main::lxdebug->enter_sub();
196 # connect to database
197 my $dbh = $form->dbconnect($myconfig);
198 my ($query, $sth, $source, $null);
200 my ($glwhere, $arwhere, $apwhere) = ("1 = 1", "1 = 1", "1 = 1");
201 my (@glvalues, @arvalues, @apvalues);
203 if ($form->{reference}) {
204 $glwhere .= qq| AND g.reference ILIKE ?|;
205 $arwhere .= qq| AND a.invnumber ILIKE ?|;
206 $apwhere .= qq| AND a.invnumber ILIKE ?|;
207 push(@glvalues, '%' . $form->{reference} . '%');
208 push(@arvalues, '%' . $form->{reference} . '%');
209 push(@apvalues, '%' . $form->{reference} . '%');
212 if ($form->{department}) {
213 my ($null, $department) = split /--/, $form->{department};
214 $glwhere .= qq| AND g.department_id = ?|;
215 $arwhere .= qq| AND a.department_id = ?|;
216 $apwhere .= qq| AND a.department_id = ?|;
217 push(@glvalues, $department);
218 push(@arvalues, $department);
219 push(@apvalues, $department);
222 if ($form->{source}) {
223 $glwhere .= " AND ac.source ILIKE ?";
224 $arwhere .= " AND ac.source ILIKE ?";
225 $apwhere .= " AND ac.source ILIKE ?";
226 push(@glvalues, '%' . $form->{source} . '%');
227 push(@arvalues, '%' . $form->{source} . '%');
228 push(@apvalues, '%' . $form->{source} . '%');
231 if ($form->{datefrom}) {
232 $glwhere .= " AND ac.transdate >= ?";
233 $arwhere .= " AND ac.transdate >= ?";
234 $apwhere .= " AND ac.transdate >= ?";
235 push(@glvalues, $form->{datefrom});
236 push(@arvalues, $form->{datefrom});
237 push(@apvalues, $form->{datefrom});
240 if ($form->{dateto}) {
241 $glwhere .= " AND ac.transdate <= ?";
242 $arwhere .= " AND ac.transdate <= ?";
243 $apwhere .= " AND ac.transdate <= ?";
244 push(@glvalues, $form->{dateto});
245 push(@arvalues, $form->{dateto});
246 push(@apvalues, $form->{dateto});
249 if ($form->{description}) {
250 $glwhere .= " AND g.description ILIKE ?";
251 $arwhere .= " AND ct.name ILIKE ?";
252 $apwhere .= " AND ct.name ILIKE ?";
253 push(@glvalues, '%' . $form->{description} . '%');
254 push(@arvalues, '%' . $form->{description} . '%');
255 push(@apvalues, '%' . $form->{description} . '%');
258 if ($form->{notes}) {
259 $glwhere .= " AND g.notes ILIKE ?";
260 $arwhere .= " AND a.notes ILIKE ?";
261 $apwhere .= " AND a.notes ILIKE ?";
262 push(@glvalues, '%' . $form->{notes} . '%');
263 push(@arvalues, '%' . $form->{notes} . '%');
264 push(@apvalues, '%' . $form->{notes} . '%');
267 if ($form->{accno}) {
268 $glwhere .= " AND c.accno = '$form->{accno}'";
269 $arwhere .= " AND c.accno = '$form->{accno}'";
270 $apwhere .= " AND c.accno = '$form->{accno}'";
273 if ($form->{category} ne 'X') {
275 qq| AND gl.id in (SELECT trans_id FROM acc_trans ac2 WHERE ac2.chart_id IN
276 (SELECT id FROM chart c2 WHERE c2.category = ?))|;
278 qq| AND ar.id in (SELECT trans_id FROM acc_trans ac2 WHERE ac2.chart_id IN
279 (SELECT id FROM chart c2 WHERE c2.category = ?))|;
281 qq| AND ap.id in (SELECT trans_id FROM acc_trans ac2 WHERE ac2.chart_id IN
282 (SELECT id FROM chart c2 WHERE c2.category = ?))"|;
283 push(@glvalues, $form->{category});
284 push(@arvalues, $form->{category});
285 push(@apvalues, $form->{category});
288 if ($form->{project_id}) {
289 $glwhere .= qq| AND g.id IN (SELECT DISTINCT trans_id FROM acc_trans WHERE project_id = ?)|;
291 qq| AND ((a.globalproject_id = ?) OR
292 (a.id IN (SELECT DISTINCT trans_id FROM acc_trans WHERE project_id = ?)))|;
294 qq| AND ((a.globalproject_id = ?) OR
295 (a.id IN (SELECT DISTINCT trans_id FROM acc_trans WHERE project_id = ?)))|;
296 my $project_id = conv_i($form->{project_id});
297 push(@glvalues, $project_id);
298 push(@arvalues, $project_id, $project_id);
299 push(@apvalues, $project_id, $project_id);
302 my ($project_columns, %project_join);
303 if ($form->{"l_projectnumbers"}) {
304 $project_columns = qq|, ac.project_id, pr.projectnumber|;
305 $project_join = qq|LEFT JOIN project pr ON (ac.project_id = pr.id)|;
308 if ($form->{accno}) {
309 # get category for account
310 $query = qq|SELECT category FROM chart WHERE accno = ?|;
311 ($form->{ml}) = selectrow_query($form, $dbh, $query, $form->{accno});
313 if ($form->{datefrom}) {
315 qq|SELECT SUM(ac.amount)
317 LEFT JOIN chart c ON (ac.chart_id = c.id)
318 WHERE (c.accno = ?) AND (ac.transdate < ?)|;
319 ($form->{balance}) = selectrow_query($form, $dbh, $query, $form->{accno}, conv_date($form->{datefrom}));
323 my $false = ($myconfig->{dbdriver} eq 'Pg') ? FALSE: q|'0'|;
328 $form->{sort} =~ s/[^a-zA-Z_]//g;
329 $sortorder = $form->{sort} . ",";
334 ac.oid AS acoid, g.id, 'gl' AS type, $false AS invoice, g.reference, ac.taxkey, c.link,
335 g.description, ac.transdate, ac.source, ac.trans_id,
336 ac.amount, c.accno, g.notes, t.chart_id, ac.oid
338 FROM gl g, acc_trans ac $project_join, chart c
339 LEFT JOIN tax t ON (t.chart_id = c.id)
341 AND (ac.chart_id = c.id)
342 AND (g.id = ac.trans_id)
346 SELECT ac.oid AS acoid, a.id, 'ar' AS type, a.invoice, a.invnumber, ac.taxkey, c.link,
347 ct.name, ac.transdate, ac.source, ac.trans_id,
348 ac.amount, c.accno, a.notes, t.chart_id, ac.oid
350 FROM ar a, acc_trans ac $project_join, customer ct, chart c
351 LEFT JOIN tax t ON (t.chart_id=c.id)
353 AND (ac.chart_id = c.id)
354 AND (a.customer_id = ct.id)
355 AND (a.id = ac.trans_id)
359 SELECT ac.oid AS acoid, a.id, 'ap' AS type, a.invoice, a.invnumber, ac.taxkey, c.link,
360 ct.name, ac.transdate, ac.source, ac.trans_id,
361 ac.amount, c.accno, a.notes, t.chart_id, ac.oid
363 FROM ap a, acc_trans ac $project_join, vendor ct, chart c
364 LEFT JOIN tax t ON (t.chart_id=c.id)
366 AND (ac.chart_id = c.id)
367 AND (a.vendor_id = ct.id)
368 AND (a.id = ac.trans_id)
370 ORDER BY $sortorder transdate, trans_id, acoid, taxkey DESC|;
372 my @values = (@glvalues, @arvalues, @apvalues);
374 # Show all $query in Debuglevel LXDebug::QUERY
375 $callingdetails = (caller (0))[3];
376 dump_query(LXDebug::QUERY, "$callingdetails", $query, @values);
378 $sth = prepare_execute_query($form, $dbh, $query, @values);
383 while (my $ref0 = $sth->fetchrow_hashref(NAME_lc)) {
385 $trans_id = $ref0->{id};
387 if ($trans_id != $trans_id2) { # first line of a booking
390 push(@{ $form->{GL} }, $ref);
395 $trans_id2 = $ref->{id};
398 if ($ref->{type} eq "gl") {
399 $ref->{module} = "gl";
403 if ($ref->{type} eq "ap") {
404 if ($ref->{invoice}) {
405 $ref->{module} = "ir";
407 $ref->{module} = "ap";
412 if ($ref->{type} eq "ar") {
413 if ($ref->{invoice}) {
414 $ref->{module} = "is";
416 $ref->{module} = "ar";
420 $ref->{"projectnumbers"} = {};
421 $ref->{"projectnumbers"}->{$ref->{"projectnumber"}} = 1 if ($ref->{"projectnumber"});
423 $balance = $ref->{amount};
425 # Linenumbers of General Ledger
426 $k = 0; # Debit # AP # Soll
427 $l = 0; # Credit # AR # Haben
428 $i = 0; # Debit Tax # AP_tax # VSt
429 $j = 0; # Credit Tax # AR_tax # USt
432 if ($ref->{chart_id} > 0) { # all tax accounts first line, no line increasing
433 if ($ref->{amount} < 0) {
434 if ($ref->{link} =~ /AR_tax/) {
435 $ref->{credit_tax}{$j} = $ref->{amount};
436 $ref->{credit_tax_accno}{$j} = $ref->{accno};
438 if ($ref->{link} =~ /AP_tax/) {
439 $ref->{debit_tax}{$i} = $ref->{amount} * -1;
440 $ref->{debit_tax_accno}{$i} = $ref->{accno};
443 if ($ref->{link} =~ /AR_tax/) {
444 $ref->{credit_tax}{$j} = $ref->{amount};
445 $ref->{credit_tax_accno}{$j} = $ref->{accno};
447 if ($ref->{link} =~ /AP_tax/) {
448 $ref->{debit_tax}{$i} = $ref->{amount} * -1;
449 $ref->{debit_tax_accno}{$i} = $ref->{accno};
452 } else { #all other accounts first line
453 if ($ref->{amount} < 0) {
454 $ref->{debit}{$k} = $ref->{amount} * -1;
455 $ref->{debit_accno}{$k} = $ref->{accno};
456 $ref->{debit_taxkey}{$k} = $ref->{taxkey};
457 $ref->{ac_transdate}{$k} = $ref->{transdate};
460 $ref->{credit}{$l} = $ref->{amount} * 1;
461 $ref->{credit_accno}{$l} = $ref->{accno};
462 $ref->{credit_taxkey}{$l} = $ref->{taxkey};
463 $ref->{ac_transdate}{$l} = $ref->{transdate};
469 } else { # following lines of a booking, line increasing
472 $trans_old = $trans_id2;
473 $trans_id2 = $ref2->{id};
476 (int($balance * 100000) + int(100000 * $ref2->{amount})) / 100000;
478 $ref->{"projectnumbers"}->{$ref2->{"projectnumber"}} = 1 if ($ref2->{"projectnumber"});
480 if ($ref2->{chart_id} > 0) { # all tax accounts, following lines
481 if ($ref2->{amount} < 0) {
482 if ($ref2->{link} =~ /AR_tax/) {
483 if ($ref->{credit_tax_accno}{$j} ne "") {
486 $ref->{credit_tax}{$j} = $ref2->{amount};
487 $ref->{credit_tax_accno}{$j} = $ref2->{accno};
489 if ($ref2->{link} =~ /AP_tax/) {
490 if ($ref->{debit_tax_accno}{$i} ne "") {
493 $ref->{debit_tax}{$i} = $ref2->{amount} * -1;
494 $ref->{debit_tax_accno}{$i} = $ref2->{accno};
497 if ($ref2->{link} =~ /AR_tax/) {
498 if ($ref->{credit_tax_accno}{$j} ne "") {
501 $ref->{credit_tax}{$j} = $ref2->{amount};
502 $ref->{credit_tax_accno}{$j} = $ref2->{accno};
504 if ($ref2->{link} =~ /AP_tax/) {
505 if ($ref->{debit_tax_accno}{$i} ne "") {
508 $ref->{debit_tax}{$i} = $ref2->{amount} * -1;
509 $ref->{debit_tax_accno}{$i} = $ref2->{accno};
512 } else { # all other accounts, following lines
513 if ($ref2->{amount} < 0) {
514 if ($ref->{debit_accno}{$k} ne "") {
517 $ref->{debit}{$k} = $ref2->{amount} * - 1;
518 $ref->{debit_accno}{$k} = $ref2->{accno};
519 $ref->{debit_taxkey}{$k} = $ref2->{taxkey};
520 $ref->{ac_transdate}{$k} = $ref2->{transdate};
522 if ($ref->{credit_accno}{$l} ne "") {
525 $ref->{credit}{$l} = $ref2->{amount};
526 $ref->{credit_accno}{$l} = $ref2->{accno};
527 $ref->{credit_taxkey}{$l} = $ref2->{taxkey};
528 $ref->{ac_transdate}{$l} = $ref2->{transdate};
533 push @{ $form->{GL} }, $ref;
536 if ($form->{accno}) {
537 $query = qq|SELECT c.description FROM chart c WHERE c.accno = ?|;
538 ($form->{account_description}) = selectrow_query($form, $dbh, $query, $form->{accno});
543 $main::lxdebug->leave_sub();
547 my ($self, $myconfig, $form) = @_;
548 $main::lxdebug->enter_sub();
550 my ($query, $sth, $ref, @values);
552 # connect to database
553 my $dbh = $form->dbconnect($myconfig);
555 $query = qq|SELECT closedto, revtrans FROM defaults|;
556 ($form->{closedto}, $form->{revtrans}) = selectrow_query($form, $dbh, $query);
560 qq|SELECT g.reference, g.description, g.notes, g.transdate,
561 d.description AS department, e.name AS employee, g.taxincluded, g.gldate
563 LEFT JOIN department d ON (d.id = g.department_id)
564 LEFT JOIN employee e ON (e.id = g.employee_id)
566 $ref = selectfirst_hashref_query($form, $dbh, $query, conv_i($form->{id}));
567 map { $form->{$_} = $ref->{$_} } keys %$ref;
569 # retrieve individual rows
571 qq|SELECT c.accno, t.taxkey AS accnotaxkey, a.amount, a.memo,
572 a.transdate, a.cleared, a.project_id, p.projectnumber,
573 a.taxkey, t.rate AS taxrate, t.id,
575 FROM chart c1, tax t1
576 WHERE (t1.id = t.id) AND (c1.id = t.chart_id)) AS taxaccno,
579 WHERE (tk.chart_id = a.chart_id) AND (tk.startdate <= a.transdate)
580 ORDER BY tk.startdate desc LIMIT 1) AS tax_id
582 JOIN chart c ON (c.id = a.chart_id)
583 LEFT JOIN project p ON (p.id = a.project_id)
588 WHERE (tk.taxkey_id = a.taxkey) AND
589 ((CASE WHEN a.chart_id IN
590 (SELECT chart_id FROM taxkeys WHERE taxkey_id = a.taxkey)
591 THEN tk.chart_id = a.chart_id
594 OR (c.link LIKE '%tax%'))
595 AND (startdate <= a.transdate)
596 ORDER BY startdate DESC LIMIT 1))
597 WHERE (a.trans_id = ?)
598 AND (a.fx_transaction = '0')
599 ORDER BY a.oid, a.transdate|;
600 $form->{GL} = selectall_hashref_query($form, $dbh, $query, conv_i($form->{id}));
607 WHERE id = (SELECT MAX(id) FROM gl)
610 ($form->{transdate}) = selectrow_query($form, $dbh, $query);
613 # get tax description
614 $query = qq|SELECT * FROM tax ORDER BY taxkey|;
615 $form->{TAX} = selectall_hashref_query($form, $dbh, $query);
617 # get chart of accounts
619 qq|SELECT c.accno, c.description, c.link, tk.taxkey_id, tk.tax_id
621 LEFT JOIN taxkeys tk ON (tk.id =
624 WHERE (taxkeys.chart_id = c.id)
626 ORDER BY startdate DESC
629 $form->{chart} = selectall_hashref_query($form, $dbh, $query, conv_date($form->{transdate}));
633 $main::lxdebug->leave_sub();