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 #======================================================================
41 sub delete_transaction {
42 my ($self, $myconfig, $form) = @_;
43 $main::lxdebug->enter_sub();
46 my $dbh = $form->dbconnect_noauto($myconfig);
48 my $query = qq|DELETE FROM gl WHERE id = $form->{id}|;
49 $dbh->do($query) || $form->dberror($query);
51 $query = qq|DELETE FROM acc_trans WHERE trans_id = $form->{id}|;
52 $dbh->do($query) || $form->dberror($query);
55 my $rc = $dbh->commit;
57 $main::lxdebug->leave_sub();
63 sub post_transaction {
64 my ($self, $myconfig, $form) = @_;
65 $main::lxdebug->enter_sub();
67 my ($debit, $credit) = (0, 0);
72 # check if debit and credit balances
74 if ($form->{storno}) {
76 $credit = $credit * -1;
78 $form->{reference} = "Storno-" . $form->{reference};
79 $form->{description} = "Storno-" . $form->{description};
82 # connect to database, turn off AutoCommit
83 my $dbh = $form->dbconnect_noauto($myconfig);
85 # post the transaction
86 # make up a unique handle and store in reference field
87 # then retrieve the record based on the unique handle to get the id
88 # replace the reference field with the actual variable
89 # add records to acc_trans
91 # if there is a $form->{id} replace the old transaction
92 # delete all acc_trans entries and add the new ones
95 map { $form->{$_} =~ s/\'/\'\'/g } qw(reference description notes);
97 if (!$form->{taxincluded}) {
98 $form->{taxincluded} = 0;
105 # delete individual transactions
106 $query = qq|DELETE FROM acc_trans
107 WHERE trans_id = $form->{id}|;
108 $dbh->do($query) || $form->dberror($query);
112 $uid .= $form->{login};
114 $query = qq|INSERT INTO gl (reference, employee_id)
115 VALUES ('$uid', (SELECT e.id FROM employee e
116 WHERE e.login = '$form->{login}'))|;
117 $dbh->do($query) || $form->dberror($query);
119 $query = qq|SELECT g.id FROM gl g
120 WHERE g.reference = '$uid'|;
121 $sth = $dbh->prepare($query);
122 $sth->execute || $form->dberror($query);
124 ($form->{id}) = $sth->fetchrow_array;
129 my ($null, $department_id) = split /--/, $form->{department};
132 $query = qq|UPDATE gl SET
133 reference = '$form->{reference}',
134 description = '$form->{description}',
135 notes = '$form->{notes}',
136 transdate = '$form->{transdate}',
137 department_id = $department_id,
138 taxincluded = '$form->{taxincluded}'
139 WHERE id = $form->{id}|;
141 $dbh->do($query) || $form->dberror($query);
142 ($taxkey, $rate) = split(/--/, $form->{taxkey});
144 # insert acc_trans transactions
145 for $i (1 .. $form->{rowcount}) {
148 my ($accno) = split(/--/, $form->{"accno_$i"});
149 my ($taxkey, $rate) = split(/--/, $form->{"taxchart_$i"});
151 my $debit = $form->{"debit_$i"};
152 my $credit = $form->{"credit_$i"};
153 my $tax = $form->{"tax_$i"};
160 $amount = $debit * -1;
165 # if there is an amount, add the record
168 ($form->{"project_id_$i"}) ? $form->{"project_id_$i"} : 'NULL';
169 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate,
170 source, memo, project_id, taxkey)
172 ($form->{id}, (SELECT c.id
174 WHERE c.accno = '$accno'),
175 $amount, '$form->{transdate}', |
176 . $dbh->quote($form->{"source_$i"}) . qq|, |
177 . $dbh->quote($form->{"memo_$i"}) . qq|,
178 $project_id, $taxkey)|;
180 $dbh->do($query) || $form->dberror($query);
189 ($form->{"project_id_$i"}) ? $form->{"project_id_$i"} : 'NULL';
190 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate,
191 source, memo, project_id, taxkey)
193 ($form->{id}, (SELECT t.chart_id
195 WHERE t.taxkey = $taxkey),
196 $amount, '$form->{transdate}', |
197 . $dbh->quote($form->{"source_$i"}) . qq|, |
198 . $dbh->quote($form->{"memo_$i"}) . qq|,
199 $project_id, $taxkey)|;
201 $dbh->do($query) || $form->dberror($query);
205 my %audittrail = (tablename => 'gl',
206 reference => $form->{reference},
207 formname => 'transaction',
211 # $form->audittrail($dbh, "", \%audittrail);
213 # commit and redirect
214 my $rc = $dbh->commit;
216 $main::lxdebug->leave_sub();
222 sub all_transactions {
223 my ($self, $myconfig, $form) = @_;
224 $main::lxdebug->enter_sub();
226 # connect to database
227 my $dbh = $form->dbconnect($myconfig);
228 my ($query, $sth, $source, $null);
230 my ($glwhere, $arwhere, $apwhere) = ("1 = 1", "1 = 1", "1 = 1");
232 if ($form->{reference}) {
233 $source = $form->like(lc $form->{reference});
234 $glwhere .= " AND lower(g.reference) LIKE '$source'";
235 $arwhere .= " AND lower(a.invnumber) LIKE '$source'";
236 $apwhere .= " AND lower(a.invnumber) LIKE '$source'";
238 if ($form->{department}) {
239 ($null, $source) = split /--/, $form->{department};
240 $glwhere .= " AND g.department_id = $source";
241 $arwhere .= " AND a.department_id = $source";
242 $apwhere .= " AND a.department_id = $source";
245 if ($form->{source}) {
246 $source = $form->like(lc $form->{source});
247 $glwhere .= " AND lower(ac.source) LIKE '$source'";
248 $arwhere .= " AND lower(ac.source) LIKE '$source'";
249 $apwhere .= " AND lower(ac.source) LIKE '$source'";
251 if ($form->{datefrom}) {
252 $glwhere .= " AND ac.transdate >= '$form->{datefrom}'";
253 $arwhere .= " AND ac.transdate >= '$form->{datefrom}'";
254 $apwhere .= " AND ac.transdate >= '$form->{datefrom}'";
256 if ($form->{dateto}) {
257 $glwhere .= " AND ac.transdate <= '$form->{dateto}'";
258 $arwhere .= " AND ac.transdate <= '$form->{dateto}'";
259 $apwhere .= " AND ac.transdate <= '$form->{dateto}'";
261 if ($form->{description}) {
262 my $description = $form->like(lc $form->{description});
263 $glwhere .= " AND lower(g.description) LIKE '$description'";
264 $arwhere .= " AND lower(ct.name) LIKE '$description'";
265 $apwhere .= " AND lower(ct.name) LIKE '$description'";
267 if ($form->{notes}) {
268 my $notes = $form->like(lc $form->{notes});
269 $glwhere .= " AND lower(g.notes) LIKE '$notes'";
270 $arwhere .= " AND lower(a.notes) LIKE '$notes'";
271 $apwhere .= " AND lower(a.notes) LIKE '$notes'";
273 if ($form->{accno}) {
274 $glwhere .= " AND c.accno = '$form->{accno}'";
275 $arwhere .= " AND c.accno = '$form->{accno}'";
276 $apwhere .= " AND c.accno = '$form->{accno}'";
278 if ($form->{gifi_accno}) {
279 $glwhere .= " AND c.gifi_accno = '$form->{gifi_accno}'";
280 $arwhere .= " AND c.gifi_accno = '$form->{gifi_accno}'";
281 $apwhere .= " AND c.gifi_accno = '$form->{gifi_accno}'";
283 if ($form->{category} ne 'X') {
284 $glwhere .= " AND c.category = '$form->{category}'";
285 $arwhere .= " AND c.category = '$form->{category}'";
286 $apwhere .= " AND c.category = '$form->{category}'";
289 if ($form->{accno}) {
291 # get category for account
292 $query = qq|SELECT c.category
294 WHERE c.accno = '$form->{accno}'|;
295 $sth = $dbh->prepare($query);
297 $sth->execute || $form->dberror($query);
298 ($form->{ml}) = $sth->fetchrow_array;
301 #if ($form->{datefrom}) {
302 $query = qq|SELECT SUM(ac.amount)
303 FROM acc_trans ac, chart c
304 WHERE ac.chart_id = c.id
305 AND c.accno = '$form->{accno}'|;
306 $query .= qq|AND ac.transdate > date '$form->{datefrom}'
307 | if ($form->{datefrom});;
308 $query .= qq|AND ac.transdate < date '$form->{dateto}'
309 | if ($form->{dateto});
311 $sth = $dbh->prepare($query);
312 $sth->execute || $form->dberror($query);
314 ($form->{balance}) = $sth->fetchrow_array;
319 if ($form->{gifi_accno}) {
321 # get category for account
322 $query = qq|SELECT c.category
324 WHERE c.gifi_accno = '$form->{gifi_accno}'|;
325 $sth = $dbh->prepare($query);
327 $sth->execute || $form->dberror($query);
328 ($form->{ml}) = $sth->fetchrow_array;
331 if ($form->{datefrom}) {
332 $query = qq|SELECT SUM(ac.amount)
333 FROM acc_trans ac, chart c
334 WHERE ac.chart_id = c.id
335 AND c.gifi_accno = '$form->{gifi_accno}'
336 AND ac.transdate < date '$form->{datefrom}'
338 $sth = $dbh->prepare($query);
339 $sth->execute || $form->dberror($query);
341 ($form->{balance}) = $sth->fetchrow_array;
346 my $false = ($myconfig->{dbdriver} eq 'Pg') ? FALSE: q|'0'|;
349 qq|SELECT g.id, 'gl' AS type, $false AS invoice, g.reference, ac.taxkey, t.taxkey AS sorttax,
350 g.description, ac.transdate, ac.source, ac.trans_id,
351 ac.amount*100 AS amount, c.accno, c.gifi_accno, g.notes, t.chart_id, ac.oid
352 FROM gl g, acc_trans ac, chart c LEFT JOIN tax t ON
355 AND ac.chart_id = c.id
356 AND g.id = ac.trans_id
358 SELECT a.id, 'ar' AS type, a.invoice, a.invnumber, ac.taxkey, t.taxkey AS sorttax,
359 ct.name, ac.transdate, ac.source, ac.trans_id,
360 ac.amount*100 AS amount, c.accno, c.gifi_accno, a.notes, t.chart_id, ac.oid
361 FROM ar a, acc_trans ac, customer ct, chart c LEFT JOIN tax t ON
364 AND ac.chart_id = c.id
365 AND a.customer_id = ct.id
366 AND a.id = ac.trans_id
368 SELECT a.id, 'ap' AS type, a.invoice, a.invnumber, ac.taxkey, t.taxkey AS sorttax,
369 ct.name, ac.transdate, ac.source, ac.trans_id,
370 ac.amount*100 AS amount, c.accno, c.gifi_accno, a.notes, t.chart_id, ac.oid
371 FROM ap a, acc_trans ac, vendor ct, chart c LEFT JOIN tax t ON
374 AND ac.chart_id = c.id
375 AND a.vendor_id = ct.id
376 AND a.id = ac.trans_id
377 ORDER BY transdate, trans_id, taxkey DESC, sorttax DESC, oid|;
378 my $sth = $dbh->prepare($query);
379 $sth->execute || $form->dberror($query);
381 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
382 print(STDERR $ref->{id}, " Transaction\n");
385 if ($ref->{type} eq "gl") {
386 $ref->{module} = "gl";
390 if ($ref->{type} eq "ap") {
391 if ($ref->{invoice}) {
392 $ref->{module} = "ir";
394 $ref->{module} = "ap";
399 if ($ref->{type} eq "ar") {
400 if ($ref->{invoice}) {
401 $ref->{module} = "is";
403 $ref->{module} = "ar";
406 $balance = $ref->{amount};
407 $ref->{amount} = $ref->{amount} * 1.0;
412 if ($ref->{amount} < 0) {
413 if ($ref->{chart_id} > 0) {
414 $ref->{debit_tax}{$i} = ($ref->{amount} / 100.0) * -1;
415 $ref->{debit_tax_accno}{$i} = $ref->{accno};
417 $ref->{debit}{$k} = ($ref->{amount} / 100.0) * -1;
418 $ref->{debit_accno}{$k} = $ref->{accno};
419 $ref->{debit_taxkey}{$k} = $ref->{taxkey};
422 if ($ref->{chart_id} > 0) {
423 $ref->{credit_tax}{$j} = $ref->{amount} / 100.0;
424 $ref->{credit_tax_accno}{$j} = $ref->{accno};
426 $ref->{credit}{$l} = $ref->{amount} / 100.0;
427 $ref->{credit_accno}{$l} = $ref->{accno};
428 $ref->{credit_taxkey}{$l} = $ref->{taxkey};
431 # if ($form->{accno} eq ''){ # flo & udo: if general report,
434 # while ($balance != 0) {
435 # my $ref2 = $sth->fetchrow_hashref(NAME_lc)
436 # || $form->error(sprintf("Unbalanced ledger! trans-id: %i", $ref->{trans_id}));
439 # (int($balance * 100000) + int(100000 * $ref2->{amount})) / 100000;
440 # $balance += $ref2->{amount};
442 #print(STDERR $balance, " BAlance\n");
443 #print(STDERR $ref2->{amount}, " Ref2->amount\n");
444 # if ($ref2->{amount} < 0) {
445 # if ($ref2->{chart_id} > 0) {
446 # if ($ref->{debit_tax_accno}{$i} ne "") {
449 # $ref->{debit_tax}{$i} = ($ref2->{amount} / 100.0) * -1;
450 # $ref->{debit_tax_accno}{$i} = $ref2->{accno};
452 # if ($ref->{debit_accno}{$k} ne "") {
455 # $ref->{debit}{$k} = ($ref2->{amount} / 100.0) * -1;
456 # $ref->{debit_accno}{$k} = $ref2->{accno};
457 # $ref->{debit_taxkey}{$k} = $ref2->{taxkey};
460 # if ($ref2->{chart_id} > 0) {
461 # if ($ref->{credit_tax_accno}{$j} ne "") {
464 # $ref->{credit_tax}{$j} = $ref2->{amount} / 100.0;
465 # $ref->{credit_tax_accno}{$j} = $ref2->{accno};
467 # if ($ref->{credit_accno}{$l} ne "") {
470 # $ref->{credit}{$l} = $ref2->{amount} / 100.0;
471 # $ref->{credit_accno}{$l} = $ref2->{accno};
472 # $ref->{credit_taxkey}{$l} = $ref2->{taxkey};
479 # print(STDERR Dumper($ref));
480 push @{ $form->{GL} }, $ref;
485 if ($form->{accno}) {
487 qq|SELECT c.description FROM chart c WHERE c.accno = '$form->{accno}'|;
488 $sth = $dbh->prepare($query);
489 $sth->execute || $form->dberror($query);
491 ($form->{account_description}) = $sth->fetchrow_array;
494 if ($form->{gifi_accno}) {
496 qq|SELECT g.description FROM gifi g WHERE g.accno = '$form->{gifi_accno}'|;
497 $sth = $dbh->prepare($query);
498 $sth->execute || $form->dberror($query);
500 ($form->{gifi_account_description}) = $sth->fetchrow_array;
503 $main::lxdebug->leave_sub();
510 my ($self, $myconfig, $form) = @_;
511 $main::lxdebug->enter_sub();
513 my ($query, $sth, $ref);
515 # connect to database
516 my $dbh = $form->dbconnect($myconfig);
519 $query = "SELECT closedto, revtrans
521 $sth = $dbh->prepare($query);
522 $sth->execute || $form->dberror($query);
524 ($form->{closedto}, $form->{revtrans}) = $sth->fetchrow_array;
527 $query = "SELECT g.reference, g.description, g.notes, g.transdate,
528 d.description AS department, e.name as employee, g.taxincluded, g.gldate
530 LEFT JOIN department d ON (d.id = g.department_id)
531 LEFT JOIN employee e ON (e.id = g.employee_id)
532 WHERE g.id = $form->{id}";
533 $sth = $dbh->prepare($query);
534 $sth->execute || $form->dberror($query);
535 $ref = $sth->fetchrow_hashref(NAME_lc);
536 map { $form->{$_} = $ref->{$_} } keys %$ref;
539 # retrieve individual rows
540 $query = "SELECT c.accno, c.taxkey_id AS accnotaxkey, a.amount, project_id,
541 (SELECT p.projectnumber FROM project p
542 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
543 FROM acc_trans a, chart c
544 WHERE a.chart_id = c.id
545 AND a.trans_id = $form->{id}
547 $sth = $dbh->prepare($query);
548 $sth->execute || $form->dberror($query);
550 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
551 push @{ $form->{GL} }, $ref;
554 # get tax description
555 $query = qq| SELECT * FROM tax t|;
556 $sth = $dbh->prepare($query);
557 $sth->execute || $form->dberror($query);
559 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
560 push @{ $form->{TAX} }, $ref;
565 $query = "SELECT current_date AS transdate, closedto, revtrans
567 $sth = $dbh->prepare($query);
568 $sth->execute || $form->dberror($query);
570 ($form->{transdate}, $form->{closedto}, $form->{revtrans}) =
571 $sth->fetchrow_array;
573 # get tax description
574 $query = qq| SELECT * FROM tax t order by t.taxkey|;
575 $sth = $dbh->prepare($query);
576 $sth->execute || $form->dberror($query);
578 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
579 push @{ $form->{TAX} }, $ref;
585 # get chart of accounts
586 $query = qq|SELECT c.accno, c.description, c.taxkey_id
588 WHERE c.charttype = 'A'
590 $sth = $dbh->prepare($query);
591 $sth->execute || $form->dberror($query);
593 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
594 push @{ $form->{chart} }, $ref;
599 $main::lxdebug->leave_sub();