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();
67 sub post_transaction {
68 $main::lxdebug->enter_sub();
70 my ($self, $myconfig, $form) = @_;
72 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)) || (($debit < $credit) && (abs(($debit + $tax)-$credit)>4))) {
86 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;
125 # delete individual transactions
126 $query = qq|DELETE FROM acc_trans
127 WHERE trans_id = $form->{id}|;
128 $dbh->do($query) || $form->dberror($query);
132 $uid .= $form->{login};
134 $query = qq|INSERT INTO gl (reference, employee_id)
135 VALUES ('$uid', (SELECT e.id FROM employee e
136 WHERE e.login = '$form->{login}'))|;
137 $dbh->do($query) || $form->dberror($query);
139 $query = qq|SELECT g.id FROM gl g
140 WHERE g.reference = '$uid'|;
141 $sth = $dbh->prepare($query);
142 $sth->execute || $form->dberror($query);
144 ($form->{id}) = $sth->fetchrow_array;
148 my ($null, $department_id) = split /--/, $form->{department};
151 $query = qq|UPDATE gl SET
152 reference = '$form->{reference}',
153 description = '$form->{description}',
154 notes = '$form->{notes}',
155 transdate = '$form->{transdate}',
156 department_id = $department_id,
157 taxincluded = '$form->{taxincluded}'
158 WHERE id = $form->{id}|;
160 $dbh->do($query) || $form->dberror($query);
161 ($taxkey, $rate) = split(/--/, $form->{taxkey});
162 # insert acc_trans transactions
163 foreach $i ((credit,debit)) {
165 ($accno) = split(/--/, $form->{"${i}chartselected"});
167 debug("$accno $i Kontonummer");
168 if ($i eq "credit") {
172 $amount = $debit * -1;
175 if ($form->{"${i}_splited"}) {
176 # if there is an amount, add the record
177 for $j (2 .. $form->{"${i}rowcount"}) {
178 ($accno) = split(/--/, $form->{"${i}chartselected_$j"});
180 $amount = $form->{"${i}_$j"};
182 ($taxkey, $taxrate) = split(/--/, $form->{"taxchartselected_$j"});
188 $project_id = ($form->{"project_id_$i"}) ? $form->{"project_id_$i"} : 'NULL';
189 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate,
190 source, project_id, taxkey)
192 ($form->{id}, (SELECT c.id
194 WHERE c.accno = '$accno'),
195 $amount, '$form->{transdate}', '$form->{reference}',
196 $project_id, $taxkey)|;
198 $dbh->do($query) || $form->dberror($query);
201 $tax = $form->{"tax_$j"};
202 print(STDERR $tax, " Steuer bei Durchlauf $j\n\n");
211 $project_id = ($form->{"project_id_$i"}) ? $form->{"project_id_$i"} : 'NULL';
212 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate,
213 source, project_id, taxkey)
215 ($form->{id}, (SELECT t.chart_id
217 WHERE t.taxkey = $taxkey),
218 $amount, '$form->{transdate}', '$form->{reference}',
219 $project_id, $taxkey)|;
221 $dbh->do($query) || $form->dberror($query);
225 # if there is an amount, add the record
226 ($taxkey, $taxrate) = split(/--/, $form->{"taxchartselected"});
228 debug("$amount auf $accno buchen");
230 $project_id = ($form->{"project_id_$i"}) ? $form->{"project_id_$i"} : 'NULL';
231 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate,
232 source, project_id, taxkey)
234 ($form->{id}, (SELECT c.id
236 WHERE c.accno = '$accno'),
237 $amount, '$form->{transdate}', '$form->{reference}',
238 $project_id, $taxkey)|;
240 $dbh->do($query) || $form->dberror($query);
244 if ($tax !=0 && !($form->{credit_splited} || $form->{debit_splited})) {
246 if ($form->{debittaxkey}) {
250 debug("$amount Steuern buchen");
252 $project_id = ($form->{"project_id_$i"}) ? $form->{"project_id_$i"} : 'NULL';
253 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate,
254 source, project_id, taxkey)
256 ($form->{id}, (SELECT t.chart_id
258 WHERE t.taxkey = $taxkey),
259 $amount, '$form->{transdate}', '$form->{reference}',
260 $project_id, $taxkey)|;
262 $dbh->do($query) || $form->dberror($query);
266 # commit and redirect
267 my $rc = $dbh->commit;
270 $main::lxdebug->leave_sub();
277 sub all_transactions {
278 $main::lxdebug->enter_sub();
280 my ($self, $myconfig, $form) = @_;
282 # connect to database
283 my $dbh = $form->dbconnect($myconfig);
284 my ($query, $sth, $source, $null);
286 my ($glwhere, $arwhere, $apwhere) = ("1 = 1", "1 = 1", "1 = 1");
288 if ($form->{reference}) {
289 $source = $form->like(lc $form->{reference});
290 $glwhere .= " AND lower(g.reference) LIKE '$source'";
291 $arwhere .= " AND lower(a.invnumber) LIKE '$source'";
292 $apwhere .= " AND lower(a.invnumber) LIKE '$source'";
294 if ($form->{department}) {
295 ($null, $source) = split /--/, $form->{department};
296 $glwhere .= " AND g.department_id = $source";
297 $arwhere .= " AND a.department_id = $source";
298 $apwhere .= " AND a.department_id = $source";
301 if ($form->{source}) {
302 $source = $form->like(lc $form->{source});
303 $glwhere .= " AND lower(ac.source) LIKE '$source'";
304 $arwhere .= " AND lower(ac.source) LIKE '$source'";
305 $apwhere .= " AND lower(ac.source) LIKE '$source'";
307 if ($form->{datefrom}) {
308 $glwhere .= " AND ac.transdate >= '$form->{datefrom}'";
309 $arwhere .= " AND ac.transdate >= '$form->{datefrom}'";
310 $apwhere .= " AND ac.transdate >= '$form->{datefrom}'";
312 if ($form->{dateto}) {
313 $glwhere .= " AND ac.transdate <= '$form->{dateto}'";
314 $arwhere .= " AND ac.transdate <= '$form->{dateto}'";
315 $apwhere .= " AND ac.transdate <= '$form->{dateto}'";
317 if ($form->{description}) {
318 my $description = $form->like(lc $form->{description});
319 $glwhere .= " AND lower(g.description) LIKE '$description'";
320 $arwhere .= " AND lower(ct.name) LIKE '$description'";
321 $apwhere .= " AND lower(ct.name) LIKE '$description'";
323 if ($form->{notes}) {
324 my $notes = $form->like(lc $form->{notes});
325 $glwhere .= " AND lower(g.notes) LIKE '$notes'";
326 $arwhere .= " AND lower(a.notes) LIKE '$notes'";
327 $apwhere .= " AND lower(a.notes) LIKE '$notes'";
329 if ($form->{accno}) {
330 $glwhere .= " AND c.accno = '$form->{accno}'";
331 $arwhere .= " AND c.accno = '$form->{accno}'";
332 $apwhere .= " AND c.accno = '$form->{accno}'";
334 if ($form->{gifi_accno}) {
335 $glwhere .= " AND c.gifi_accno = '$form->{gifi_accno}'";
336 $arwhere .= " AND c.gifi_accno = '$form->{gifi_accno}'";
337 $apwhere .= " AND c.gifi_accno = '$form->{gifi_accno}'";
339 if ($form->{category} ne 'X') {
340 $glwhere .= " AND c.category = '$form->{category}'";
341 $arwhere .= " AND c.category = '$form->{category}'";
342 $apwhere .= " AND c.category = '$form->{category}'";
345 if ($form->{accno}) {
346 # get category for account
347 $query = qq|SELECT c.category
349 WHERE c.accno = '$form->{accno}'|;
350 $sth = $dbh->prepare($query);
352 $sth->execute || $form->dberror($query);
353 ($form->{ml}) = $sth->fetchrow_array;
356 if ($form->{datefrom}) {
357 $query = qq|SELECT SUM(ac.amount)
358 FROM acc_trans ac, chart c
359 WHERE ac.chart_id = c.id
360 AND c.accno = '$form->{accno}'
361 AND ac.transdate < date '$form->{datefrom}'
363 $sth = $dbh->prepare($query);
364 $sth->execute || $form->dberror($query);
366 ($form->{balance}) = $sth->fetchrow_array;
371 if ($form->{gifi_accno}) {
372 # get category for account
373 $query = qq|SELECT c.category
375 WHERE c.gifi_accno = '$form->{gifi_accno}'|;
376 $sth = $dbh->prepare($query);
378 $sth->execute || $form->dberror($query);
379 ($form->{ml}) = $sth->fetchrow_array;
382 if ($form->{datefrom}) {
383 $query = qq|SELECT SUM(ac.amount)
384 FROM acc_trans ac, chart c
385 WHERE ac.chart_id = c.id
386 AND c.gifi_accno = '$form->{gifi_accno}'
387 AND ac.transdate < date '$form->{datefrom}'
389 $sth = $dbh->prepare($query);
390 $sth->execute || $form->dberror($query);
392 ($form->{balance}) = $sth->fetchrow_array;
397 my $false = ($myconfig->{dbdriver} eq 'Pg') ? FALSE : q|'0'|;
400 my $query = qq|SELECT g.id, 'gl' AS type, $false AS invoice, g.reference, ac.taxkey, t.taxkey AS sorttax,
401 g.description, ac.transdate, ac.source, ac.trans_id,
402 ac.amount, c.accno, c.gifi_accno, g.notes, t.chart_id, ac.oid
403 FROM gl g, acc_trans ac, chart c LEFT JOIN tax t ON
406 AND ac.chart_id = c.id
407 AND g.id = ac.trans_id
409 SELECT a.id, 'ar' AS type, a.invoice, a.invnumber, ac.taxkey, t.taxkey AS sorttax,
410 ct.name, ac.transdate, ac.source, ac.trans_id,
411 ac.amount, c.accno, c.gifi_accno, a.notes, t.chart_id, ac.oid
412 FROM ar a, acc_trans ac, customer ct, chart c LEFT JOIN tax t ON
415 AND ac.chart_id = c.id
416 AND a.customer_id = ct.id
417 AND a.id = ac.trans_id
419 SELECT a.id, 'ap' AS type, a.invoice, a.invnumber, ac.taxkey, t.taxkey AS sorttax,
420 ct.name, ac.transdate, ac.source, ac.trans_id,
421 ac.amount, c.accno, c.gifi_accno, a.notes, t.chart_id, ac.oid
422 FROM ap a, acc_trans ac, vendor ct, chart c LEFT JOIN tax t ON
425 AND ac.chart_id = c.id
426 AND a.vendor_id = ct.id
427 AND a.id = ac.trans_id
428 ORDER BY transdate, trans_id, taxkey DESC, sorttax DESC, oid|;
429 my $sth = $dbh->prepare($query);
430 $sth->execute || $form->dberror($query);
432 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
433 print(STDERR $ref->{id}, " Transaction\n");
435 if ($ref->{type} eq "gl") {
436 $ref->{module} = "gl";
440 if ($ref->{type} eq "ap") {
441 if ($ref->{invoice}) {
442 $ref->{module} = "ir";
444 $ref->{module} = "ap";
449 if ($ref->{type} eq "ar") {
450 if ($ref->{invoice}) {
451 $ref->{module} = "is";
453 $ref->{module} = "ar";
456 $balance=$ref->{amount};
461 if ($ref->{amount} < 0) {
462 if ($ref->{chart_id} >0) {
463 $ref->{debit_tax}{$i} = $ref->{amount} * -1;
464 $ref->{debit_tax_accno}{$i} = $ref->{accno};
467 $ref->{debit}{$k} = $ref->{amount} * -1;
468 $ref->{debit_accno}{$k} = $ref->{accno};
469 $ref->{debit_taxkey}{$k} = $ref->{taxkey};
472 if ($ref->{chart_id} >0) {
473 $ref->{credit_tax}{$j} = $ref->{amount};
474 $ref->{credit_tax_accno}{$j} = $ref->{accno};
477 $ref->{credit}{$l} = $ref->{amount};
478 $ref->{credit_accno}{$l} = $ref->{accno};
479 $ref->{credit_taxkey}{$l} = $ref->{taxkey};
483 while (abs($balance)>=0.015) {
484 my $ref2 = $sth->fetchrow_hashref(NAME_lc) || $form->error("Unbalanced ledger!");
486 $balance = (int($balance * 100000) + int(100000 * $ref2->{amount})) / 100000;
487 print(STDERR $balance," BAlance\n");
488 if ($ref2->{amount} < 0) {
489 if ($ref2->{chart_id} >0) {
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 ($ref->{debit_accno}{$k} ne "") {
500 $ref->{debit}{$k} = $ref2->{amount} * -1;
501 $ref->{debit_accno}{$k} = $ref2->{accno};
502 $ref->{debit_taxkey}{$k} = $ref2->{taxkey};
505 if ($ref2->{chart_id} >0) {
506 if ($ref->{credit_tax_accno}{$j} ne "") {
509 $ref->{credit_tax}{$j} = $ref2->{amount};
510 $ref->{credit_tax_accno}{$j} = $ref2->{accno};
513 if ($ref->{credit_accno}{$l} ne "") {
516 $ref->{credit}{$l} = $ref2->{amount};
517 $ref->{credit_accno}{$l} = $ref2->{accno};
518 $ref->{credit_taxkey}{$l} = $ref2->{taxkey};
522 # print(STDERR Dumper($ref));
523 push @{ $form->{GL} }, $ref;
528 if ($form->{accno}) {
529 $query = qq|SELECT c.description FROM chart c WHERE c.accno = '$form->{accno}'|;
530 $sth = $dbh->prepare($query);
531 $sth->execute || $form->dberror($query);
533 ($form->{account_description}) = $sth->fetchrow_array;
536 if ($form->{gifi_accno}) {
537 $query = qq|SELECT g.description FROM gifi g WHERE g.accno = '$form->{gifi_accno}'|;
538 $sth = $dbh->prepare($query);
539 $sth->execute || $form->dberror($query);
541 ($form->{gifi_account_description}) = $sth->fetchrow_array;
547 $main::lxdebug->leave_sub();
552 $main::lxdebug->enter_sub();
554 my ($self, $myconfig, $form) = @_;
556 my ($query, $sth, $ref);
558 # connect to database
559 my $dbh = $form->dbconnect($myconfig);
560 $form->{creditrowcount} = 1;
561 $form->{debitrowcount} = 1;
563 $query = "SELECT closedto, revtrans
565 $sth = $dbh->prepare($query);
566 $sth->execute || $form->dberror($query);
568 ($form->{closedto}, $form->{revtrans}) = $sth->fetchrow_array;
571 $query = "SELECT g.reference, g.description, g.notes, g.transdate,
572 d.description AS department, e.name as employee, g.taxincluded, g.gldate
574 LEFT JOIN department d ON (d.id = g.department_id)
575 LEFT JOIN employee e ON (e.id = g.employee_id)
576 WHERE g.id = $form->{id}";
577 $sth = $dbh->prepare($query);
578 $sth->execute || $form->dberror($query);
579 $ref = $sth->fetchrow_hashref(NAME_lc);
580 map { $form->{$_} = $ref->{$_} } keys %$ref;
583 # retrieve individual rows
584 $query = "SELECT c.accno, a.amount, project_id,
585 (SELECT p.projectnumber FROM project p
586 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
587 FROM acc_trans a, chart c
588 WHERE a.chart_id = c.id
589 AND a.trans_id = $form->{id}
591 $sth = $dbh->prepare($query);
592 $sth->execute || $form->dberror($query);
597 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
599 if ($ref->{accno} eq $ref->{taxaccno}) {
600 $form->{"tax_$taxcount"}= $ref->{amount};
601 $form->{"tax"} += $ref->{amount};
602 $form->{"taxchartselected_$taxcount"} = $ref->{taxkey};
606 if ($ref->{amount} < 0) {
607 $form->{"debit_$debitcount"} = $ref->{amount} * -1;
608 $form->{"debit"} += $ref->{amount} * -1;
609 $form->{"debitchartselected_$debitcount"} = $ref->{accno};
612 if ($ref->{amount} > 0) {
614 $form->{"credit_$creditcount"} = $ref->{amount};
615 $form->{"credit"} += $ref->{amount};
616 $form->{"creditchartselected_$creditcount"} = $ref->{accno};
621 $taxkey = $ref->{taxkey} *1;
623 if ($creditcount > 3) {
624 $form->{credit_splited} = 1;
625 $form->{credit} = $form->{credit} + $form->{tax};
626 $form->{creditrowcount} = $creditcount - 1;
628 $form->{credit} = $form->{credit_2};
629 $form->{amount} = $form->{amount_2};
630 $form->{creditaccno} = $form->{creditchartselected_2};
632 if ($debitcount > 3) {
633 $form->{debit_splited} = 1;
634 $form->{debit} = $form->{debit} + $form->{tax};
635 $form->{debitrowcount} = $debitcount - 1;
637 $form->{debit} = $form->{debit_2};
638 $form->{debitaccno} = $form->{debitchartselected_2};
641 if ((($form->{credit} > $form->{debit}) && (!$form->{taxincluded})) || (($form->{credit} > $form->{debit}) && ($form->{taxincluded}))) {
642 $form->{amount} = $form->{debit};
644 $form->{amount} = $form->{credit};
647 # get tax description
648 $query = qq| SELECT * FROM tax t|;
649 $sth = $dbh->prepare($query);
650 $sth->execute || $form->dberror($query);
652 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
653 push @{ $form->{TAX} }, $ref;
658 $query = "SELECT current_date AS transdate, closedto, revtrans
660 $sth = $dbh->prepare($query);
661 $sth->execute || $form->dberror($query);
663 ($form->{transdate}, $form->{closedto}, $form->{revtrans}) = $sth->fetchrow_array;
665 # get tax description
666 $query = qq| SELECT * FROM tax t order by t.taxkey|;
667 $sth = $dbh->prepare($query);
668 $sth->execute || $form->dberror($query);
670 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
671 push @{ $form->{TAX} }, $ref;
677 # get chart of accounts
678 $query = qq|SELECT c.accno, c.description, c.taxkey_id
680 WHERE c.charttype = 'A'
682 $sth = $dbh->prepare($query);
683 $sth->execute || $form->dberror($query);
685 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
686 push @{ $form->{chart} }, $ref;
696 $main::lxdebug->leave_sub();
701 if (open(OUT, ">>/tmp/linet.log")) {
703 print(OUT join("\n", @_), "\n");
706 print(STDERR "noe: $!\n");