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) = @_;
45 my $dbh = $form->dbconnect_noauto($myconfig);
47 my $query = qq|DELETE FROM gl WHERE id = $form->{id}|;
48 $dbh->do($query) || $form->dberror($query);
50 $query = qq|DELETE FROM acc_trans WHERE trans_id = $form->{id}|;
51 $dbh->do($query) || $form->dberror($query);
54 my $rc = $dbh->commit;
62 sub post_transaction {
63 my ($self, $myconfig, $form) = @_;
65 my ($debit, $credit) = (0, 0);
69 # check if debit and credit balances
71 if ($form->{storno}) {
73 $credit = $credit * -1;
75 $form->{reference} = "Storno-".$form->{reference};
76 $form->{description} = "Storno-".$form->{description};
79 # connect to database, turn off AutoCommit
80 my $dbh = $form->dbconnect_noauto($myconfig);
82 # post the transaction
83 # make up a unique handle and store in reference field
84 # then retrieve the record based on the unique handle to get the id
85 # replace the reference field with the actual variable
86 # add records to acc_trans
88 # if there is a $form->{id} replace the old transaction
89 # delete all acc_trans entries and add the new ones
92 map { $form->{$_} =~ s/\'/\'\'/g } qw(reference description notes);
94 if (!$form->{taxincluded}) {
95 $form->{taxincluded} = 0;
101 # delete individual transactions
102 $query = qq|DELETE FROM acc_trans
103 WHERE trans_id = $form->{id}|;
104 $dbh->do($query) || $form->dberror($query);
108 $uid .= $form->{login};
110 $query = qq|INSERT INTO gl (reference, employee_id)
111 VALUES ('$uid', (SELECT e.id FROM employee e
112 WHERE e.login = '$form->{login}'))|;
113 $dbh->do($query) || $form->dberror($query);
115 $query = qq|SELECT g.id FROM gl g
116 WHERE g.reference = '$uid'|;
117 $sth = $dbh->prepare($query);
118 $sth->execute || $form->dberror($query);
120 ($form->{id}) = $sth->fetchrow_array;
125 my ($null, $department_id) = split /--/, $form->{department};
128 $query = qq|UPDATE gl SET
129 reference = '$form->{reference}',
130 description = '$form->{description}',
131 notes = '$form->{notes}',
132 transdate = '$form->{transdate}',
133 department_id = $department_id,
134 taxincluded = '$form->{taxincluded}'
135 WHERE id = $form->{id}|;
137 $dbh->do($query) || $form->dberror($query);
138 ($taxkey, $rate) = split(/--/, $form->{taxkey});
141 # insert acc_trans transactions
142 for $i (1 .. $form->{rowcount}) {
144 my ($accno) = split(/--/, $form->{"accno_$i"});
145 my ($taxkey, $rate) = split(/--/, $form->{"taxchart_$i"});
147 my $debit = $form->{"debit_$i"};
148 my $credit = $form->{"credit_$i"};
149 my $tax = $form->{"tax_$i"};
156 $amount = $debit * -1;
162 # if there is an amount, add the record
164 $project_id = ($form->{"project_id_$i"}) ? $form->{"project_id_$i"} : 'NULL';
165 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate,
166 source, memo, project_id, taxkey)
168 ($form->{id}, (SELECT c.id
170 WHERE c.accno = '$accno'),
171 $amount, '$form->{transdate}', |.
172 $dbh->quote($form->{"source_$i"}) .qq|, |.
173 $dbh->quote($form->{"memo_$i"}).qq|,
174 $project_id, $taxkey)|;
176 $dbh->do($query) || $form->dberror($query);
184 $project_id = ($form->{"project_id_$i"}) ? $form->{"project_id_$i"} : 'NULL';
185 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate,
186 source, memo, project_id, taxkey)
188 ($form->{id}, (SELECT t.chart_id
190 WHERE t.taxkey = $taxkey),
191 $amount, '$form->{transdate}', |.
192 $dbh->quote($form->{"source_$i"}) .qq|, |.
193 $dbh->quote($form->{"memo_$i"}).qq|,
194 $project_id, $taxkey)|;
196 $dbh->do($query) || $form->dberror($query);
200 my %audittrail = ( tablename => 'gl',
201 reference => $form->{reference},
202 formname => 'transaction',
206 # $form->audittrail($dbh, "", \%audittrail);
208 # commit and redirect
209 my $rc = $dbh->commit;
218 sub all_transactions {
219 my ($self, $myconfig, $form) = @_;
221 # connect to database
222 my $dbh = $form->dbconnect($myconfig);
223 my ($query, $sth, $source, $null);
225 my ($glwhere, $arwhere, $apwhere) = ("1 = 1", "1 = 1", "1 = 1");
227 if ($form->{reference}) {
228 $source = $form->like(lc $form->{reference});
229 $glwhere .= " AND lower(g.reference) LIKE '$source'";
230 $arwhere .= " AND lower(a.invnumber) LIKE '$source'";
231 $apwhere .= " AND lower(a.invnumber) LIKE '$source'";
233 if ($form->{department}) {
234 ($null, $source) = split /--/, $form->{department};
235 $glwhere .= " AND g.department_id = $source";
236 $arwhere .= " AND a.department_id = $source";
237 $apwhere .= " AND a.department_id = $source";
240 if ($form->{source}) {
241 $source = $form->like(lc $form->{source});
242 $glwhere .= " AND lower(ac.source) LIKE '$source'";
243 $arwhere .= " AND lower(ac.source) LIKE '$source'";
244 $apwhere .= " AND lower(ac.source) LIKE '$source'";
246 if ($form->{datefrom}) {
247 $glwhere .= " AND ac.transdate >= '$form->{datefrom}'";
248 $arwhere .= " AND ac.transdate >= '$form->{datefrom}'";
249 $apwhere .= " AND ac.transdate >= '$form->{datefrom}'";
251 if ($form->{dateto}) {
252 $glwhere .= " AND ac.transdate <= '$form->{dateto}'";
253 $arwhere .= " AND ac.transdate <= '$form->{dateto}'";
254 $apwhere .= " AND ac.transdate <= '$form->{dateto}'";
256 if ($form->{description}) {
257 my $description = $form->like(lc $form->{description});
258 $glwhere .= " AND lower(g.description) LIKE '$description'";
259 $arwhere .= " AND lower(ct.name) LIKE '$description'";
260 $apwhere .= " AND lower(ct.name) LIKE '$description'";
262 if ($form->{notes}) {
263 my $notes = $form->like(lc $form->{notes});
264 $glwhere .= " AND lower(g.notes) LIKE '$notes'";
265 $arwhere .= " AND lower(a.notes) LIKE '$notes'";
266 $apwhere .= " AND lower(a.notes) LIKE '$notes'";
268 if ($form->{accno}) {
269 $glwhere .= " AND c.accno = '$form->{accno}'";
270 $arwhere .= " AND c.accno = '$form->{accno}'";
271 $apwhere .= " AND c.accno = '$form->{accno}'";
273 if ($form->{gifi_accno}) {
274 $glwhere .= " AND c.gifi_accno = '$form->{gifi_accno}'";
275 $arwhere .= " AND c.gifi_accno = '$form->{gifi_accno}'";
276 $apwhere .= " AND c.gifi_accno = '$form->{gifi_accno}'";
278 if ($form->{category} ne 'X') {
279 $glwhere .= " AND c.category = '$form->{category}'";
280 $arwhere .= " AND c.category = '$form->{category}'";
281 $apwhere .= " AND c.category = '$form->{category}'";
284 if ($form->{accno}) {
285 # get category for account
286 $query = qq|SELECT c.category
288 WHERE c.accno = '$form->{accno}'|;
289 $sth = $dbh->prepare($query);
291 $sth->execute || $form->dberror($query);
292 ($form->{ml}) = $sth->fetchrow_array;
295 if ($form->{datefrom}) {
296 $query = qq|SELECT SUM(ac.amount)
297 FROM acc_trans ac, chart c
298 WHERE ac.chart_id = c.id
299 AND c.accno = '$form->{accno}'
300 AND ac.transdate < date '$form->{datefrom}'
302 $sth = $dbh->prepare($query);
303 $sth->execute || $form->dberror($query);
305 ($form->{balance}) = $sth->fetchrow_array;
310 if ($form->{gifi_accno}) {
311 # get category for account
312 $query = qq|SELECT c.category
314 WHERE c.gifi_accno = '$form->{gifi_accno}'|;
315 $sth = $dbh->prepare($query);
317 $sth->execute || $form->dberror($query);
318 ($form->{ml}) = $sth->fetchrow_array;
321 if ($form->{datefrom}) {
322 $query = qq|SELECT SUM(ac.amount)
323 FROM acc_trans ac, chart c
324 WHERE ac.chart_id = c.id
325 AND c.gifi_accno = '$form->{gifi_accno}'
326 AND ac.transdate < date '$form->{datefrom}'
328 $sth = $dbh->prepare($query);
329 $sth->execute || $form->dberror($query);
331 ($form->{balance}) = $sth->fetchrow_array;
336 my $false = ($myconfig->{dbdriver} eq 'Pg') ? FALSE : q|'0'|;
339 my $query = qq|SELECT g.id, 'gl' AS type, $false AS invoice, g.reference, ac.taxkey, t.taxkey AS sorttax,
340 g.description, ac.transdate, ac.source, ac.trans_id,
341 ac.amount, c.accno, c.gifi_accno, g.notes, t.chart_id, ac.oid
342 FROM gl g, acc_trans ac, chart c LEFT JOIN tax t ON
345 AND ac.chart_id = c.id
346 AND g.id = ac.trans_id
348 SELECT a.id, 'ar' AS type, a.invoice, a.invnumber, ac.taxkey, t.taxkey AS sorttax,
349 ct.name, ac.transdate, ac.source, ac.trans_id,
350 ac.amount, c.accno, c.gifi_accno, a.notes, t.chart_id, ac.oid
351 FROM ar a, acc_trans ac, customer ct, chart c LEFT JOIN tax t ON
354 AND ac.chart_id = c.id
355 AND a.customer_id = ct.id
356 AND a.id = ac.trans_id
358 SELECT a.id, 'ap' 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, c.accno, c.gifi_accno, a.notes, t.chart_id, ac.oid
361 FROM ap a, acc_trans ac, vendor ct, chart c LEFT JOIN tax t ON
364 AND ac.chart_id = c.id
365 AND a.vendor_id = ct.id
366 AND a.id = ac.trans_id
367 ORDER BY transdate, trans_id, taxkey DESC, sorttax DESC, oid|;
368 my $sth = $dbh->prepare($query);
369 $sth->execute || $form->dberror($query);
371 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
372 print(STDERR $ref->{id}, " Transaction\n");
374 if ($ref->{type} eq "gl") {
375 $ref->{module} = "gl";
379 if ($ref->{type} eq "ap") {
380 if ($ref->{invoice}) {
381 $ref->{module} = "ir";
383 $ref->{module} = "ap";
388 if ($ref->{type} eq "ar") {
389 if ($ref->{invoice}) {
390 $ref->{module} = "is";
392 $ref->{module} = "ar";
395 $balance=$ref->{amount};
400 if ($ref->{amount} < 0) {
401 if ($ref->{chart_id} >0) {
402 $ref->{debit_tax}{$i} = $ref->{amount} * -1;
403 $ref->{debit_tax_accno}{$i} = $ref->{accno};
406 $ref->{debit}{$k} = $ref->{amount} * -1;
407 $ref->{debit_accno}{$k} = $ref->{accno};
408 $ref->{debit_taxkey}{$k} = $ref->{taxkey};
411 if ($ref->{chart_id} >0) {
412 $ref->{credit_tax}{$j} = $ref->{amount};
413 $ref->{credit_tax_accno}{$j} = $ref->{accno};
416 $ref->{credit}{$l} = $ref->{amount};
417 $ref->{credit_accno}{$l} = $ref->{accno};
418 $ref->{credit_taxkey}{$l} = $ref->{taxkey};
422 while (abs($balance)>=0.015) {
423 my $ref2 = $sth->fetchrow_hashref(NAME_lc) || $form->error("Unbalanced ledger!");
425 $balance = (int($balance * 100000) + int(100000 * $ref2->{amount})) / 100000;
426 print(STDERR $balance," BAlance\n");
427 if ($ref2->{amount} < 0) {
428 if ($ref2->{chart_id} >0) {
429 if ($ref->{debit_tax_accno}{$i} ne "") {
432 $ref->{debit_tax}{$i} = $ref2->{amount} * -1;
433 $ref->{debit_tax_accno}{$i} = $ref2->{accno};
436 if ($ref->{debit_accno}{$k} ne "") {
439 $ref->{debit}{$k} = $ref2->{amount} * -1;
440 $ref->{debit_accno}{$k} = $ref2->{accno};
441 $ref->{debit_taxkey}{$k} = $ref2->{taxkey};
444 if ($ref2->{chart_id} >0) {
445 if ($ref->{credit_tax_accno}{$j} ne "") {
448 $ref->{credit_tax}{$j} = $ref2->{amount};
449 $ref->{credit_tax_accno}{$j} = $ref2->{accno};
452 if ($ref->{credit_accno}{$l} ne "") {
455 $ref->{credit}{$l} = $ref2->{amount};
456 $ref->{credit_accno}{$l} = $ref2->{accno};
457 $ref->{credit_taxkey}{$l} = $ref2->{taxkey};
461 # print(STDERR Dumper($ref));
462 push @{ $form->{GL} }, $ref;
467 if ($form->{accno}) {
468 $query = qq|SELECT c.description FROM chart c WHERE c.accno = '$form->{accno}'|;
469 $sth = $dbh->prepare($query);
470 $sth->execute || $form->dberror($query);
472 ($form->{account_description}) = $sth->fetchrow_array;
475 if ($form->{gifi_accno}) {
476 $query = qq|SELECT g.description FROM gifi g WHERE g.accno = '$form->{gifi_accno}'|;
477 $sth = $dbh->prepare($query);
478 $sth->execute || $form->dberror($query);
480 ($form->{gifi_account_description}) = $sth->fetchrow_array;
490 my ($self, $myconfig, $form) = @_;
492 my ($query, $sth, $ref);
494 # connect to database
495 my $dbh = $form->dbconnect($myconfig);
498 $query = "SELECT closedto, revtrans
500 $sth = $dbh->prepare($query);
501 $sth->execute || $form->dberror($query);
503 ($form->{closedto}, $form->{revtrans}) = $sth->fetchrow_array;
506 $query = "SELECT g.reference, g.description, g.notes, g.transdate,
507 d.description AS department, e.name as employee, g.taxincluded, g.gldate
509 LEFT JOIN department d ON (d.id = g.department_id)
510 LEFT JOIN employee e ON (e.id = g.employee_id)
511 WHERE g.id = $form->{id}";
512 $sth = $dbh->prepare($query);
513 $sth->execute || $form->dberror($query);
514 $ref = $sth->fetchrow_hashref(NAME_lc);
515 map { $form->{$_} = $ref->{$_} } keys %$ref;
518 # retrieve individual rows
519 $query = "SELECT c.accno, a.amount, project_id,
520 (SELECT p.projectnumber FROM project p
521 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
522 FROM acc_trans a, chart c
523 WHERE a.chart_id = c.id
524 AND a.trans_id = $form->{id}
526 $sth = $dbh->prepare($query);
527 $sth->execute || $form->dberror($query);
529 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
530 push @{ $form->{GL} }, $ref;
533 # get tax description
534 $query = qq| SELECT * FROM tax t|;
535 $sth = $dbh->prepare($query);
536 $sth->execute || $form->dberror($query);
538 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
539 push @{ $form->{TAX} }, $ref;
544 $query = "SELECT current_date AS transdate, closedto, revtrans
546 $sth = $dbh->prepare($query);
547 $sth->execute || $form->dberror($query);
549 ($form->{transdate}, $form->{closedto}, $form->{revtrans}) = $sth->fetchrow_array;
551 # get tax description
552 $query = qq| SELECT * FROM tax t order by t.taxkey|;
553 $sth = $dbh->prepare($query);
554 $sth->execute || $form->dberror($query);
556 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
557 push @{ $form->{TAX} }, $ref;
563 # get chart of accounts
564 $query = qq|SELECT c.accno, c.description, c.taxkey_id
566 WHERE c.charttype = 'A'
568 $sth = $dbh->prepare($query);
569 $sth->execute || $form->dberror($query);
571 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
572 push @{ $form->{chart} }, $ref;