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});
114 reference = ?, description = ?, notes = ?,
115 transdate = ?, department_id = ?, taxincluded = ?,
116 storno = ?, storno_id = ?
119 @values = ($form->{reference}, $form->{description}, $form->{notes},
120 conv_date($form->{transdate}), $department_id, $form->{taxincluded},
121 $form->{storno} ? 't' : 'f', conv_i($form->{storno_id}),
122 conv_i($form->{id}));
123 do_query($form, $dbh, $query, @values);
125 # insert acc_trans transactions
126 for $i (1 .. $form->{rowcount}) {
128 my ($accno) = split(/--/, $form->{"accno_$i"});
129 ($form->{"tax_id_$i"}) = split(/--/, $form->{"taxchart_$i"});
130 if ($form->{"tax_id_$i"} ne "") {
131 $query = qq|SELECT taxkey, rate FROM tax WHERE id = ?|;
132 ($taxkey, $rate) = selectrow_query($form, $dbh, $query, conv_i($form->{"tax_id_$i"}));
136 my $debit = $form->{"debit_$i"};
137 my $credit = $form->{"credit_$i"};
138 my $tax = $form->{"tax_$i"};
145 $amount = $debit * -1;
150 $project_id = conv_i($form->{"project_id_$i"});
152 # if there is an amount, add the record
155 qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate,
156 source, memo, project_id, taxkey)
157 VALUES (?, (SELECT id FROM chart WHERE accno = ?),
159 @values = (conv_i($form->{id}), $accno, $amount, conv_date($form->{transdate}),
160 $form->{"source_$i"}, $form->{"memo_$i"}, $project_id, $taxkey);
161 do_query($form, $dbh, $query, @values);
167 qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate,
168 source, memo, project_id, taxkey)
169 VALUES (?, (SELECT chart_id FROM tax WHERE id = ?),
171 @values = (conv_i($form->{id}), conv_i($form->{"tax_id_$i"}),
172 $tax, conv_date($form->{transdate}), $form->{"source_$i"},
173 $form->{"memo_$i"}, $project_id, $taxkey);
174 do_query($form, $dbh, $query, @values);
178 if ($form->{storno} && $form->{storno_id}) {
179 do_query($form, $dbh, qq|UPDATE gl SET storno = 't' WHERE id = ?|, conv_i($form->{storno_id}));
182 # commit and redirect
183 my $rc = $dbh->commit;
185 $main::lxdebug->leave_sub();
190 sub all_transactions {
191 my ($self, $myconfig, $form) = @_;
192 $main::lxdebug->enter_sub();
194 # connect to database
195 my $dbh = $form->dbconnect($myconfig);
196 my ($query, $sth, $source, $null);
198 my ($glwhere, $arwhere, $apwhere) = ("1 = 1", "1 = 1", "1 = 1");
199 my (@glvalues, @arvalues, @apvalues);
201 if ($form->{reference}) {
202 $glwhere .= qq| AND g.reference ILIKE ?|;
203 $arwhere .= qq| AND a.invnumber ILIKE ?|;
204 $apwhere .= qq| AND a.invnumber ILIKE ?|;
205 push(@glvalues, '%' . $form->{reference} . '%');
206 push(@arvalues, '%' . $form->{reference} . '%');
207 push(@apvalues, '%' . $form->{reference} . '%');
210 if ($form->{department}) {
211 my ($null, $department) = split /--/, $form->{department};
212 $glwhere .= qq| AND g.department_id = ?|;
213 $arwhere .= qq| AND a.department_id = ?|;
214 $apwhere .= qq| AND a.department_id = ?|;
215 push(@glvalues, $department);
216 push(@arvalues, $department);
217 push(@apvalues, $department);
220 if ($form->{source}) {
221 $glwhere .= " AND ac.source ILIKE ?";
222 $arwhere .= " AND ac.source ILIKE ?";
223 $apwhere .= " AND ac.source ILIKE ?";
224 push(@glvalues, '%' . $form->{source} . '%');
225 push(@arvalues, '%' . $form->{source} . '%');
226 push(@apvalues, '%' . $form->{source} . '%');
229 if ($form->{datefrom}) {
230 $glwhere .= " AND ac.transdate >= ?";
231 $arwhere .= " AND ac.transdate >= ?";
232 $apwhere .= " AND ac.transdate >= ?";
233 push(@glvalues, $form->{datefrom});
234 push(@arvalues, $form->{datefrom});
235 push(@apvalues, $form->{datefrom});
238 if ($form->{dateto}) {
239 $glwhere .= " AND ac.transdate <= ?";
240 $arwhere .= " AND ac.transdate <= ?";
241 $apwhere .= " AND ac.transdate <= ?";
242 push(@glvalues, $form->{dateto});
243 push(@arvalues, $form->{dateto});
244 push(@apvalues, $form->{dateto});
247 if ($form->{description}) {
248 $glwhere .= " AND g.description ILIKE ?";
249 $arwhere .= " AND ct.name ILIKE ?";
250 $apwhere .= " AND ct.name ILIKE ?";
251 push(@glvalues, '%' . $form->{description} . '%');
252 push(@arvalues, '%' . $form->{description} . '%');
253 push(@apvalues, '%' . $form->{description} . '%');
256 if ($form->{notes}) {
257 $glwhere .= " AND g.notes ILIKE ?";
258 $arwhere .= " AND a.notes ILIKE ?";
259 $apwhere .= " AND a.notes ILIKE ?";
260 push(@glvalues, '%' . $form->{notes} . '%');
261 push(@arvalues, '%' . $form->{notes} . '%');
262 push(@apvalues, '%' . $form->{notes} . '%');
265 if ($form->{accno}) {
266 $glwhere .= " AND c.accno = '$form->{accno}'";
267 $arwhere .= " AND c.accno = '$form->{accno}'";
268 $apwhere .= " AND c.accno = '$form->{accno}'";
271 if ($form->{category} ne 'X') {
273 qq| AND gl.id in (SELECT trans_id FROM acc_trans ac2 WHERE ac2.chart_id IN
274 (SELECT id FROM chart c2 WHERE c2.category = ?))|;
276 qq| AND ar.id in (SELECT trans_id FROM acc_trans ac2 WHERE ac2.chart_id IN
277 (SELECT id FROM chart c2 WHERE c2.category = ?))|;
279 qq| AND ap.id in (SELECT trans_id FROM acc_trans ac2 WHERE ac2.chart_id IN
280 (SELECT id FROM chart c2 WHERE c2.category = ?))|;
281 push(@glvalues, $form->{category});
282 push(@arvalues, $form->{category});
283 push(@apvalues, $form->{category});
286 if ($form->{project_id}) {
287 $glwhere .= qq| AND g.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 = ?)))|;
292 qq| AND ((a.globalproject_id = ?) OR
293 (a.id IN (SELECT DISTINCT trans_id FROM acc_trans WHERE project_id = ?)))|;
294 my $project_id = conv_i($form->{project_id});
295 push(@glvalues, $project_id);
296 push(@arvalues, $project_id, $project_id);
297 push(@apvalues, $project_id, $project_id);
300 my ($project_columns, %project_join);
301 if ($form->{"l_projectnumbers"}) {
302 $project_columns = qq|, ac.project_id, pr.projectnumber|;
303 $project_join = qq|LEFT JOIN project pr ON (ac.project_id = pr.id)|;
306 if ($form->{accno}) {
307 # get category for account
308 $query = qq|SELECT category FROM chart WHERE accno = ?|;
309 ($form->{ml}) = selectrow_query($form, $dbh, $query, $form->{accno});
311 if ($form->{datefrom}) {
313 qq|SELECT SUM(ac.amount)
315 LEFT JOIN chart c ON (ac.chart_id = c.id)
316 WHERE (c.accno = ?) AND (ac.transdate < ?)|;
317 ($form->{balance}) = selectrow_query($form, $dbh, $query, $form->{accno}, conv_date($form->{datefrom}));
321 my $false = ($myconfig->{dbdriver} eq 'Pg') ? FALSE: q|'0'|;
326 $form->{sort} =~ s/[^a-zA-Z_]//g;
327 $sortorder = $form->{sort} . ",";
332 ac.oid AS acoid, g.id, 'gl' AS type, $false AS invoice, g.reference, ac.taxkey, c.link,
333 g.description, ac.transdate, ac.source, ac.trans_id,
334 ac.amount, c.accno, g.notes, t.chart_id, ac.oid
336 FROM gl g, acc_trans ac $project_join, chart c
337 LEFT JOIN tax t ON (t.chart_id = c.id)
339 AND (ac.chart_id = c.id)
340 AND (g.id = ac.trans_id)
344 SELECT ac.oid AS acoid, a.id, 'ar' AS type, a.invoice, a.invnumber, ac.taxkey, c.link,
345 ct.name, ac.transdate, ac.source, ac.trans_id,
346 ac.amount, c.accno, a.notes, t.chart_id, ac.oid
348 FROM ar a, acc_trans ac $project_join, customer ct, chart c
349 LEFT JOIN tax t ON (t.chart_id=c.id)
351 AND (ac.chart_id = c.id)
352 AND (a.customer_id = ct.id)
353 AND (a.id = ac.trans_id)
357 SELECT ac.oid AS acoid, a.id, 'ap' AS type, a.invoice, a.invnumber, ac.taxkey, c.link,
358 ct.name, ac.transdate, ac.source, ac.trans_id,
359 ac.amount, c.accno, a.notes, t.chart_id, ac.oid
361 FROM ap a, acc_trans ac $project_join, vendor ct, chart c
362 LEFT JOIN tax t ON (t.chart_id=c.id)
364 AND (ac.chart_id = c.id)
365 AND (a.vendor_id = ct.id)
366 AND (a.id = ac.trans_id)
368 ORDER BY $sortorder transdate, trans_id, acoid, taxkey DESC|;
370 my @values = (@glvalues, @arvalues, @apvalues);
372 # Show all $query in Debuglevel LXDebug::QUERY
373 $callingdetails = (caller (0))[3];
374 dump_query(LXDebug::QUERY, "$callingdetails", $query, @values);
376 $sth = prepare_execute_query($form, $dbh, $query, @values);
380 my ($i, $j, $k, $l, $ref, $ref2);
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);
558 $query = qq|SELECT id, gldate
560 WHERE id = (SELECT max(id) FROM gl)|;
561 ($form->{previous_id}, $form->{previous_gldate}) = selectrow_query($form, $dbh, $query);
565 qq|SELECT g.reference, g.description, g.notes, g.transdate, g.storno, g.storno_id,
566 d.description AS department, e.name AS employee, g.taxincluded, g.gldate
568 LEFT JOIN department d ON (d.id = g.department_id)
569 LEFT JOIN employee e ON (e.id = g.employee_id)
571 $ref = selectfirst_hashref_query($form, $dbh, $query, conv_i($form->{id}));
572 map { $form->{$_} = $ref->{$_} } keys %$ref;
574 # retrieve individual rows
576 qq|SELECT c.accno, t.taxkey AS accnotaxkey, a.amount, a.memo,
577 a.transdate, a.cleared, a.project_id, p.projectnumber,
578 a.taxkey, t.rate AS taxrate, t.id,
580 FROM chart c1, tax t1
581 WHERE (t1.id = t.id) AND (c1.id = t.chart_id)) AS taxaccno,
584 WHERE (tk.chart_id = a.chart_id) AND (tk.startdate <= a.transdate)
585 ORDER BY tk.startdate desc LIMIT 1) AS tax_id
587 JOIN chart c ON (c.id = a.chart_id)
588 LEFT JOIN project p ON (p.id = a.project_id)
593 WHERE (tk.taxkey_id = a.taxkey) AND
594 ((CASE WHEN a.chart_id IN
595 (SELECT chart_id FROM taxkeys WHERE taxkey_id = a.taxkey)
596 THEN tk.chart_id = a.chart_id
599 OR (c.link LIKE '%tax%'))
600 AND (startdate <= a.transdate)
601 ORDER BY startdate DESC LIMIT 1))
602 WHERE (a.trans_id = ?)
603 AND (a.fx_transaction = '0')
604 ORDER BY a.oid, a.transdate|;
605 $form->{GL} = selectall_hashref_query($form, $dbh, $query, conv_i($form->{id}));
612 WHERE id = (SELECT MAX(id) FROM gl)
615 ($form->{transdate}) = selectrow_query($form, $dbh, $query);
618 # get tax description
619 $query = qq|SELECT * FROM tax ORDER BY taxkey|;
620 $form->{TAX} = selectall_hashref_query($form, $dbh, $query);
622 # get chart of accounts
624 qq|SELECT c.accno, c.description, c.link, tk.taxkey_id, tk.tax_id
626 LEFT JOIN taxkeys tk ON (tk.id =
629 WHERE (taxkeys.chart_id = c.id)
631 ORDER BY startdate DESC
634 $form->{chart} = selectall_hashref_query($form, $dbh, $query, conv_date($form->{transdate}));
638 $main::lxdebug->leave_sub();