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 #======================================================================
43 sub delete_transaction {
44 my ($self, $myconfig, $form) = @_;
45 $main::lxdebug->enter_sub();
48 my $dbh = $form->dbconnect_noauto($myconfig);
50 my $query = qq|DELETE FROM gl WHERE id = $form->{id}|;
51 $dbh->do($query) || $form->dberror($query);
53 $query = qq|DELETE FROM acc_trans WHERE trans_id = $form->{id}|;
54 $dbh->do($query) || $form->dberror($query);
57 my $rc = $dbh->commit;
59 $main::lxdebug->leave_sub();
65 sub post_transaction {
66 my ($self, $myconfig, $form) = @_;
67 $main::lxdebug->enter_sub();
69 my ($debit, $credit) = (0, 0);
74 # check if debit and credit balances
76 if ($form->{storno}) {
78 $credit = $credit * -1;
80 $form->{reference} = "Storno-" . $form->{reference};
81 $form->{description} = "Storno-" . $form->{description};
84 # connect to database, turn off AutoCommit
85 my $dbh = $form->dbconnect_noauto($myconfig);
87 # post the transaction
88 # make up a unique handle and store in reference field
89 # then retrieve the record based on the unique handle to get the id
90 # replace the reference field with the actual variable
91 # add records to acc_trans
93 # if there is a $form->{id} replace the old transaction
94 # delete all acc_trans entries and add the new ones
97 map { $form->{$_} =~ s/\'/\'\'/g } qw(reference description notes);
99 if (!$form->{taxincluded}) {
100 $form->{taxincluded} = 0;
107 # delete individual transactions
108 $query = qq|DELETE FROM acc_trans
109 WHERE trans_id = $form->{id}|;
110 $dbh->do($query) || $form->dberror($query);
114 $uid .= $form->{login};
116 $query = qq|INSERT INTO gl (reference, employee_id)
117 VALUES ('$uid', (SELECT e.id FROM employee e
118 WHERE e.login = '$form->{login}'))|;
119 $dbh->do($query) || $form->dberror($query);
121 $query = qq|SELECT g.id FROM gl g
122 WHERE g.reference = '$uid'|;
123 $sth = $dbh->prepare($query);
124 $sth->execute || $form->dberror($query);
126 ($form->{id}) = $sth->fetchrow_array;
131 my ($null, $department_id) = split /--/, $form->{department};
134 $query = qq|UPDATE gl SET
135 reference = '$form->{reference}',
136 description = '$form->{description}',
137 notes = '$form->{notes}',
138 transdate = '$form->{transdate}',
139 department_id = $department_id,
140 taxincluded = '$form->{taxincluded}'
141 WHERE id = $form->{id}|;
143 $dbh->do($query) || $form->dberror($query);
144 ($taxkey, $rate) = split(/--/, $form->{taxkey});
146 # insert acc_trans transactions
147 for $i (1 .. $form->{rowcount}) {
150 my ($accno) = split(/--/, $form->{"accno_$i"});
151 my ($taxkey, $rate) = split(/--/, $form->{"taxchart_$i"});
153 my $debit = $form->{"debit_$i"};
154 my $credit = $form->{"credit_$i"};
155 my $tax = $form->{"tax_$i"};
162 $amount = $debit * -1;
167 # if there is an amount, add the record
170 ($form->{"project_id_$i"}) ? $form->{"project_id_$i"} : 'NULL';
171 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate,
172 source, memo, project_id, taxkey)
174 ($form->{id}, (SELECT c.id
176 WHERE c.accno = '$accno'),
177 $amount, '$form->{transdate}', |
178 . $dbh->quote($form->{"source_$i"}) . qq|, |
179 . $dbh->quote($form->{"memo_$i"}) . qq|,
180 $project_id, $taxkey)|;
182 $dbh->do($query) || $form->dberror($query);
191 ($form->{"project_id_$i"}) ? $form->{"project_id_$i"} : 'NULL';
192 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate,
193 source, memo, project_id, taxkey)
195 ($form->{id}, (SELECT t.chart_id
197 WHERE t.taxkey = $taxkey),
198 $amount, '$form->{transdate}', |
199 . $dbh->quote($form->{"source_$i"}) . qq|, |
200 . $dbh->quote($form->{"memo_$i"}) . qq|,
201 $project_id, $taxkey)|;
203 $dbh->do($query) || $form->dberror($query);
207 my %audittrail = (tablename => 'gl',
208 reference => $form->{reference},
209 formname => 'transaction',
213 # $form->audittrail($dbh, "", \%audittrail);
215 # commit and redirect
216 my $rc = $dbh->commit;
218 $main::lxdebug->leave_sub();
224 sub all_transactions {
225 my ($self, $myconfig, $form) = @_;
226 $main::lxdebug->enter_sub();
228 # connect to database
229 my $dbh = $form->dbconnect($myconfig);
230 my ($query, $sth, $source, $null);
232 my ($glwhere, $arwhere, $apwhere) = ("1 = 1", "1 = 1", "1 = 1");
234 if ($form->{reference}) {
235 $source = $form->like(lc $form->{reference});
236 $glwhere .= " AND lower(g.reference) LIKE '$source'";
237 $arwhere .= " AND lower(a.invnumber) LIKE '$source'";
238 $apwhere .= " AND lower(a.invnumber) LIKE '$source'";
240 if ($form->{department}) {
241 ($null, $source) = split /--/, $form->{department};
242 $glwhere .= " AND g.department_id = $source";
243 $arwhere .= " AND a.department_id = $source";
244 $apwhere .= " AND a.department_id = $source";
247 if ($form->{source}) {
248 $source = $form->like(lc $form->{source});
249 $glwhere .= " AND lower(ac.source) LIKE '$source'";
250 $arwhere .= " AND lower(ac.source) LIKE '$source'";
251 $apwhere .= " AND lower(ac.source) LIKE '$source'";
253 if ($form->{datefrom}) {
254 $glwhere .= " AND ac.transdate >= '$form->{datefrom}'";
255 $arwhere .= " AND ac.transdate >= '$form->{datefrom}'";
256 $apwhere .= " AND ac.transdate >= '$form->{datefrom}'";
258 if ($form->{dateto}) {
259 $glwhere .= " AND ac.transdate <= '$form->{dateto}'";
260 $arwhere .= " AND ac.transdate <= '$form->{dateto}'";
261 $apwhere .= " AND ac.transdate <= '$form->{dateto}'";
263 if ($form->{description}) {
264 my $description = $form->like(lc $form->{description});
265 $glwhere .= " AND lower(g.description) LIKE '$description'";
266 $arwhere .= " AND lower(ct.name) LIKE '$description'";
267 $apwhere .= " AND lower(ct.name) LIKE '$description'";
269 if ($form->{notes}) {
270 my $notes = $form->like(lc $form->{notes});
271 $glwhere .= " AND lower(g.notes) LIKE '$notes'";
272 $arwhere .= " AND lower(a.notes) LIKE '$notes'";
273 $apwhere .= " AND lower(a.notes) LIKE '$notes'";
275 if ($form->{accno}) {
276 $glwhere .= " AND c.accno = '$form->{accno}'";
277 $arwhere .= " AND c.accno = '$form->{accno}'";
278 $apwhere .= " AND c.accno = '$form->{accno}'";
280 if ($form->{gifi_accno}) {
281 $glwhere .= " AND c.gifi_accno = '$form->{gifi_accno}'";
282 $arwhere .= " AND c.gifi_accno = '$form->{gifi_accno}'";
283 $apwhere .= " AND c.gifi_accno = '$form->{gifi_accno}'";
285 if ($form->{category} ne 'X') {
286 $glwhere .= " AND gl.id in (SELECT trans_id FROM acc_trans ac2 WHERE ac2.chart_id IN (SELECT id FROM chart c2 WHERE c2.category = '$form->{category}'))";
287 $arwhere .= " AND ar.id in (SELECT trans_id FROM acc_trans ac2 WHERE ac2.chart_id IN (SELECT id FROM chart c2 WHERE c2.category = '$form->{category}'))";
288 $apwhere .= " AND ap.id in (SELECT trans_id FROM acc_trans ac2 WHERE ac2.chart_id IN (SELECT id FROM chart c2 WHERE c2.category = '$form->{category}'))";
291 if ($form->{accno}) {
293 # get category for account
294 $query = qq|SELECT c.category
296 WHERE c.accno = '$form->{accno}'|;
297 $sth = $dbh->prepare($query);
299 $sth->execute || $form->dberror($query);
300 ($form->{ml}) = $sth->fetchrow_array;
303 if ($form->{datefrom}) {
304 $query = qq|SELECT SUM(ac.amount)
305 FROM acc_trans ac, chart c
306 WHERE ac.chart_id = c.id
307 AND c.accno = '$form->{accno}'
308 AND ac.transdate < date '$form->{datefrom}'
310 $sth = $dbh->prepare($query);
311 $sth->execute || $form->dberror($query);
313 ($form->{balance}) = $sth->fetchrow_array;
318 if ($form->{gifi_accno}) {
320 # get category for account
321 $query = qq|SELECT c.category
323 WHERE c.gifi_accno = '$form->{gifi_accno}'|;
324 $sth = $dbh->prepare($query);
326 $sth->execute || $form->dberror($query);
327 ($form->{ml}) = $sth->fetchrow_array;
330 if ($form->{datefrom}) {
331 $query = qq|SELECT SUM(ac.amount)
332 FROM acc_trans ac, chart c
333 WHERE ac.chart_id = c.id
334 AND c.gifi_accno = '$form->{gifi_accno}'
335 AND ac.transdate < date '$form->{datefrom}'
337 $sth = $dbh->prepare($query);
338 $sth->execute || $form->dberror($query);
340 ($form->{balance}) = $sth->fetchrow_array;
345 my $false = ($myconfig->{dbdriver} eq 'Pg') ? FALSE: q|'0'|;
347 my $sortorder = join ', ', $form->sort_columns(qw(transdate reference source description accno));
348 my %ordinal = ( transdate => 6,
352 map { $sortorder =~ s/$_/$ordinal{$_}/ } keys %ordinal;
355 $sortorder = $form->{sort} . ",";
361 qq|SELECT g.id, 'gl' AS type, $false AS invoice, g.reference, ac.taxkey, t.taxkey AS sorttax,
362 g.description, ac.transdate, ac.source, ac.trans_id,
363 ac.amount, c.accno, c.gifi_accno, g.notes, t.chart_id, ac.oid
364 FROM gl g, acc_trans ac, chart c LEFT JOIN tax t ON
367 AND ac.chart_id = c.id
368 AND g.id = ac.trans_id
370 SELECT a.id, 'ar' AS type, a.invoice, a.invnumber, ac.taxkey, t.taxkey AS sorttax,
371 ct.name, ac.transdate, ac.source, ac.trans_id,
372 ac.amount, c.accno, c.gifi_accno, a.notes, t.chart_id, ac.oid
373 FROM ar a, acc_trans ac, customer ct, chart c LEFT JOIN tax t ON
376 AND ac.chart_id = c.id
377 AND a.customer_id = ct.id
378 AND a.id = ac.trans_id
380 SELECT a.id, 'ap' AS type, a.invoice, a.invnumber, ac.taxkey, t.taxkey AS sorttax,
381 ct.name, ac.transdate, ac.source, ac.trans_id,
382 ac.amount, c.accno, c.gifi_accno, a.notes, t.chart_id, ac.oid
383 FROM ap a, acc_trans ac, vendor ct, chart c LEFT JOIN tax t ON
386 AND ac.chart_id = c.id
387 AND a.vendor_id = ct.id
388 AND a.id = ac.trans_id
389 ORDER BY $sortorder transdate, trans_id, taxkey DESC, sorttax DESC,oid|;
390 my $sth = $dbh->prepare($query);
391 $sth->execute || $form->dberror($query);
394 while (my $ref0 = $sth->fetchrow_hashref(NAME_lc)) {
395 $trans_id = $ref0->{id};
396 if ($trans_id != $trans_id2) {
398 push @{ $form->{GL} }, $ref;
402 $trans_id2 = $ref->{id};
405 if ($ref->{type} eq "gl") {
406 $ref->{module} = "gl";
409 if ($ref->{type} eq "ap") {
410 if ($ref->{invoice}) {
411 $ref->{module} = "ir";
413 $ref->{module} = "ap";
418 if ($ref->{type} eq "ar") {
419 if ($ref->{invoice}) {
420 $ref->{module} = "is";
422 $ref->{module} = "ar";
425 $balance = $ref->{amount};
430 if ($ref->{amount} < 0) {
431 if ($ref->{chart_id} > 0) {
432 $ref->{debit_tax}{$i} = $ref->{amount} * -1;
433 $ref->{debit_tax_accno}{$i} = $ref->{accno};
435 $ref->{debit}{$k} = $ref->{amount} * -1;
436 $ref->{debit_accno}{$k} = $ref->{accno};
437 $ref->{debit_taxkey}{$k} = $ref->{taxkey};
440 if ($ref->{chart_id} > 0) {
441 $ref->{credit_tax}{$j} = $ref->{amount};
442 $ref->{credit_tax_accno}{$j} = $ref->{accno};
444 $ref->{credit}{$l} = $ref->{amount};
445 $ref->{credit_accno}{$l} = $ref->{accno};
446 $ref->{credit_taxkey}{$l} = $ref->{taxkey};
451 $trans_id2 = $ref2->{id};
452 # if ($form->{accno} eq ''){ # flo & udo: if general report,
454 # while (abs($balance) >= 0.015) {
455 # my $ref2 = $sth->fetchrow_hashref(NAME_lc)
456 # || $form->error("Unbalanced ledger!");
459 (int($balance * 100000) + int(100000 * $ref2->{amount})) / 100000;
460 if ($ref2->{amount} < 0) {
461 if ($ref2->{chart_id} > 0) {
462 if ($ref->{debit_tax_accno}{$i} ne "") {
465 $ref->{debit_tax}{$i} = $ref2->{amount} * -1;
466 $ref->{debit_tax_accno}{$i} = $ref2->{accno};
468 if ($ref->{debit_accno}{$k} ne "") {
471 $ref->{debit}{$k} = $ref2->{amount} * -1;
472 $ref->{debit_accno}{$k} = $ref2->{accno};
473 $ref->{debit_taxkey}{$k} = $ref2->{taxkey};
476 if ($ref2->{chart_id} > 0) {
477 if ($ref->{credit_tax_accno}{$j} ne "") {
480 $ref->{credit_tax}{$j} = $ref2->{amount};
481 $ref->{credit_tax_accno}{$j} = $ref2->{accno};
483 if ($ref->{credit_accno}{$l} ne "") {
486 $ref->{credit}{$l} = $ref2->{amount};
487 $ref->{credit_accno}{$l} = $ref2->{accno};
488 $ref->{credit_taxkey}{$l} = $ref2->{taxkey};
493 # # if account-report, then calculate the Balance?!
494 # # ToDo: Calculate the Balance
499 # print(STDERR Dumper($ref));
502 push @{ $form->{GL} }, $ref;
505 if ($form->{accno}) {
507 qq|SELECT c.description FROM chart c WHERE c.accno = '$form->{accno}'|;
508 $sth = $dbh->prepare($query);
509 $sth->execute || $form->dberror($query);
511 ($form->{account_description}) = $sth->fetchrow_array;
514 if ($form->{gifi_accno}) {
516 qq|SELECT g.description FROM gifi g WHERE g.accno = '$form->{gifi_accno}'|;
517 $sth = $dbh->prepare($query);
518 $sth->execute || $form->dberror($query);
520 ($form->{gifi_account_description}) = $sth->fetchrow_array;
523 print(STDERR Dumper($form->{GL}));
524 $main::lxdebug->leave_sub();
531 my ($self, $myconfig, $form) = @_;
532 $main::lxdebug->enter_sub();
534 my ($query, $sth, $ref);
536 # connect to database
537 my $dbh = $form->dbconnect($myconfig);
540 $query = "SELECT closedto, revtrans
542 $sth = $dbh->prepare($query);
543 $sth->execute || $form->dberror($query);
545 ($form->{closedto}, $form->{revtrans}) = $sth->fetchrow_array;
548 $query = "SELECT g.reference, g.description, g.notes, g.transdate,
549 d.description AS department, e.name as employee, g.taxincluded, g.gldate
551 LEFT JOIN department d ON (d.id = g.department_id)
552 LEFT JOIN employee e ON (e.id = g.employee_id)
553 WHERE g.id = $form->{id}";
554 $sth = $dbh->prepare($query);
555 $sth->execute || $form->dberror($query);
556 $ref = $sth->fetchrow_hashref(NAME_lc);
557 map { $form->{$_} = $ref->{$_} } keys %$ref;
560 # retrieve individual rows
561 $query = "SELECT c.accno, c.taxkey_id AS accnotaxkey, a.amount, project_id,
562 (SELECT p.projectnumber FROM project p
563 WHERE a.project_id = p.id) AS projectnumber, a.taxkey, (SELECT c1.accno FROM chart c1, tax t WHERE t.taxkey=a.taxkey AND c1.id=t.chart_id) AS taxaccno, (SELECT t1.rate FROM tax t1 WHERE t1.taxkey=a.taxkey) AS taxrate
564 FROM acc_trans a, chart c
565 WHERE a.chart_id = c.id
566 AND a.trans_id = $form->{id}
568 $sth = $dbh->prepare($query);
569 $sth->execute || $form->dberror($query);
571 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
572 push @{ $form->{GL} }, $ref;
575 # get tax description
576 $query = qq| SELECT * FROM tax t order by t.taxkey|;
577 $sth = $dbh->prepare($query);
578 $sth->execute || $form->dberror($query);
580 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
581 push @{ $form->{TAX} }, $ref;
586 $query = "SELECT current_date AS transdate, closedto, revtrans
588 $sth = $dbh->prepare($query);
589 $sth->execute || $form->dberror($query);
591 ($form->{transdate}, $form->{closedto}, $form->{revtrans}) =
592 $sth->fetchrow_array;
594 # get tax description
595 $query = qq| SELECT * FROM tax t order by t.taxkey|;
596 $sth = $dbh->prepare($query);
597 $sth->execute || $form->dberror($query);
599 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
600 push @{ $form->{TAX} }, $ref;
606 # get chart of accounts
607 $query = qq|SELECT c.accno, c.description, c.taxkey_id
609 WHERE c.charttype = 'A'
611 $sth = $dbh->prepare($query);
612 $sth->execute || $form->dberror($query);
614 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
615 push @{ $form->{chart} }, $ref;
620 $main::lxdebug->leave_sub();