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 $main::lxdebug->enter_sub();
46 my ($self, $myconfig, $form) = @_;
49 my $dbh = $form->dbconnect_noauto($myconfig);
51 my $query = qq|DELETE FROM gl WHERE id = $form->{id}|;
52 $dbh->do($query) || $form->dberror($query);
54 $query = qq|DELETE FROM acc_trans WHERE trans_id = $form->{id}|;
55 $dbh->do($query) || $form->dberror($query);
58 my $rc = $dbh->commit;
61 $main::lxdebug->leave_sub();
66 sub post_transaction {
67 $main::lxdebug->enter_sub();
69 my ($self, $myconfig, $form) = @_;
71 my ($debit, $credit) = (0, 0);
76 # check if debit and credit balances
78 $debit = abs(int($form->round_amount($form->{debit}, 3) * 1000));
79 $credit = abs(int($form->round_amount($form->{credit}, 3) * 1000));
80 $tax = abs(int($form->round_amount($form->{tax}, 3) * 1000));
82 if ( (($debit >= $credit) && (abs($debit - ($credit + $tax)) > 4))
83 || (($debit < $credit) && (abs(($debit + $tax) - $credit) > 4))) {
87 if (($debit + $credit + $tax) == 0) {
91 $debit = $form->round_amount($form->{debit}, 2);
92 $credit = $form->round_amount($form->{credit}, 2);
93 $tax = $form->round_amount($form->{tax}, 2);
94 debug($debit, $credit, $tax, "Betraege");
96 if ($form->{storno}) {
98 $credit = $credit * -1;
100 $form->{reference} = "Storno-" . $form->{reference};
101 $form->{description} = "Storno-" . $form->{description};
104 # connect to database, turn off AutoCommit
105 my $dbh = $form->dbconnect_noauto($myconfig);
107 # post the transaction
108 # make up a unique handle and store in reference field
109 # then retrieve the record based on the unique handle to get the id
110 # replace the reference field with the actual variable
111 # add records to acc_trans
113 # if there is a $form->{id} replace the old transaction
114 # delete all acc_trans entries and add the new ones
117 map { $form->{$_} =~ s/\'/\'\'/g } qw(reference description notes);
119 if (!$form->{taxincluded}) {
120 $form->{taxincluded} = 0;
126 # delete individual transactions
127 $query = qq|DELETE FROM acc_trans
128 WHERE trans_id = $form->{id}|;
129 $dbh->do($query) || $form->dberror($query);
133 $uid .= $form->{login};
135 $query = qq|INSERT INTO gl (reference, employee_id)
136 VALUES ('$uid', (SELECT e.id FROM employee e
137 WHERE e.login = '$form->{login}'))|;
138 $dbh->do($query) || $form->dberror($query);
140 $query = qq|SELECT g.id FROM gl g
141 WHERE g.reference = '$uid'|;
142 $sth = $dbh->prepare($query);
143 $sth->execute || $form->dberror($query);
145 ($form->{id}) = $sth->fetchrow_array;
149 my ($null, $department_id) = split /--/, $form->{department};
152 $query = qq|UPDATE gl SET
153 reference = '$form->{reference}',
154 description = '$form->{description}',
155 notes = '$form->{notes}',
156 transdate = '$form->{transdate}',
157 department_id = $department_id,
158 taxincluded = '$form->{taxincluded}'
159 WHERE id = $form->{id}|;
161 $dbh->do($query) || $form->dberror($query);
162 ($taxkey, $rate) = split(/--/, $form->{taxkey});
164 # insert acc_trans transactions
165 foreach $i ((credit, debit)) {
168 ($accno) = split(/--/, $form->{"${i}chartselected"});
170 debug("$accno $i Kontonummer");
171 if ($i eq "credit") {
175 $amount = $debit * -1;
178 if ($form->{"${i}_splited"}) {
180 # if there is an amount, add the record
181 for $j (2 .. $form->{"${i}rowcount"}) {
182 ($accno) = split(/--/, $form->{"${i}chartselected_$j"});
184 $amount = $form->{"${i}_$j"};
186 ($taxkey, $taxrate) = split(/--/, $form->{"taxchartselected_$j"});
193 ($form->{"project_id_$i"}) ? $form->{"project_id_$i"} : 'NULL';
195 qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate,
196 source, project_id, taxkey)
198 ($form->{id}, (SELECT c.id
200 WHERE c.accno = '$accno'),
201 $amount, '$form->{transdate}', '$form->{reference}',
202 $project_id, $taxkey)|;
204 $dbh->do($query) || $form->dberror($query);
207 $tax = $form->{"tax_$j"};
208 print(STDERR $tax, " Steuer bei Durchlauf $j\n\n");
218 ($form->{"project_id_$i"}) ? $form->{"project_id_$i"} : 'NULL';
220 qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate,
221 source, project_id, taxkey)
223 ($form->{id}, (SELECT t.chart_id
225 WHERE t.taxkey = $taxkey),
226 $amount, '$form->{transdate}', '$form->{reference}',
227 $project_id, $taxkey)|;
229 $dbh->do($query) || $form->dberror($query);
234 # if there is an amount, add the record
235 ($taxkey, $taxrate) = split(/--/, $form->{"taxchartselected"});
237 debug("$amount auf $accno buchen");
240 ($form->{"project_id_$i"}) ? $form->{"project_id_$i"} : 'NULL';
242 qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate,
243 source, project_id, taxkey)
245 ($form->{id}, (SELECT c.id
247 WHERE c.accno = '$accno'),
248 $amount, '$form->{transdate}', '$form->{reference}',
249 $project_id, $taxkey)|;
251 $dbh->do($query) || $form->dberror($query);
255 if ($tax != 0 && !($form->{credit_splited} || $form->{debit_splited})) {
258 if ($form->{debittaxkey}) {
262 debug("$amount Steuern buchen");
265 ($form->{"project_id_$i"}) ? $form->{"project_id_$i"} : 'NULL';
266 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate,
267 source, project_id, taxkey)
269 ($form->{id}, (SELECT t.chart_id
271 WHERE t.taxkey = $taxkey),
272 $amount, '$form->{transdate}', '$form->{reference}',
273 $project_id, $taxkey)|;
275 $dbh->do($query) || $form->dberror($query);
278 # commit and redirect
279 my $rc = $dbh->commit;
282 $main::lxdebug->leave_sub();
287 sub all_transactions {
288 $main::lxdebug->enter_sub();
290 my ($self, $myconfig, $form) = @_;
292 # connect to database
293 my $dbh = $form->dbconnect($myconfig);
294 my ($query, $sth, $source, $null);
296 my ($glwhere, $arwhere, $apwhere) = ("1 = 1", "1 = 1", "1 = 1");
298 if ($form->{reference}) {
299 $source = $form->like(lc $form->{reference});
300 $glwhere .= " AND lower(g.reference) LIKE '$source'";
301 $arwhere .= " AND lower(a.invnumber) LIKE '$source'";
302 $apwhere .= " AND lower(a.invnumber) LIKE '$source'";
304 if ($form->{department}) {
305 ($null, $source) = split /--/, $form->{department};
306 $glwhere .= " AND g.department_id = $source";
307 $arwhere .= " AND a.department_id = $source";
308 $apwhere .= " AND a.department_id = $source";
311 if ($form->{source}) {
312 $source = $form->like(lc $form->{source});
313 $glwhere .= " AND lower(ac.source) LIKE '$source'";
314 $arwhere .= " AND lower(ac.source) LIKE '$source'";
315 $apwhere .= " AND lower(ac.source) LIKE '$source'";
317 if ($form->{datefrom}) {
318 $glwhere .= " AND ac.transdate >= '$form->{datefrom}'";
319 $arwhere .= " AND ac.transdate >= '$form->{datefrom}'";
320 $apwhere .= " AND ac.transdate >= '$form->{datefrom}'";
322 if ($form->{dateto}) {
323 $glwhere .= " AND ac.transdate <= '$form->{dateto}'";
324 $arwhere .= " AND ac.transdate <= '$form->{dateto}'";
325 $apwhere .= " AND ac.transdate <= '$form->{dateto}'";
327 if ($form->{description}) {
328 my $description = $form->like(lc $form->{description});
329 $glwhere .= " AND lower(g.description) LIKE '$description'";
330 $arwhere .= " AND lower(ct.name) LIKE '$description'";
331 $apwhere .= " AND lower(ct.name) LIKE '$description'";
333 if ($form->{notes}) {
334 my $notes = $form->like(lc $form->{notes});
335 $glwhere .= " AND lower(g.notes) LIKE '$notes'";
336 $arwhere .= " AND lower(a.notes) LIKE '$notes'";
337 $apwhere .= " AND lower(a.notes) LIKE '$notes'";
339 if ($form->{accno}) {
340 $glwhere .= " AND c.accno = '$form->{accno}'";
341 $arwhere .= " AND c.accno = '$form->{accno}'";
342 $apwhere .= " AND c.accno = '$form->{accno}'";
344 if ($form->{gifi_accno}) {
345 $glwhere .= " AND c.gifi_accno = '$form->{gifi_accno}'";
346 $arwhere .= " AND c.gifi_accno = '$form->{gifi_accno}'";
347 $apwhere .= " AND c.gifi_accno = '$form->{gifi_accno}'";
349 if ($form->{category} ne 'X') {
350 $glwhere .= " AND c.category = '$form->{category}'";
351 $arwhere .= " AND c.category = '$form->{category}'";
352 $apwhere .= " AND c.category = '$form->{category}'";
355 if ($form->{accno}) {
357 # get category for account
358 $query = qq|SELECT c.category
360 WHERE c.accno = '$form->{accno}'|;
361 $sth = $dbh->prepare($query);
363 $sth->execute || $form->dberror($query);
364 ($form->{ml}) = $sth->fetchrow_array;
367 if ($form->{datefrom}) {
368 $query = qq|SELECT SUM(ac.amount)
369 FROM acc_trans ac, chart c
370 WHERE ac.chart_id = c.id
371 AND c.accno = '$form->{accno}'
372 AND ac.transdate < date '$form->{datefrom}'
374 $sth = $dbh->prepare($query);
375 $sth->execute || $form->dberror($query);
377 ($form->{balance}) = $sth->fetchrow_array;
382 if ($form->{gifi_accno}) {
384 # get category for account
385 $query = qq|SELECT c.category
387 WHERE c.gifi_accno = '$form->{gifi_accno}'|;
388 $sth = $dbh->prepare($query);
390 $sth->execute || $form->dberror($query);
391 ($form->{ml}) = $sth->fetchrow_array;
394 if ($form->{datefrom}) {
395 $query = qq|SELECT SUM(ac.amount)
396 FROM acc_trans ac, chart c
397 WHERE ac.chart_id = c.id
398 AND c.gifi_accno = '$form->{gifi_accno}'
399 AND ac.transdate < date '$form->{datefrom}'
401 $sth = $dbh->prepare($query);
402 $sth->execute || $form->dberror($query);
404 ($form->{balance}) = $sth->fetchrow_array;
409 my $false = ($myconfig->{dbdriver} eq 'Pg') ? FALSE: q|'0'|;
412 qq|SELECT g.id, 'gl' AS type, $false AS invoice, g.reference, ac.taxkey, t.taxkey AS sorttax,
413 g.description, ac.transdate, ac.source, ac.trans_id,
414 ac.amount, c.accno, c.gifi_accno, g.notes, t.chart_id, ac.oid
415 FROM gl g, acc_trans ac, chart c LEFT JOIN tax t ON
418 AND ac.chart_id = c.id
419 AND g.id = ac.trans_id
421 SELECT a.id, 'ar' AS type, a.invoice, a.invnumber, ac.taxkey, t.taxkey AS sorttax,
422 ct.name, ac.transdate, ac.source, ac.trans_id,
423 ac.amount, c.accno, c.gifi_accno, a.notes, t.chart_id, ac.oid
424 FROM ar a, acc_trans ac, customer ct, chart c LEFT JOIN tax t ON
427 AND ac.chart_id = c.id
428 AND a.customer_id = ct.id
429 AND a.id = ac.trans_id
431 SELECT a.id, 'ap' AS type, a.invoice, a.invnumber, ac.taxkey, t.taxkey AS sorttax,
432 ct.name, ac.transdate, ac.source, ac.trans_id,
433 ac.amount, c.accno, c.gifi_accno, a.notes, t.chart_id, ac.oid
434 FROM ap a, acc_trans ac, vendor ct, chart c LEFT JOIN tax t ON
437 AND ac.chart_id = c.id
438 AND a.vendor_id = ct.id
439 AND a.id = ac.trans_id
440 ORDER BY transdate, trans_id, taxkey DESC, sorttax DESC, oid|;
441 my $sth = $dbh->prepare($query);
442 $sth->execute || $form->dberror($query);
444 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
445 print(STDERR $ref->{id}, " Transaction\n");
448 if ($ref->{type} eq "gl") {
449 $ref->{module} = "gl";
453 if ($ref->{type} eq "ap") {
454 if ($ref->{invoice}) {
455 $ref->{module} = "ir";
457 $ref->{module} = "ap";
462 if ($ref->{type} eq "ar") {
463 if ($ref->{invoice}) {
464 $ref->{module} = "is";
466 $ref->{module} = "ar";
469 $balance = $ref->{amount};
474 if ($ref->{amount} < 0) {
475 if ($ref->{chart_id} > 0) {
476 $ref->{debit_tax}{$i} = $ref->{amount} * -1;
477 $ref->{debit_tax_accno}{$i} = $ref->{accno};
479 $ref->{debit}{$k} = $ref->{amount} * -1;
480 $ref->{debit_accno}{$k} = $ref->{accno};
481 $ref->{debit_taxkey}{$k} = $ref->{taxkey};
484 if ($ref->{chart_id} > 0) {
485 $ref->{credit_tax}{$j} = $ref->{amount};
486 $ref->{credit_tax_accno}{$j} = $ref->{accno};
488 $ref->{credit}{$l} = $ref->{amount};
489 $ref->{credit_accno}{$l} = $ref->{accno};
490 $ref->{credit_taxkey}{$l} = $ref->{taxkey};
494 while (abs($balance) >= 0.015) {
495 my $ref2 = $sth->fetchrow_hashref(NAME_lc)
496 || $form->error("Unbalanced ledger!");
499 (int($balance * 100000) + int(100000 * $ref2->{amount})) / 100000;
500 print(STDERR $balance, " BAlance\n");
501 if ($ref2->{amount} < 0) {
502 if ($ref2->{chart_id} > 0) {
503 if ($ref->{debit_tax_accno}{$i} ne "") {
506 $ref->{debit_tax}{$i} = $ref2->{amount} * -1;
507 $ref->{debit_tax_accno}{$i} = $ref2->{accno};
509 if ($ref->{debit_accno}{$k} ne "") {
512 $ref->{debit}{$k} = $ref2->{amount} * -1;
513 $ref->{debit_accno}{$k} = $ref2->{accno};
514 $ref->{debit_taxkey}{$k} = $ref2->{taxkey};
517 if ($ref2->{chart_id} > 0) {
518 if ($ref->{credit_tax_accno}{$j} ne "") {
521 $ref->{credit_tax}{$j} = $ref2->{amount};
522 $ref->{credit_tax_accno}{$j} = $ref2->{accno};
524 if ($ref->{credit_accno}{$l} ne "") {
527 $ref->{credit}{$l} = $ref2->{amount};
528 $ref->{credit_accno}{$l} = $ref2->{accno};
529 $ref->{credit_taxkey}{$l} = $ref2->{taxkey};
534 # print(STDERR Dumper($ref));
535 push @{ $form->{GL} }, $ref;
540 if ($form->{accno}) {
542 qq|SELECT c.description FROM chart c WHERE c.accno = '$form->{accno}'|;
543 $sth = $dbh->prepare($query);
544 $sth->execute || $form->dberror($query);
546 ($form->{account_description}) = $sth->fetchrow_array;
549 if ($form->{gifi_accno}) {
551 qq|SELECT g.description FROM gifi g WHERE g.accno = '$form->{gifi_accno}'|;
552 $sth = $dbh->prepare($query);
553 $sth->execute || $form->dberror($query);
555 ($form->{gifi_account_description}) = $sth->fetchrow_array;
561 $main::lxdebug->leave_sub();
565 $main::lxdebug->enter_sub();
567 my ($self, $myconfig, $form) = @_;
569 my ($query, $sth, $ref);
571 # connect to database
572 my $dbh = $form->dbconnect($myconfig);
573 $form->{creditrowcount} = 1;
574 $form->{debitrowcount} = 1;
576 $query = "SELECT closedto, revtrans
578 $sth = $dbh->prepare($query);
579 $sth->execute || $form->dberror($query);
581 ($form->{closedto}, $form->{revtrans}) = $sth->fetchrow_array;
584 $query = "SELECT g.reference, g.description, g.notes, g.transdate,
585 d.description AS department, e.name as employee, g.taxincluded, g.gldate
587 LEFT JOIN department d ON (d.id = g.department_id)
588 LEFT JOIN employee e ON (e.id = g.employee_id)
589 WHERE g.id = $form->{id}";
590 $sth = $dbh->prepare($query);
591 $sth->execute || $form->dberror($query);
592 $ref = $sth->fetchrow_hashref(NAME_lc);
593 map { $form->{$_} = $ref->{$_} } keys %$ref;
596 # retrieve individual rows
597 $query = "SELECT c.accno, a.amount, project_id,
598 (SELECT p.projectnumber FROM project p
599 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
600 FROM acc_trans a, chart c
601 WHERE a.chart_id = c.id
602 AND a.trans_id = $form->{id}
604 $sth = $dbh->prepare($query);
605 $sth->execute || $form->dberror($query);
610 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
612 if ($ref->{accno} eq $ref->{taxaccno}) {
613 $form->{"tax_$taxcount"} = $ref->{amount};
614 $form->{"tax"} += $ref->{amount};
615 $form->{"taxchartselected_$taxcount"} = $ref->{taxkey};
619 if ($ref->{amount} < 0) {
620 $form->{"debit_$debitcount"} = $ref->{amount} * -1;
621 $form->{"debit"} += $ref->{amount} * -1;
622 $form->{"debitchartselected_$debitcount"} = $ref->{accno};
625 if ($ref->{amount} > 0) {
627 $form->{"credit_$creditcount"} = $ref->{amount};
628 $form->{"credit"} += $ref->{amount};
629 $form->{"creditchartselected_$creditcount"} = $ref->{accno};
634 $taxkey = $ref->{taxkey} * 1;
636 if ($creditcount > 3) {
637 $form->{credit_splited} = 1;
638 $form->{credit} = $form->{credit} + $form->{tax};
639 $form->{creditrowcount} = $creditcount - 1;
641 $form->{credit} = $form->{credit_2};
642 $form->{amount} = $form->{amount_2};
643 $form->{creditaccno} = $form->{creditchartselected_2};
645 if ($debitcount > 3) {
646 $form->{debit_splited} = 1;
647 $form->{debit} = $form->{debit} + $form->{tax};
648 $form->{debitrowcount} = $debitcount - 1;
650 $form->{debit} = $form->{debit_2};
651 $form->{debitaccno} = $form->{debitchartselected_2};
654 if ( (($form->{credit} > $form->{debit}) && (!$form->{taxincluded}))
655 || (($form->{credit} > $form->{debit}) && ($form->{taxincluded}))) {
656 $form->{amount} = $form->{debit};
658 $form->{amount} = $form->{credit};
661 # get tax description
662 $query = qq| SELECT * FROM tax t|;
663 $sth = $dbh->prepare($query);
664 $sth->execute || $form->dberror($query);
666 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
667 push @{ $form->{TAX} }, $ref;
672 $query = "SELECT current_date AS transdate, closedto, revtrans
674 $sth = $dbh->prepare($query);
675 $sth->execute || $form->dberror($query);
677 ($form->{transdate}, $form->{closedto}, $form->{revtrans}) =
678 $sth->fetchrow_array;
680 # get tax description
681 $query = qq| SELECT * FROM tax t order by t.taxkey|;
682 $sth = $dbh->prepare($query);
683 $sth->execute || $form->dberror($query);
685 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
686 push @{ $form->{TAX} }, $ref;
692 # get chart of accounts
693 $query = qq|SELECT c.accno, c.description, c.taxkey_id
695 WHERE c.charttype = 'A'
697 $sth = $dbh->prepare($query);
698 $sth->execute || $form->dberror($query);
700 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
701 push @{ $form->{chart} }, $ref;
709 $main::lxdebug->leave_sub();
714 if (open(OUT, ">>/tmp/linet.log")) {
717 print(OUT join("\n", @_), "\n");
720 print(STDERR "noe: $!\n");