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();
191 sub all_transactions {
192 my ($self, $myconfig, $form) = @_;
193 $main::lxdebug->enter_sub();
195 # connect to database
196 my $dbh = $form->dbconnect($myconfig);
197 my ($query, $sth, $source, $null);
199 my ($glwhere, $arwhere, $apwhere) = ("1 = 1", "1 = 1", "1 = 1");
200 my (@glvalues, @arvalues, @apvalues);
202 if ($form->{reference}) {
203 $glwhere .= qq| AND g.reference ILIKE ?|;
204 $arwhere .= qq| AND a.invnumber ILIKE ?|;
205 $apwhere .= qq| AND a.invnumber ILIKE ?|;
206 push(@glvalues, '%' . $form->{reference} . '%');
207 push(@arvalues, '%' . $form->{reference} . '%');
208 push(@apvalues, '%' . $form->{reference} . '%');
211 if ($form->{department}) {
212 my ($null, $department) = split /--/, $form->{department};
213 $glwhere .= qq| AND g.department_id = ?|;
214 $arwhere .= qq| AND a.department_id = ?|;
215 $apwhere .= qq| AND a.department_id = ?|;
216 push(@glvalues, $department);
217 push(@arvalues, $department);
218 push(@apvalues, $department);
221 if ($form->{source}) {
222 $glwhere .= " AND ac.source ILIKE ?";
223 $arwhere .= " AND ac.source ILIKE ?";
224 $apwhere .= " AND ac.source ILIKE ?";
225 push(@glvalues, '%' . $form->{source} . '%');
226 push(@arvalues, '%' . $form->{source} . '%');
227 push(@apvalues, '%' . $form->{source} . '%');
230 if ($form->{datefrom}) {
231 $glwhere .= " AND ac.transdate >= ?";
232 $arwhere .= " AND ac.transdate >= ?";
233 $apwhere .= " AND ac.transdate >= ?";
234 push(@glvalues, $form->{datefrom});
235 push(@arvalues, $form->{datefrom});
236 push(@apvalues, $form->{datefrom});
239 if ($form->{dateto}) {
240 $glwhere .= " AND ac.transdate <= ?";
241 $arwhere .= " AND ac.transdate <= ?";
242 $apwhere .= " AND ac.transdate <= ?";
243 push(@glvalues, $form->{dateto});
244 push(@arvalues, $form->{dateto});
245 push(@apvalues, $form->{dateto});
248 if ($form->{description}) {
249 $glwhere .= " AND g.description ILIKE ?";
250 $arwhere .= " AND ct.name ILIKE ?";
251 $apwhere .= " AND ct.name ILIKE ?";
252 push(@glvalues, '%' . $form->{description} . '%');
253 push(@arvalues, '%' . $form->{description} . '%');
254 push(@apvalues, '%' . $form->{description} . '%');
257 if ($form->{notes}) {
258 $glwhere .= " AND g.notes ILIKE ?";
259 $arwhere .= " AND a.notes ILIKE ?";
260 $apwhere .= " AND a.notes ILIKE ?";
261 push(@glvalues, '%' . $form->{notes} . '%');
262 push(@arvalues, '%' . $form->{notes} . '%');
263 push(@apvalues, '%' . $form->{notes} . '%');
266 if ($form->{accno}) {
267 $glwhere .= " AND c.accno = '$form->{accno}'";
268 $arwhere .= " AND c.accno = '$form->{accno}'";
269 $apwhere .= " AND c.accno = '$form->{accno}'";
272 if ($form->{category} ne 'X') {
274 qq| AND gl.id in (SELECT trans_id FROM acc_trans ac2 WHERE ac2.chart_id IN
275 (SELECT id FROM chart c2 WHERE c2.category = ?))|;
277 qq| AND ar.id in (SELECT trans_id FROM acc_trans ac2 WHERE ac2.chart_id IN
278 (SELECT id FROM chart c2 WHERE c2.category = ?))|;
280 qq| AND ap.id in (SELECT trans_id FROM acc_trans ac2 WHERE ac2.chart_id IN
281 (SELECT id FROM chart c2 WHERE c2.category = ?))"|;
282 push(@glvalues, $form->{category});
283 push(@arvalues, $form->{category});
284 push(@apvalues, $form->{category});
287 if ($form->{project_id}) {
288 $glwhere .= qq| AND g.id IN (SELECT DISTINCT trans_id FROM acc_trans WHERE project_id = ?)|;
290 qq| AND ((a.globalproject_id = ?) OR
291 (a.id IN (SELECT DISTINCT trans_id FROM acc_trans WHERE project_id = ?)))|;
293 qq| AND ((a.globalproject_id = ?) OR
294 (a.id IN (SELECT DISTINCT trans_id FROM acc_trans WHERE project_id = ?)))|;
295 my $project_id = conv_i($form->{project_id});
296 push(@glvalues, $project_id);
297 push(@arvalues, $project_id, $project_id);
298 push(@apvalues, $project_id, $project_id);
301 my ($project_columns, %project_join);
302 if ($form->{"l_projectnumbers"}) {
303 $project_columns = qq|, ac.project_id, pr.projectnumber|;
304 $project_join = qq|LEFT JOIN project pr ON (ac.project_id = pr.id)|;
307 if ($form->{accno}) {
308 # get category for account
309 $query = qq|SELECT category FROM chart WHERE accno = ?|;
310 ($form->{ml}) = selectrow_query($form, $dbh, $query, $form->{accno});
312 if ($form->{datefrom}) {
314 qq|SELECT SUM(ac.amount)
316 LEFT JOIN chart c ON (ac.chart_id = c.id)
317 WHERE (c.accno = ?) AND (ac.transdate < ?)|;
318 ($form->{balance}) = selectrow_query($form, $dbh, $query, $form->{accno}, conv_date($form->{datefrom}));
322 my $false = ($myconfig->{dbdriver} eq 'Pg') ? FALSE: q|'0'|;
327 $form->{sort} =~ s/[^a-zA-Z_]//g;
328 $sortorder = $form->{sort} . ",";
333 ac.oid AS acoid, g.id, 'gl' AS type, $false AS invoice, g.reference, ac.taxkey, c.link,
334 g.description, ac.transdate, ac.source, ac.trans_id,
335 ac.amount, c.accno, g.notes, t.chart_id, ac.oid
337 FROM gl g, acc_trans ac $project_join, chart c
338 LEFT JOIN tax t ON (t.chart_id = c.id)
340 AND (ac.chart_id = c.id)
341 AND (g.id = ac.trans_id)
345 SELECT ac.oid AS acoid, a.id, 'ar' AS type, a.invoice, a.invnumber, ac.taxkey, c.link,
346 ct.name, ac.transdate, ac.source, ac.trans_id,
347 ac.amount, c.accno, a.notes, t.chart_id, ac.oid
349 FROM ar a, acc_trans ac $project_join, customer ct, chart c
350 LEFT JOIN tax t ON (t.chart_id=c.id)
352 AND (ac.chart_id = c.id)
353 AND (a.customer_id = ct.id)
354 AND (a.id = ac.trans_id)
358 SELECT ac.oid AS acoid, a.id, 'ap' AS type, a.invoice, a.invnumber, ac.taxkey, c.link,
359 ct.name, ac.transdate, ac.source, ac.trans_id,
360 ac.amount, c.accno, a.notes, t.chart_id, ac.oid
362 FROM ap a, acc_trans ac $project_join, vendor ct, chart c
363 LEFT JOIN tax t ON (t.chart_id=c.id)
365 AND (ac.chart_id = c.id)
366 AND (a.vendor_id = ct.id)
367 AND (a.id = ac.trans_id)
369 ORDER BY $sortorder transdate, trans_id, acoid, taxkey DESC|;
371 my @values = (@glvalues, @arvalues, @apvalues);
373 # Show all $query in Debuglevel LXDebug::QUERY
374 $callingdetails = (caller (0))[3];
375 dump_query(LXDebug::QUERY, "$callingdetails", $query, @values);
377 $sth = prepare_execute_query($form, $dbh, $query, @values);
381 my ($i, $j, $k, $l, $ref, $ref2);
384 while (my $ref0 = $sth->fetchrow_hashref(NAME_lc)) {
386 $trans_id = $ref0->{id};
388 if ($trans_id != $trans_id2) { # first line of a booking
391 push(@{ $form->{GL} }, $ref);
396 $trans_id2 = $ref->{id};
399 if ($ref->{type} eq "gl") {
400 $ref->{module} = "gl";
404 if ($ref->{type} eq "ap") {
405 if ($ref->{invoice}) {
406 $ref->{module} = "ir";
408 $ref->{module} = "ap";
413 if ($ref->{type} eq "ar") {
414 if ($ref->{invoice}) {
415 $ref->{module} = "is";
417 $ref->{module} = "ar";
421 $ref->{"projectnumbers"} = {};
422 $ref->{"projectnumbers"}->{$ref->{"projectnumber"}} = 1 if ($ref->{"projectnumber"});
424 $balance = $ref->{amount};
426 # Linenumbers of General Ledger
427 $k = 0; # Debit # AP # Soll
428 $l = 0; # Credit # AR # Haben
429 $i = 0; # Debit Tax # AP_tax # VSt
430 $j = 0; # Credit Tax # AR_tax # USt
433 if ($ref->{chart_id} > 0) { # all tax accounts first line, no line increasing
434 if ($ref->{amount} < 0) {
435 if ($ref->{link} =~ /AR_tax/) {
436 $ref->{credit_tax}{$j} = $ref->{amount};
437 $ref->{credit_tax_accno}{$j} = $ref->{accno};
439 if ($ref->{link} =~ /AP_tax/) {
440 $ref->{debit_tax}{$i} = $ref->{amount} * -1;
441 $ref->{debit_tax_accno}{$i} = $ref->{accno};
444 if ($ref->{link} =~ /AR_tax/) {
445 $ref->{credit_tax}{$j} = $ref->{amount};
446 $ref->{credit_tax_accno}{$j} = $ref->{accno};
448 if ($ref->{link} =~ /AP_tax/) {
449 $ref->{debit_tax}{$i} = $ref->{amount} * -1;
450 $ref->{debit_tax_accno}{$i} = $ref->{accno};
453 } else { #all other accounts first line
454 if ($ref->{amount} < 0) {
455 $ref->{debit}{$k} = $ref->{amount} * -1;
456 $ref->{debit_accno}{$k} = $ref->{accno};
457 $ref->{debit_taxkey}{$k} = $ref->{taxkey};
458 $ref->{ac_transdate}{$k} = $ref->{transdate};
461 $ref->{credit}{$l} = $ref->{amount} * 1;
462 $ref->{credit_accno}{$l} = $ref->{accno};
463 $ref->{credit_taxkey}{$l} = $ref->{taxkey};
464 $ref->{ac_transdate}{$l} = $ref->{transdate};
470 } else { # following lines of a booking, line increasing
473 $trans_old = $trans_id2;
474 $trans_id2 = $ref2->{id};
477 (int($balance * 100000) + int(100000 * $ref2->{amount})) / 100000;
479 $ref->{"projectnumbers"}->{$ref2->{"projectnumber"}} = 1 if ($ref2->{"projectnumber"});
481 if ($ref2->{chart_id} > 0) { # all tax accounts, following lines
482 if ($ref2->{amount} < 0) {
483 if ($ref2->{link} =~ /AR_tax/) {
484 if ($ref->{credit_tax_accno}{$j} ne "") {
487 $ref->{credit_tax}{$j} = $ref2->{amount};
488 $ref->{credit_tax_accno}{$j} = $ref2->{accno};
490 if ($ref2->{link} =~ /AP_tax/) {
491 if ($ref->{debit_tax_accno}{$i} ne "") {
494 $ref->{debit_tax}{$i} = $ref2->{amount} * -1;
495 $ref->{debit_tax_accno}{$i} = $ref2->{accno};
498 if ($ref2->{link} =~ /AR_tax/) {
499 if ($ref->{credit_tax_accno}{$j} ne "") {
502 $ref->{credit_tax}{$j} = $ref2->{amount};
503 $ref->{credit_tax_accno}{$j} = $ref2->{accno};
505 if ($ref2->{link} =~ /AP_tax/) {
506 if ($ref->{debit_tax_accno}{$i} ne "") {
509 $ref->{debit_tax}{$i} = $ref2->{amount} * -1;
510 $ref->{debit_tax_accno}{$i} = $ref2->{accno};
513 } else { # all other accounts, following lines
514 if ($ref2->{amount} < 0) {
515 if ($ref->{debit_accno}{$k} ne "") {
518 $ref->{debit}{$k} = $ref2->{amount} * - 1;
519 $ref->{debit_accno}{$k} = $ref2->{accno};
520 $ref->{debit_taxkey}{$k} = $ref2->{taxkey};
521 $ref->{ac_transdate}{$k} = $ref2->{transdate};
523 if ($ref->{credit_accno}{$l} ne "") {
526 $ref->{credit}{$l} = $ref2->{amount};
527 $ref->{credit_accno}{$l} = $ref2->{accno};
528 $ref->{credit_taxkey}{$l} = $ref2->{taxkey};
529 $ref->{ac_transdate}{$l} = $ref2->{transdate};
534 push @{ $form->{GL} }, $ref;
537 if ($form->{accno}) {
538 $query = qq|SELECT c.description FROM chart c WHERE c.accno = ?|;
539 ($form->{account_description}) = selectrow_query($form, $dbh, $query, $form->{accno});
544 $main::lxdebug->leave_sub();
548 my ($self, $myconfig, $form) = @_;
549 $main::lxdebug->enter_sub();
551 my ($query, $sth, $ref, @values);
553 # connect to database
554 my $dbh = $form->dbconnect($myconfig);
556 $query = qq|SELECT closedto, revtrans FROM defaults|;
557 ($form->{closedto}, $form->{revtrans}) = selectrow_query($form, $dbh, $query);
561 qq|SELECT g.reference, g.description, g.notes, g.transdate,
562 d.description AS department, e.name AS employee, g.taxincluded, g.gldate
564 LEFT JOIN department d ON (d.id = g.department_id)
565 LEFT JOIN employee e ON (e.id = g.employee_id)
567 $ref = selectfirst_hashref_query($form, $dbh, $query, conv_i($form->{id}));
568 map { $form->{$_} = $ref->{$_} } keys %$ref;
570 # retrieve individual rows
572 qq|SELECT c.accno, t.taxkey AS accnotaxkey, a.amount, a.memo,
573 a.transdate, a.cleared, a.project_id, p.projectnumber,
574 a.taxkey, t.rate AS taxrate, t.id,
576 FROM chart c1, tax t1
577 WHERE (t1.id = t.id) AND (c1.id = t.chart_id)) AS taxaccno,
580 WHERE (tk.chart_id = a.chart_id) AND (tk.startdate <= a.transdate)
581 ORDER BY tk.startdate desc LIMIT 1) AS tax_id
583 JOIN chart c ON (c.id = a.chart_id)
584 LEFT JOIN project p ON (p.id = a.project_id)
589 WHERE (tk.taxkey_id = a.taxkey) AND
590 ((CASE WHEN a.chart_id IN
591 (SELECT chart_id FROM taxkeys WHERE taxkey_id = a.taxkey)
592 THEN tk.chart_id = a.chart_id
595 OR (c.link LIKE '%tax%'))
596 AND (startdate <= a.transdate)
597 ORDER BY startdate DESC LIMIT 1))
598 WHERE (a.trans_id = ?)
599 AND (a.fx_transaction = '0')
600 ORDER BY a.oid, a.transdate|;
601 $form->{GL} = selectall_hashref_query($form, $dbh, $query, conv_i($form->{id}));
608 WHERE id = (SELECT MAX(id) FROM gl)
611 ($form->{transdate}) = selectrow_query($form, $dbh, $query);
614 # get tax description
615 $query = qq|SELECT * FROM tax ORDER BY taxkey|;
616 $form->{TAX} = selectall_hashref_query($form, $dbh, $query);
618 # get chart of accounts
620 qq|SELECT c.accno, c.description, c.link, tk.taxkey_id, tk.tax_id
622 LEFT JOIN taxkeys tk ON (tk.id =
625 WHERE (taxkeys.chart_id = c.id)
627 ORDER BY startdate DESC
630 $form->{chart} = selectall_hashref_query($form, $dbh, $query, conv_date($form->{transdate}));
634 $main::lxdebug->leave_sub();