1 #=====================================================================
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
7 #=====================================================================
8 # SQL-Ledger Accounting
9 # Copyright (C) 1998-2002
11 # Author: Dieter Simader
12 # Email: dsimader@sql-ledger.org
13 # Web: http://www.sql-ledger.org
15 # Contributors: Benjamin Lee <benjaminlee@consultant.com>
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 # backend code for reports
33 #======================================================================
37 sub income_statement {
38 $main::lxdebug->enter_sub();
40 my ($self, $myconfig, $form) = @_;
43 my $dbh = $form->dbconnect($myconfig);
46 my @categories = qw(I E);
49 $form->{decimalplaces} *= 1;
51 &get_accounts($dbh, $last_period, $form->{fromdate}, $form->{todate}, $form,
54 # if there are any compare dates
55 if ($form->{comparefromdate} || $form->{comparetodate}) {
58 &get_accounts($dbh, $last_period,
59 $form->{comparefromdate},
60 $form->{comparetodate},
67 # now we got $form->{I}{accno}{ }
68 # and $form->{E}{accno}{ }
71 'I' => { 'label' => 'income',
75 'E' => { 'label' => 'expense',
76 'labels' => 'expenses',
82 foreach $category (@categories) {
84 foreach $key (sort keys %{ $form->{$category} }) {
86 # push description onto array
88 $str = ($form->{l_heading}) ? $form->{padding} : "";
90 if ($form->{$category}{$key}{charttype} eq "A") {
93 ? "$form->{$category}{$key}{accno} - $form->{$category}{$key}{description}"
94 : "$form->{$category}{$key}{description}";
96 if ($form->{$category}{$key}{charttype} eq "H") {
97 if ($account{$category}{subtotal} && $form->{l_subtotal}) {
99 push(@{ $form->{"$account{$category}{label}_account"} },
100 "$str$form->{bold}$account{$category}{subdescription}$form->{endbold}"
102 push(@{ $form->{"$account{$category}{labels}_this_period"} },
103 $form->format_amount(
105 $account{$category}{subthis} * $account{$category}{ml},
106 $form->{decimalplaces}, $dash
110 push(@{ $form->{"$account{$category}{labels}_last_period"} },
111 $form->format_amount(
113 $account{$category}{sublast} * $account{$category}{ml},
114 $form->{decimalplaces}, $dash
121 "$form->{br}$form->{bold}$form->{$category}{$key}{description}$form->{endbold}";
123 $account{$category}{subthis} = $form->{$category}{$key}{this};
124 $account{$category}{sublast} = $form->{$category}{$key}{last};
125 $account{$category}{subdescription} =
126 $form->{$category}{$key}{description};
127 $account{$category}{subtotal} = 1;
129 $form->{$category}{$key}{this} = 0;
130 $form->{$category}{$key}{last} = 0;
132 next unless $form->{l_heading};
137 push(@{ $form->{"$account{$category}{label}_account"} }, $str);
139 if ($form->{$category}{$key}{charttype} eq 'A') {
140 $form->{"total_$account{$category}{labels}_this_period"} +=
141 $form->{$category}{$key}{this} * $account{$category}{ml};
145 push(@{ $form->{"$account{$category}{labels}_this_period"} },
146 $form->format_amount(
148 $form->{$category}{$key}{this} * $account{$category}{ml},
149 $form->{decimalplaces}, $dash
152 # add amount or - for last period
154 $form->{"total_$account{$category}{labels}_last_period"} +=
155 $form->{$category}{$key}{last} * $account{$category}{ml};
157 push(@{ $form->{"$account{$category}{labels}_last_period"} },
158 $form->format_amount(
160 $form->{$category}{$key}{last} * $account{$category}{ml},
161 $form->{decimalplaces}, $dash
166 $str = ($form->{l_heading}) ? $form->{padding} : "";
167 if ($account{$category}{subtotal} && $form->{l_subtotal}) {
168 push(@{ $form->{"$account{$category}{label}_account"} },
169 "$str$form->{bold}$account{$category}{subdescription}$form->{endbold}"
171 push(@{ $form->{"$account{$category}{labels}_this_period"} },
172 $form->format_amount(
174 $account{$category}{subthis} * $account{$category}{ml},
175 $form->{decimalplaces}, $dash
179 push(@{ $form->{"$account{$category}{labels}_last_period"} },
180 $form->format_amount(
182 $account{$category}{sublast} * $account{$category}{ml},
183 $form->{decimalplaces}, $dash
190 # totals for income and expenses
191 $form->{total_income_this_period} =
192 $form->round_amount($form->{total_income_this_period},
193 $form->{decimalplaces});
194 $form->{total_expenses_this_period} =
195 $form->round_amount($form->{total_expenses_this_period},
196 $form->{decimalplaces});
198 # total for income/loss
199 $form->{total_this_period} =
200 $form->{total_income_this_period} - $form->{total_expenses_this_period};
204 # total for income/loss
205 $form->{total_last_period} =
206 $form->format_amount(
208 $form->{total_income_last_period} - $form->{total_expenses_last_period},
209 $form->{decimalplaces},
212 # totals for income and expenses for last_period
213 $form->{total_income_last_period} =
214 $form->format_amount($myconfig,
215 $form->{total_income_last_period},
216 $form->{decimalplaces}, "- ");
217 $form->{total_expenses_last_period} =
218 $form->format_amount($myconfig,
219 $form->{total_expenses_last_period},
220 $form->{decimalplaces}, "- ");
224 $form->{total_income_this_period} =
225 $form->format_amount($myconfig,
226 $form->{total_income_this_period},
227 $form->{decimalplaces}, "- ");
228 $form->{total_expenses_this_period} =
229 $form->format_amount($myconfig,
230 $form->{total_expenses_this_period},
231 $form->{decimalplaces}, "- ");
232 $form->{total_this_period} =
233 $form->format_amount($myconfig,
234 $form->{total_this_period},
235 $form->{decimalplaces}, "- ");
237 $main::lxdebug->leave_sub();
241 $main::lxdebug->enter_sub();
243 my ($self, $myconfig, $form) = @_;
245 # connect to database
246 my $dbh = $form->dbconnect($myconfig);
249 my @categories = qw(A C L Q);
251 # if there are any dates construct a where
252 if ($form->{asofdate}) {
254 $form->{this_period} = "$form->{asofdate}";
255 $form->{period} = "$form->{asofdate}";
259 $form->{decimalplaces} *= 1;
261 &get_accounts($dbh, $last_period, "", $form->{asofdate}, $form,
264 # if there are any compare dates
265 if ($form->{compareasofdate}) {
268 &get_accounts($dbh, $last_period, "", $form->{compareasofdate},
269 $form, \@categories);
271 $form->{last_period} = "$form->{compareasofdate}";
278 # now we got $form->{A}{accno}{ } assets
279 # and $form->{L}{accno}{ } liabilities
280 # and $form->{Q}{accno}{ } equity
281 # build asset accounts
287 'A' => { 'label' => 'asset',
288 'labels' => 'assets',
291 'L' => { 'label' => 'liability',
292 'labels' => 'liabilities',
295 'Q' => { 'label' => 'equity',
296 'labels' => 'equity',
300 foreach $category (grep { !/C/ } @categories) {
302 foreach $key (sort keys %{ $form->{$category} }) {
304 $str = ($form->{l_heading}) ? $form->{padding} : "";
306 if ($form->{$category}{$key}{charttype} eq "A") {
309 ? "$form->{$category}{$key}{accno} - $form->{$category}{$key}{description}"
310 : "$form->{$category}{$key}{description}";
312 if ($form->{$category}{$key}{charttype} eq "H") {
313 if ($account{$category}{subtotal} && $form->{l_subtotal}) {
315 push(@{ $form->{"$account{$category}{label}_account"} },
316 "$str$form->{bold}$account{$category}{subdescription}$form->{endbold}"
318 push(@{ $form->{"$account{$category}{label}_this_period"} },
319 $form->format_amount(
321 $account{$category}{subthis} * $account{$category}{ml},
322 $form->{decimalplaces}, $dash
326 push(@{ $form->{"$account{$category}{label}_last_period"} },
327 $form->format_amount(
329 $account{$category}{sublast} * $account{$category}{ml},
330 $form->{decimalplaces}, $dash
336 "$form->{bold}$form->{$category}{$key}{description}$form->{endbold}";
338 $account{$category}{subthis} = $form->{$category}{$key}{this};
339 $account{$category}{sublast} = $form->{$category}{$key}{last};
340 $account{$category}{subdescription} =
341 $form->{$category}{$key}{description};
342 $account{$category}{subtotal} = 1;
344 $form->{$category}{$key}{this} = 0;
345 $form->{$category}{$key}{last} = 0;
347 next unless $form->{l_heading};
352 # push description onto array
353 push(@{ $form->{"$account{$category}{label}_account"} }, $str);
355 if ($form->{$category}{$key}{charttype} eq 'A') {
356 $form->{"total_$account{$category}{labels}_this_period"} +=
357 $form->{$category}{$key}{this} * $account{$category}{ml};
361 push(@{ $form->{"$account{$category}{label}_this_period"} },
362 $form->format_amount(
364 $form->{$category}{$key}{this} * $account{$category}{ml},
365 $form->{decimalplaces}, $dash
369 $form->{"total_$account{$category}{labels}_last_period"} +=
370 $form->{$category}{$key}{last} * $account{$category}{ml};
372 push(@{ $form->{"$account{$category}{label}_last_period"} },
373 $form->format_amount(
375 $form->{$category}{$key}{last} * $account{$category}{ml},
376 $form->{decimalplaces}, $dash
381 $str = ($form->{l_heading}) ? $form->{padding} : "";
382 if ($account{$category}{subtotal} && $form->{l_subtotal}) {
383 push(@{ $form->{"$account{$category}{label}_account"} },
384 "$str$form->{bold}$account{$category}{subdescription}$form->{endbold}"
386 push(@{ $form->{"$account{$category}{label}_this_period"} },
387 $form->format_amount(
389 $account{$category}{subthis} * $account{$category}{ml},
390 $form->{decimalplaces}, $dash
394 push(@{ $form->{"$account{$category}{label}_last_period"} },
395 $form->format_amount(
397 $account{$category}{sublast} * $account{$category}{ml},
398 $form->{decimalplaces}, $dash
405 # totals for assets, liabilities
406 $form->{total_assets_this_period} =
407 $form->round_amount($form->{total_assets_this_period},
408 $form->{decimalplaces});
409 $form->{total_liabilities_this_period} =
410 $form->round_amount($form->{total_liabilities_this_period},
411 $form->{decimalplaces});
412 $form->{total_equity_this_period} =
413 $form->round_amount($form->{total_equity_this_period},
414 $form->{decimalplaces});
417 $form->{earnings_this_period} =
418 $form->{total_assets_this_period} -
419 $form->{total_liabilities_this_period} - $form->{total_equity_this_period};
421 push(@{ $form->{equity_this_period} },
422 $form->format_amount($myconfig,
423 $form->{earnings_this_period},
424 $form->{decimalplaces}, "- "
427 $form->{total_equity_this_period} =
429 $form->{total_equity_this_period} + $form->{earnings_this_period},
430 $form->{decimalplaces});
432 # add liability + equity
433 $form->{total_this_period} =
434 $form->format_amount(
436 $form->{total_liabilities_this_period} + $form->{total_equity_this_period},
437 $form->{decimalplaces},
442 # totals for assets, liabilities
443 $form->{total_assets_last_period} =
444 $form->round_amount($form->{total_assets_last_period},
445 $form->{decimalplaces});
446 $form->{total_liabilities_last_period} =
447 $form->round_amount($form->{total_liabilities_last_period},
448 $form->{decimalplaces});
449 $form->{total_equity_last_period} =
450 $form->round_amount($form->{total_equity_last_period},
451 $form->{decimalplaces});
453 # calculate retained earnings
454 $form->{earnings_last_period} =
455 $form->{total_assets_last_period} -
456 $form->{total_liabilities_last_period} -
457 $form->{total_equity_last_period};
459 push(@{ $form->{equity_last_period} },
460 $form->format_amount($myconfig,
461 $form->{earnings_last_period},
462 $form->{decimalplaces}, "- "
465 $form->{total_equity_last_period} =
467 $form->{total_equity_last_period} + $form->{earnings_last_period},
468 $form->{decimalplaces});
470 # add liability + equity
471 $form->{total_last_period} =
472 $form->format_amount($myconfig,
473 $form->{total_liabilities_last_period} +
474 $form->{total_equity_last_period},
475 $form->{decimalplaces},
480 $form->{total_liabilities_last_period} =
481 $form->format_amount($myconfig,
482 $form->{total_liabilities_last_period},
483 $form->{decimalplaces}, "- ")
484 if ($form->{total_liabilities_last_period} != 0);
486 $form->{total_equity_last_period} =
487 $form->format_amount($myconfig,
488 $form->{total_equity_last_period},
489 $form->{decimalplaces}, "- ")
490 if ($form->{total_equity_last_period} != 0);
492 $form->{total_assets_last_period} =
493 $form->format_amount($myconfig,
494 $form->{total_assets_last_period},
495 $form->{decimalplaces}, "- ")
496 if ($form->{total_assets_last_period} != 0);
498 $form->{total_assets_this_period} =
499 $form->format_amount($myconfig,
500 $form->{total_assets_this_period},
501 $form->{decimalplaces}, "- ");
503 $form->{total_liabilities_this_period} =
504 $form->format_amount($myconfig,
505 $form->{total_liabilities_this_period},
506 $form->{decimalplaces}, "- ");
508 $form->{total_equity_this_period} =
509 $form->format_amount($myconfig,
510 $form->{total_equity_this_period},
511 $form->{decimalplaces}, "- ");
513 $main::lxdebug->leave_sub();
517 $main::lxdebug->enter_sub();
519 my ($dbh, $last_period, $fromdate, $todate, $form, $categories) = @_;
521 my ($null, $department_id) = split /--/, $form->{department};
532 my $category = "AND (";
533 foreach $item (@{$categories}) {
534 $category .= qq|c.category = '$item' OR |;
536 $category =~ s/OR $/\)/;
539 $query = qq|SELECT c.accno, c.description, c.category
541 WHERE c.charttype = 'H'
545 if ($form->{accounttype} eq 'gifi') {
546 $query = qq|SELECT g.accno, g.description, c.category
548 JOIN chart c ON (c.gifi_accno = g.accno)
549 WHERE c.charttype = 'H'
554 $sth = $dbh->prepare($query);
555 $sth->execute || $form->dberror($query);
557 my @headingaccounts = ();
558 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
559 $form->{ $ref->{category} }{ $ref->{accno} }{description} =
560 "$ref->{description}";
561 $form->{ $ref->{category} }{ $ref->{accno} }{charttype} = "H";
562 $form->{ $ref->{category} }{ $ref->{accno} }{accno} = $ref->{accno};
564 push @headingaccounts, $ref->{accno};
570 if ($form->{method} eq 'cash') {
571 $subwhere .= " AND transdate >= '$fromdate'";
572 $glwhere = " AND ac.transdate >= '$fromdate'";
574 $where .= " AND ac.transdate >= '$fromdate'";
579 $where .= " AND ac.transdate <= '$todate'";
580 $subwhere .= " AND transdate <= '$todate'";
583 if ($department_id) {
585 JOIN department t ON (a.department_id = t.id)
588 AND t.id = $department_id
592 if ($form->{project_id}) {
594 AND ac.project_id = $form->{project_id}
598 if ($form->{accounttype} eq 'gifi') {
600 if ($form->{method} eq 'cash') {
604 SELECT g.accno, sum(ac.amount) AS amount,
605 g.description, c.category
607 JOIN chart c ON (c.id = ac.chart_id)
608 JOIN ar a ON (a.id = ac.trans_id)
609 JOIN gifi g ON (g.accno = c.gifi_accno)
618 JOIN chart ON (chart_id = id)
619 WHERE link LIKE '%AR_paid%'
623 GROUP BY g.accno, g.description, c.category
627 SELECT '' AS accno, SUM(ac.amount) AS amount,
628 '' AS description, c.category
630 JOIN chart c ON (c.id = ac.chart_id)
631 JOIN ar a ON (a.id = ac.trans_id)
636 AND c.gifi_accno = ''
641 JOIN chart ON (chart_id = id)
642 WHERE link LIKE '%AR_paid%'
650 SELECT g.accno, sum(ac.amount) AS amount,
651 g.description, c.category
653 JOIN chart c ON (c.id = ac.chart_id)
654 JOIN ap a ON (a.id = ac.trans_id)
655 JOIN gifi g ON (g.accno = c.gifi_accno)
664 JOIN chart ON (chart_id = id)
665 WHERE link LIKE '%AP_paid%'
669 GROUP BY g.accno, g.description, c.category
673 SELECT '' AS accno, SUM(ac.amount) AS amount,
674 '' AS description, c.category
676 JOIN chart c ON (c.id = ac.chart_id)
677 JOIN ap a ON (a.id = ac.trans_id)
682 AND c.gifi_accno = ''
687 JOIN chart ON (chart_id = id)
688 WHERE link LIKE '%AP_paid%'
698 SELECT g.accno, sum(ac.amount) AS amount,
699 g.description, c.category
701 JOIN chart c ON (c.id = ac.chart_id)
702 JOIN gifi g ON (g.accno = c.gifi_accno)
703 JOIN gl a ON (a.id = ac.trans_id)
709 AND NOT (c.link = 'AR' OR c.link = 'AP')
711 GROUP BY g.accno, g.description, c.category
715 SELECT '' AS accno, SUM(ac.amount) AS amount,
716 '' AS description, c.category
718 JOIN chart c ON (c.id = ac.chart_id)
719 JOIN gl a ON (a.id = ac.trans_id)
725 AND c.gifi_accno = ''
726 AND NOT (c.link = 'AR' OR c.link = 'AP')
731 if ($form->{project_id}) {
737 SELECT g.accno AS accno, SUM(ac.sellprice * ac.qty) AS amount,
738 g.description AS description, c.category
740 JOIN ar a ON (a.id = ac.trans_id)
741 JOIN parts p ON (ac.parts_id = p.id)
742 JOIN chart c on (p.income_accno_id = c.id)
743 JOIN gifi g ON (g.accno = c.gifi_accno)
745 -- use transdate from subwhere
746 WHERE 1 = 1 $subwhere
753 JOIN chart ON (chart_id = id)
754 WHERE link LIKE '%AR_paid%'
758 GROUP BY g.accno, g.description, c.category
762 SELECT g.accno AS accno, SUM(ac.sellprice * ac.qty) * -1 AS amount,
763 g.description AS description, c.category
765 JOIN ap a ON (a.id = ac.trans_id)
766 JOIN parts p ON (ac.parts_id = p.id)
767 JOIN chart c on (p.expense_accno_id = c.id)
768 JOIN gifi g ON (g.accno = c.gifi_accno)
770 WHERE 1 = 1 $subwhere
777 JOIN chart ON (chart_id = id)
778 WHERE link LIKE '%AP_paid%'
782 GROUP BY g.accno, g.description, c.category
788 if ($department_id) {
790 JOIN dpt_trans t ON (t.trans_id = ac.trans_id)
793 AND t.department_id = $department_id
800 SELECT g.accno, SUM(ac.amount) AS amount,
801 g.description, c.category
803 JOIN chart c ON (c.id = ac.chart_id)
804 JOIN gifi g ON (c.gifi_accno = g.accno)
810 GROUP BY g.accno, g.description, c.category
814 SELECT '' AS accno, SUM(ac.amount) AS amount,
815 '' AS description, c.category
817 JOIN chart c ON (c.id = ac.chart_id)
822 AND c.gifi_accno = ''
827 if ($form->{project_id}) {
833 SELECT g.accno AS accno, SUM(ac.sellprice * ac.qty) AS amount,
834 g.description AS description, c.category
836 JOIN ar a ON (a.id = ac.trans_id)
837 JOIN parts p ON (ac.parts_id = p.id)
838 JOIN chart c on (p.income_accno_id = c.id)
839 JOIN gifi g ON (c.gifi_accno = g.accno)
841 -- use transdate from subwhere
842 WHERE 1 = 1 $subwhere
846 GROUP BY g.accno, g.description, c.category
850 SELECT g.accno AS accno, SUM(ac.sellprice * ac.qty) * -1 AS amount,
851 g.description AS description, c.category
853 JOIN ap a ON (a.id = ac.trans_id)
854 JOIN parts p ON (ac.parts_id = p.id)
855 JOIN chart c on (p.expense_accno_id = c.id)
856 JOIN gifi g ON (c.gifi_accno = g.accno)
858 WHERE 1 = 1 $subwhere
862 GROUP BY g.accno, g.description, c.category
868 } else { # standard account
870 if ($form->{method} eq 'cash') {
874 SELECT c.accno, sum(ac.amount) AS amount,
875 c.description, c.category
877 JOIN chart c ON (c.id = ac.chart_id)
878 JOIN ar a ON (a.id = ac.trans_id)
887 JOIN chart ON (chart_id = id)
888 WHERE link LIKE '%AR_paid%'
893 GROUP BY c.accno, c.description, c.category
897 SELECT c.accno, sum(ac.amount) AS amount,
898 c.description, c.category
900 JOIN chart c ON (c.id = ac.chart_id)
901 JOIN ap a ON (a.id = ac.trans_id)
910 JOIN chart ON (chart_id = id)
911 WHERE link LIKE '%AP_paid%'
916 GROUP BY c.accno, c.description, c.category
920 SELECT c.accno, sum(ac.amount) AS amount,
921 c.description, c.category
923 JOIN chart c ON (c.id = ac.chart_id)
924 JOIN gl a ON (a.id = ac.trans_id)
930 AND NOT (c.link = 'AR' OR c.link = 'AP')
932 GROUP BY c.accno, c.description, c.category
935 if ($form->{project_id}) {
941 SELECT c.accno AS accno, SUM(ac.sellprice * ac.qty) AS amount,
942 c.description AS description, c.category
944 JOIN ar a ON (a.id = ac.trans_id)
945 JOIN parts p ON (ac.parts_id = p.id)
946 JOIN chart c on (p.income_accno_id = c.id)
948 -- use transdate from subwhere
949 WHERE 1 = 1 $subwhere
956 JOIN chart ON (chart_id = id)
957 WHERE link LIKE '%AR_paid%'
962 GROUP BY c.accno, c.description, c.category
966 SELECT c.accno AS accno, SUM(ac.sellprice) AS amount,
967 c.description AS description, c.category
969 JOIN ap a ON (a.id = ac.trans_id)
970 JOIN parts p ON (ac.parts_id = p.id)
971 JOIN chart c on (p.expense_accno_id = c.id)
973 WHERE 1 = 1 $subwhere
980 JOIN chart ON (chart_id = id)
981 WHERE link LIKE '%AP_paid%'
986 GROUP BY c.accno, c.description, c.category
992 if ($department_id) {
994 JOIN dpt_trans t ON (t.trans_id = ac.trans_id)
997 AND t.department_id = $department_id
1003 SELECT c.accno, sum(ac.amount) AS amount,
1004 c.description, c.category
1006 JOIN chart c ON (c.id = ac.chart_id)
1012 GROUP BY c.accno, c.description, c.category
1015 if ($form->{project_id}) {
1021 SELECT c.accno AS accno, SUM(ac.sellprice * ac.qty) AS amount,
1022 c.description AS description, c.category
1024 JOIN ar a ON (a.id = ac.trans_id)
1025 JOIN parts p ON (ac.parts_id = p.id)
1026 JOIN chart c on (p.income_accno_id = c.id)
1028 -- use transdate from subwhere
1029 WHERE 1 = 1 $subwhere
1030 AND c.category = 'I'
1033 GROUP BY c.accno, c.description, c.category
1037 SELECT c.accno AS accno, SUM(ac.sellprice * ac.qty) * -1 AS amount,
1038 c.description AS description, c.category
1040 JOIN ap a ON (a.id = ac.trans_id)
1041 JOIN parts p ON (ac.parts_id = p.id)
1042 JOIN chart c on (p.expense_accno_id = c.id)
1044 WHERE 1 = 1 $subwhere
1045 AND c.category = 'E'
1048 GROUP BY c.accno, c.description, c.category
1059 my $sth = $dbh->prepare($query);
1060 $sth->execute || $form->dberror($query);
1062 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1064 if ($ref->{category} eq 'C') {
1065 $ref->{category} = 'A';
1068 # get last heading account
1069 @accno = grep { $_ le "$ref->{accno}" } @headingaccounts;
1070 $accno = pop @accno;
1073 $form->{ $ref->{category} }{$accno}{last} += $ref->{amount};
1075 $form->{ $ref->{category} }{$accno}{this} += $ref->{amount};
1079 $form->{ $ref->{category} }{ $ref->{accno} }{accno} = $ref->{accno};
1080 $form->{ $ref->{category} }{ $ref->{accno} }{description} =
1081 $ref->{description};
1082 $form->{ $ref->{category} }{ $ref->{accno} }{charttype} = "A";
1085 $form->{ $ref->{category} }{ $ref->{accno} }{last} += $ref->{amount};
1087 $form->{ $ref->{category} }{ $ref->{accno} }{this} += $ref->{amount};
1092 # remove accounts with zero balance
1093 foreach $category (@{$categories}) {
1094 foreach $accno (keys %{ $form->{$category} }) {
1095 $form->{$category}{$accno}{last} =
1096 $form->round_amount($form->{$category}{$accno}{last},
1097 $form->{decimalplaces});
1098 $form->{$category}{$accno}{this} =
1099 $form->round_amount($form->{$category}{$accno}{this},
1100 $form->{decimalplaces});
1102 delete $form->{$category}{$accno}
1103 if ( $form->{$category}{$accno}{this} == 0
1104 && $form->{$category}{$accno}{last} == 0);
1108 $main::lxdebug->leave_sub();
1111 sub get_accounts_g {
1112 $main::lxdebug->enter_sub();
1114 my ($dbh, $last_period, $fromdate, $todate, $form, $category) = @_;
1116 my ($null, $department_id) = split /--/, $form->{department};
1122 my $where = "1 = 1";
1128 if ($form->{method} eq 'cash') {
1129 $subwhere .= " AND transdate >= '$fromdate'";
1130 $glwhere = " AND ac.transdate >= '$fromdate'";
1132 $where .= " AND ac.transdate >= '$fromdate'";
1137 $where .= " AND ac.transdate <= '$todate'";
1138 $subwhere .= " AND transdate <= '$todate'";
1141 if ($department_id) {
1143 JOIN department t ON (a.department_id = t.id)
1146 AND t.id = $department_id
1150 if ($form->{project_id}) {
1152 AND ac.project_id = $form->{project_id}
1156 if ($form->{method} eq 'cash') {
1160 SELECT sum(ac.amount) AS amount,
1163 JOIN chart c ON (c.id = ac.chart_id)
1164 JOIN ar a ON (a.id = ac.trans_id)
1172 JOIN chart ON (chart_id = id)
1173 WHERE link LIKE '%AR_paid%'
1178 GROUP BY c.$category
1182 SELECT sum(ac.amount) AS amount,
1185 JOIN chart c ON (c.id = ac.chart_id)
1186 JOIN ap a ON (a.id = ac.trans_id)
1194 JOIN chart ON (chart_id = id)
1195 WHERE link LIKE '%AP_paid%'
1200 GROUP BY c.$category
1204 SELECT sum(ac.amount) AS amount,
1207 JOIN chart c ON (c.id = ac.chart_id)
1208 JOIN gl a ON (a.id = ac.trans_id)
1213 AND NOT (c.link = 'AR' OR c.link = 'AP')
1215 GROUP BY c.$category
1218 if ($form->{project_id}) {
1224 SELECT SUM(ac.sellprice * ac.qty) AS amount,
1227 JOIN ar a ON (a.id = ac.trans_id)
1228 JOIN parts p ON (ac.parts_id = p.id)
1229 JOIN chart c on (p.income_accno_id = c.id)
1231 -- use transdate from subwhere
1232 WHERE 1 = 1 $subwhere
1233 AND c.category = 'I'
1239 JOIN chart ON (chart_id = id)
1240 WHERE link LIKE '%AR_paid%'
1245 GROUP BY c.$category
1249 SELECT SUM(ac.sellprice) AS amount,
1252 JOIN ap a ON (a.id = ac.trans_id)
1253 JOIN parts p ON (ac.parts_id = p.id)
1254 JOIN chart c on (p.expense_accno_id = c.id)
1256 WHERE 1 = 1 $subwhere
1257 AND c.category = 'E'
1263 JOIN chart ON (chart_id = id)
1264 WHERE link LIKE '%AP_paid%'
1269 GROUP BY c.$category
1275 if ($department_id) {
1277 JOIN dpt_trans t ON (t.trans_id = ac.trans_id)
1280 AND t.department_id = $department_id
1286 SELECT sum(ac.amount) AS amount,
1289 JOIN chart c ON (c.id = ac.chart_id)
1294 GROUP BY c.$category
1297 if ($form->{project_id}) {
1303 SELECT SUM(ac.sellprice * ac.qty) AS amount,
1306 JOIN ar a ON (a.id = ac.trans_id)
1307 JOIN parts p ON (ac.parts_id = p.id)
1308 JOIN chart c on (p.income_accno_id = c.id)
1310 -- use transdate from subwhere
1311 WHERE 1 = 1 $subwhere
1312 AND c.category = 'I'
1315 GROUP BY c.$category
1319 SELECT SUM(ac.sellprice * ac.qty) * -1 AS amount,
1322 JOIN ap a ON (a.id = ac.trans_id)
1323 JOIN parts p ON (ac.parts_id = p.id)
1324 JOIN chart c on (p.expense_accno_id = c.id)
1326 WHERE 1 = 1 $subwhere
1327 AND c.category = 'E'
1330 GROUP BY c.$category
1340 my $sth = $dbh->prepare($query);
1341 $sth->execute || $form->dberror($query);
1343 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1344 if ($ref->{amount} < 0) {
1345 $ref->{amount} *= -1;
1347 if ($category eq "pos_bwa") {
1349 $form->{ $ref->{$category} }{kumm} += $ref->{amount};
1351 $form->{ $ref->{$category} }{jetzt} += $ref->{amount};
1354 $form->{ $ref->{$category} } += $ref->{amount};
1359 $main::lxdebug->leave_sub();
1363 $main::lxdebug->enter_sub();
1365 my ($self, $myconfig, $form) = @_;
1367 my $dbh = $form->dbconnect($myconfig);
1369 my ($query, $sth, $ref);
1372 my ($null, $department_id) = split /--/, $form->{department};
1373 my @headingaccounts = ();
1378 my $where = "1 = 1";
1379 my $invwhere = $where;
1381 if ($department_id) {
1383 JOIN dpt_trans t ON (ac.trans_id = t.trans_id)
1386 AND t.department_id = $department_id
1390 # project_id only applies to getting transactions
1391 # it has nothing to do with a trial balance
1392 # but we use the same function to collect information
1394 if ($form->{project_id}) {
1396 AND ac.project_id = $form->{project_id}
1400 # get beginning balances
1401 if ($form->{fromdate}) {
1403 if ($form->{accounttype} eq 'gifi') {
1405 $query = qq|SELECT g.accno, c.category, SUM(ac.amount) AS amount,
1408 JOIN chart c ON (ac.chart_id = c.id)
1409 JOIN gifi g ON (c.gifi_accno = g.accno)
1411 WHERE ac.transdate < '$form->{fromdate}'
1414 GROUP BY g.accno, c.category, g.description
1419 $query = qq|SELECT c.accno, c.category, SUM(ac.amount) AS amount,
1422 JOIN chart c ON (ac.chart_id = c.id)
1424 WHERE ac.transdate < '$form->{fromdate}'
1427 GROUP BY c.accno, c.category, c.description
1432 $sth = $dbh->prepare($query);
1433 $sth->execute || $form->dberror($query);
1435 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1436 $balance{ $ref->{accno} } = $ref->{amount};
1438 if ($ref->{amount} != 0 && $form->{all_accounts}) {
1439 $trb{ $ref->{accno} }{description} = $ref->{description};
1440 $trb{ $ref->{accno} }{charttype} = 'A';
1441 $trb{ $ref->{accno} }{category} = $ref->{category};
1450 $query = qq|SELECT c.accno, c.description, c.category
1452 WHERE c.charttype = 'H'
1455 if ($form->{accounttype} eq 'gifi') {
1456 $query = qq|SELECT g.accno, g.description, c.category
1458 JOIN chart c ON (c.gifi_accno = g.accno)
1459 WHERE c.charttype = 'H'
1463 $sth = $dbh->prepare($query);
1464 $sth->execute || $form->dberror($query);
1466 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1467 $trb{ $ref->{accno} }{description} = $ref->{description};
1468 $trb{ $ref->{accno} }{charttype} = 'H';
1469 $trb{ $ref->{accno} }{category} = $ref->{category};
1471 push @headingaccounts, $ref->{accno};
1478 if ($form->{fromdate} || $form->{todate}) {
1479 if ($form->{fromdate}) {
1480 $tofrom .= " AND ac.transdate >= '$form->{fromdate}'";
1481 $subwhere .= " AND transdate >= '$form->{fromdate}'";
1482 $invwhere .= " AND a.transdate >= '$form->{fromdate}'";
1483 $glwhere = " AND ac.transdate >= '$form->{fromdate}'";
1485 if ($form->{todate}) {
1486 $tofrom .= " AND ac.transdate <= '$form->{todate}'";
1487 $invwhere .= " AND a.transdate <= '$form->{todate}'";
1488 $subwhere .= " AND transdate <= '$form->{todate}'";
1489 $glwhere .= " AND ac.transdate <= '$form->{todate}'";
1493 $where .= qq| AND ((ac.trans_id in (SELECT id from ar)
1498 JOIN chart ON (chart_id = id)
1499 WHERE link LIKE '%AR_paid%'
1501 )) OR (ac.trans_id in (SELECT id from ap)
1506 JOIN chart ON (chart_id = id)
1507 WHERE link LIKE '%AP_paid%'
1509 )) OR (ac.trans_id in (SELECT id from gl)
1515 if ($form->{accounttype} eq 'gifi') {
1517 $query = qq|SELECT g.accno, g.description, c.category,
1518 SUM(ac.amount) AS amount
1520 JOIN chart c ON (c.id = ac.chart_id)
1521 JOIN gifi g ON (c.gifi_accno = g.accno)
1526 GROUP BY g.accno, g.description, c.category
1529 if ($form->{project_id}) {
1533 -- add project transactions from invoice
1537 SELECT g.accno, g.description, c.category,
1538 SUM(ac.sellprice * ac.qty) AS amount
1540 JOIN ar a ON (ac.trans_id = a.id)
1541 JOIN parts p ON (ac.parts_id = p.id)
1542 JOIN chart c ON (p.income_accno_id = c.id)
1543 JOIN gifi g ON (c.gifi_accno = g.accno)
1548 GROUP BY g.accno, g.description, c.category
1552 SELECT g.accno, g.description, c.category,
1553 SUM(ac.sellprice * ac.qty) * -1 AS amount
1555 JOIN ap a ON (ac.trans_id = a.id)
1556 JOIN parts p ON (ac.parts_id = p.id)
1557 JOIN chart c ON (p.expense_accno_id = c.id)
1558 JOIN gifi g ON (c.gifi_accno = g.accno)
1563 GROUP BY g.accno, g.description, c.category
1572 $query = qq|SELECT c.accno, c.description, c.category,
1573 SUM(ac.amount) AS amount
1575 JOIN chart c ON (c.id = ac.chart_id)
1580 GROUP BY c.accno, c.description, c.category
1583 if ($form->{project_id}) {
1587 -- add project transactions from invoice
1591 SELECT c.accno, c.description, c.category,
1592 SUM(ac.sellprice * ac.qty) AS amount
1594 JOIN ar a ON (ac.trans_id = a.id)
1595 JOIN parts p ON (ac.parts_id = p.id)
1596 JOIN chart c ON (p.income_accno_id = c.id)
1601 GROUP BY c.accno, c.description, c.category
1605 SELECT c.accno, c.description, c.category,
1606 SUM(ac.sellprice * ac.qty) * -1 AS amount
1608 JOIN ap a ON (ac.trans_id = a.id)
1609 JOIN parts p ON (ac.parts_id = p.id)
1610 JOIN chart c ON (p.expense_accno_id = c.id)
1615 GROUP BY c.accno, c.description, c.category
1624 $sth = $dbh->prepare($query);
1625 $sth->execute || $form->dberror($query);
1627 # prepare query for each account
1628 $query = qq|SELECT (SELECT SUM(ac.amount) * -1
1630 JOIN chart c ON (c.id = ac.chart_id)
1636 AND c.accno = ?) AS debit,
1638 (SELECT SUM(ac.amount)
1640 JOIN chart c ON (c.id = ac.chart_id)
1646 AND c.accno = ?) AS credit
1649 if ($form->{accounttype} eq 'gifi') {
1651 $query = qq|SELECT (SELECT SUM(ac.amount) * -1
1653 JOIN chart c ON (c.id = ac.chart_id)
1659 AND c.gifi_accno = ?) AS debit,
1661 (SELECT SUM(ac.amount)
1663 JOIN chart c ON (c.id = ac.chart_id)
1669 AND c.gifi_accno = ?) AS credit|;
1673 $drcr = $dbh->prepare($query);
1675 if ($form->{project_id}) {
1677 # prepare query for each account
1678 $query = qq|SELECT (SELECT SUM(ac.sellprice * ac.qty) * -1
1680 JOIN parts p ON (ac.parts_id = p.id)
1681 JOIN ap a ON (ac.trans_id = a.id)
1682 JOIN chart c ON (p.expense_accno_id = c.id)
1687 AND c.accno = ?) AS debit,
1689 (SELECT SUM(ac.sellprice * ac.qty)
1691 JOIN parts p ON (ac.parts_id = p.id)
1692 JOIN ar a ON (ac.trans_id = a.id)
1693 JOIN chart c ON (p.income_accno_id = c.id)
1698 AND c.accno = ?) AS credit
1701 $project_drcr = $dbh->prepare($query);
1705 # calculate the debit and credit in the period
1706 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1707 $trb{ $ref->{accno} }{description} = $ref->{description};
1708 $trb{ $ref->{accno} }{charttype} = 'A';
1709 $trb{ $ref->{accno} }{category} = $ref->{category};
1710 $trb{ $ref->{accno} }{amount} += $ref->{amount};
1714 my ($debit, $credit);
1716 foreach my $accno (sort keys %trb) {
1719 $ref->{accno} = $accno;
1720 map { $ref->{$_} = $trb{$accno}{$_} }
1721 qw(description category charttype amount);
1723 $ref->{balance} = $form->round_amount($balance{ $ref->{accno} }, 2);
1725 if ($trb{$accno}{charttype} eq 'A') {
1728 $drcr->execute($ref->{accno}, $ref->{accno}) || $form->dberror($query);
1730 ($debit, $credit) = (0, 0);
1731 while (($debit, $credit) = $drcr->fetchrow_array) {
1732 $ref->{debit} += $debit;
1733 $ref->{credit} += $credit;
1737 if ($form->{project_id}) {
1740 $project_drcr->execute($ref->{accno}, $ref->{accno})
1741 || $form->dberror($query);
1743 ($debit, $credit) = (0, 0);
1744 while (($debit, $credit) = $project_drcr->fetchrow_array) {
1745 $ref->{debit} += $debit;
1746 $ref->{credit} += $credit;
1748 $project_drcr->finish;
1751 $ref->{debit} = $form->round_amount($ref->{debit}, 2);
1752 $ref->{credit} = $form->round_amount($ref->{credit}, 2);
1757 @accno = grep { $_ le "$ref->{accno}" } @headingaccounts;
1758 $accno = pop @accno;
1760 $trb{$accno}{debit} += $ref->{debit};
1761 $trb{$accno}{credit} += $ref->{credit};
1764 push @{ $form->{TB} }, $ref;
1770 # debits and credits for headings
1771 foreach $accno (@headingaccounts) {
1772 foreach $ref (@{ $form->{TB} }) {
1773 if ($accno eq $ref->{accno}) {
1774 $ref->{debit} = $trb{$accno}{debit};
1775 $ref->{credit} = $trb{$accno}{credit};
1780 $main::lxdebug->leave_sub();
1784 $main::lxdebug->enter_sub();
1786 my ($self, $myconfig, $form) = @_;
1788 # connect to database
1789 my $dbh = $form->dbconnect($myconfig);
1790 my $invoice = ($form->{arap} eq 'ar') ? 'is' : 'ir';
1792 $form->{todate} = $form->current_date($myconfig) unless ($form->{todate});
1794 my $where = "1 = 1";
1797 if ($form->{"$form->{ct}_id"}) {
1798 $where .= qq| AND ct.id = $form->{"$form->{ct}_id"}|;
1800 if ($form->{ $form->{ct} }) {
1801 $name = $form->like(lc $form->{ $form->{ct} });
1802 $where .= qq| AND lower(ct.name) LIKE '$name'| if $form->{ $form->{ct} };
1807 if ($form->{department}) {
1808 ($null, $department_id) = split /--/, $form->{department};
1810 JOIN department d ON (a.department_id = d.id)
1813 $where .= qq| AND a.department_id = $department_id|;
1816 # select outstanding vendors or customers, depends on $ct
1817 my $query = qq|SELECT DISTINCT ct.id, ct.name
1818 FROM $form->{ct} ct, $form->{arap} a
1821 AND a.$form->{ct}_id = ct.id
1822 AND a.paid != a.amount
1823 AND (a.transdate <= '$form->{todate}')
1826 my $sth = $dbh->prepare($query);
1827 $sth->execute || $form->dberror;
1829 my $buysell = ($form->{arap} eq 'ar') ? 'buy' : 'sell';
1831 # for each company that has some stuff outstanding
1832 while (my ($id) = $sth->fetchrow_array) {
1836 -- between 0-30 days
1838 SELECT $form->{ct}.id AS ctid, $form->{ct}.name,
1839 street, zipcode, city, country, contact, email,
1840 phone as customerphone, fax as customerfax, $form->{ct}number,
1841 "invnumber", "transdate",
1842 (amount - paid) as "c0", 0.00 as "c30", 0.00 as "c60", 0.00 as "c90",
1843 "duedate", invoice, $form->{arap}.id,
1844 (SELECT $buysell FROM exchangerate
1845 WHERE $form->{arap}.curr = exchangerate.curr
1846 AND exchangerate.transdate = $form->{arap}.transdate) AS exchangerate
1847 FROM $form->{arap}, $form->{ct}
1848 WHERE paid != amount
1849 AND $form->{arap}.$form->{ct}_id = $form->{ct}.id
1850 AND $form->{ct}.id = $id
1852 transdate <= (date '$form->{todate}' - interval '0 days')
1853 AND transdate >= (date '$form->{todate}' - interval '30 days')
1858 -- between 31-60 days
1860 SELECT $form->{ct}.id AS ctid, $form->{ct}.name,
1861 street, zipcode, city, country, contact, email,
1862 phone as customerphone, fax as customerfax, $form->{ct}number,
1863 "invnumber", "transdate",
1864 0.00 as "c0", (amount - paid) as "c30", 0.00 as "c60", 0.00 as "c90",
1865 "duedate", invoice, $form->{arap}.id,
1866 (SELECT $buysell FROM exchangerate
1867 WHERE $form->{arap}.curr = exchangerate.curr
1868 AND exchangerate.transdate = $form->{arap}.transdate) AS exchangerate
1869 FROM $form->{arap}, $form->{ct}
1870 WHERE paid != amount
1871 AND $form->{arap}.$form->{ct}_id = $form->{ct}.id
1872 AND $form->{ct}.id = $id
1874 transdate < (date '$form->{todate}' - interval '30 days')
1875 AND transdate >= (date '$form->{todate}' - interval '60 days')
1880 -- between 61-90 days
1882 SELECT $form->{ct}.id AS ctid, $form->{ct}.name,
1883 street, zipcode, city, country, contact, email,
1884 phone as customerphone, fax as customerfax, $form->{ct}number,
1885 "invnumber", "transdate",
1886 0.00 as "c0", 0.00 as "c30", (amount - paid) as "c60", 0.00 as "c90",
1887 "duedate", invoice, $form->{arap}.id,
1888 (SELECT $buysell FROM exchangerate
1889 WHERE $form->{arap}.curr = exchangerate.curr
1890 AND exchangerate.transdate = $form->{arap}.transdate) AS exchangerate
1891 FROM $form->{arap}, $form->{ct}
1892 WHERE paid != amount
1893 AND $form->{arap}.$form->{ct}_id = $form->{ct}.id
1894 AND $form->{ct}.id = $id
1896 transdate < (date '$form->{todate}' - interval '60 days')
1897 AND transdate >= (date '$form->{todate}' - interval '90 days')
1904 SELECT $form->{ct}.id AS ctid, $form->{ct}.name,
1905 street, zipcode, city, country, contact, email,
1906 phone as customerphone, fax as customerfax, $form->{ct}number,
1907 "invnumber", "transdate",
1908 0.00 as "c0", 0.00 as "c30", 0.00 as "c60", (amount - paid) as "c90",
1909 "duedate", invoice, $form->{arap}.id,
1910 (SELECT $buysell FROM exchangerate
1911 WHERE $form->{arap}.curr = exchangerate.curr
1912 AND exchangerate.transdate = $form->{arap}.transdate) AS exchangerate
1913 FROM $form->{arap}, $form->{ct}
1914 WHERE paid != amount
1915 AND $form->{arap}.$form->{ct}_id = $form->{ct}.id
1916 AND $form->{ct}.id = $id
1917 AND transdate < (date '$form->{todate}' - interval '90 days')
1921 ctid, transdate, invnumber
1925 my $sth = $dbh->prepare($query);
1926 $sth->execute || $form->dberror;
1928 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1929 $ref->{module} = ($ref->{invoice}) ? $invoice : $form->{arap};
1930 $ref->{exchangerate} = 1 unless $ref->{exchangerate};
1931 push @{ $form->{AG} }, $ref;
1943 $main::lxdebug->leave_sub();
1947 $main::lxdebug->enter_sub();
1949 my ($self, $myconfig, $form) = @_;
1951 # connect to database
1952 my $dbh = $form->dbconnect($myconfig);
1954 my $query = qq|SELECT ct.name, ct.email, ct.cc, ct.bcc
1956 WHERE ct.id = $form->{"$form->{ct}_id"}|;
1957 my $sth = $dbh->prepare($query);
1958 $sth->execute || $form->dberror;
1960 ($form->{ $form->{ct} }, $form->{email}, $form->{cc}, $form->{bcc}) =
1961 $sth->fetchrow_array;
1965 $main::lxdebug->leave_sub();
1968 sub get_taxaccounts {
1969 $main::lxdebug->enter_sub();
1971 my ($self, $myconfig, $form) = @_;
1973 # connect to database
1974 my $dbh = $form->dbconnect($myconfig);
1977 my $query = qq|SELECT c.accno, c.description, t.rate
1979 WHERE c.link LIKE '%CT_tax%'
1980 AND c.id = t.chart_id
1982 my $sth = $dbh->prepare($query);
1983 $sth->execute || $form->dberror;
1986 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1987 push @{ $form->{taxaccounts} }, $ref;
1991 # get gifi tax accounts
1992 my $query = qq|SELECT DISTINCT ON (g.accno) g.accno, g.description,
1994 FROM gifi g, chart c, tax t
1995 WHERE g.accno = c.gifi_accno
1996 AND c.id = t.chart_id
1997 AND c.link LIKE '%CT_tax%'
1998 GROUP BY g.accno, g.description
2000 my $sth = $dbh->prepare($query);
2001 $sth->execute || $form->dberror;
2003 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
2004 push @{ $form->{gifi_taxaccounts} }, $ref;
2010 $main::lxdebug->leave_sub();
2014 $main::lxdebug->enter_sub();
2016 my ($self, $myconfig, $form) = @_;
2018 # connect to database
2019 my $dbh = $form->dbconnect($myconfig);
2021 my ($null, $department_id) = split /--/, $form->{department};
2024 my $where = "1 = 1";
2026 if ($department_id) {
2028 AND a.department_id = $department_id
2034 if ($form->{accno}) {
2035 if ($form->{accno} =~ /^gifi_/) {
2036 ($null, $accno) = split /_/, $form->{accno};
2037 $rate = $form->{"$form->{accno}_rate"};
2038 $accno = qq| AND ch.gifi_accno = '$accno'|;
2040 $accno = $form->{accno};
2041 $rate = $form->{"$form->{accno}_rate"};
2042 $accno = qq| AND ch.accno = '$accno'|;
2049 if ($form->{db} eq 'ar') {
2050 $table = "customer";
2053 if ($form->{db} eq 'ap') {
2058 my $transdate = "a.transdate";
2060 if ($form->{method} eq 'cash') {
2061 $transdate = "a.datepaid";
2064 ($form->{todate}) ? $form->{todate} : $form->current_date($myconfig);
2071 JOIN chart ON (chart_id = id)
2072 WHERE link LIKE '%${ARAP}_paid%'
2073 AND transdate <= '$todate'
2078 # if there are any dates construct a where
2079 if ($form->{fromdate} || $form->{todate}) {
2080 if ($form->{fromdate}) {
2081 $where .= " AND $transdate >= '$form->{fromdate}'";
2083 if ($form->{todate}) {
2084 $where .= " AND $transdate <= '$form->{todate}'";
2088 my $ml = ($form->{db} eq 'ar') ? 1 : -1;
2090 my $sortorder = join ', ', $form->sort_columns(qw(transdate invnumber name));
2091 $sortorder = $form->{sort} unless $sortorder;
2093 $query = qq|SELECT a.id, '0' AS invoice, $transdate AS transdate,
2094 a.invnumber, n.name, a.netamount,
2095 ac.amount * $ml AS tax
2097 JOIN $form->{db} a ON (a.id = ac.trans_id)
2098 JOIN chart ch ON (ch.id = ac.chart_id)
2099 JOIN $table n ON (n.id = a.${table}_id)
2104 SELECT a.id, '1' AS invoice, $transdate AS transdate,
2105 a.invnumber, n.name, i.sellprice * i.qty AS netamount,
2106 i.sellprice * i.qty * $rate * $ml AS tax
2108 JOIN $form->{db} a ON (a.id = ac.trans_id)
2109 JOIN chart ch ON (ch.id = ac.chart_id)
2110 JOIN $table n ON (n.id = a.${table}_id)
2111 JOIN ${table}tax t ON (t.${table}_id = n.id)
2112 JOIN invoice i ON (i.trans_id = a.id)
2113 JOIN partstax p ON (p.parts_id = i.parts_id)
2117 ORDER by $sortorder|;
2119 if ($form->{report} =~ /nontaxable/) {
2121 # only gather up non-taxable transactions
2122 $query = qq|SELECT a.id, '0' AS invoice, $transdate AS transdate,
2123 a.invnumber, n.name, a.netamount
2125 JOIN $form->{db} a ON (a.id = ac.trans_id)
2126 JOIN $table n ON (n.id = a.${table}_id)
2129 AND a.netamount = a.amount
2131 SELECT a.id, '1' AS invoice, $transdate AS transdate,
2132 a.invnumber, n.name, i.sellprice * i.qty AS netamount
2134 JOIN $form->{db} a ON (a.id = ac.trans_id)
2135 JOIN $table n ON (n.id = a.${table}_id)
2136 JOIN invoice i ON (i.trans_id = a.id)
2140 a.${table}_id NOT IN (
2141 SELECT ${table}_id FROM ${table}tax t (${table}_id)
2144 SELECT parts_id FROM partstax p (parts_id)
2147 GROUP BY a.id, a.invnumber, $transdate, n.name, i.sellprice, i.qty
2148 ORDER by $sortorder|;
2151 my $sth = $dbh->prepare($query);
2152 $sth->execute || $form->dberror($query);
2154 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
2155 push @{ $form->{TR} }, $ref;
2161 $main::lxdebug->leave_sub();
2164 sub paymentaccounts {
2165 $main::lxdebug->enter_sub();
2167 my ($self, $myconfig, $form) = @_;
2169 # connect to database, turn AutoCommit off
2170 my $dbh = $form->dbconnect_noauto($myconfig);
2172 my $ARAP = uc $form->{db};
2174 # get A(R|P)_paid accounts
2175 my $query = qq|SELECT c.accno, c.description
2177 WHERE c.link LIKE '%${ARAP}_paid%'|;
2178 my $sth = $dbh->prepare($query);
2179 $sth->execute || $form->dberror($query);
2181 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
2182 push @{ $form->{PR} }, $ref;
2188 $main::lxdebug->leave_sub();
2192 $main::lxdebug->enter_sub();
2194 my ($self, $myconfig, $form) = @_;
2196 # connect to database, turn AutoCommit off
2197 my $dbh = $form->dbconnect_noauto($myconfig);
2200 if ($form->{db} eq 'ar') {
2201 $table = 'customer';
2204 if ($form->{db} eq 'ap') {
2212 if ($form->{department_id}) {
2214 JOIN dpt_trans t ON (t.trans_id = ac.trans_id)
2218 AND t.department_id = $form->{department_id}
2222 if ($form->{fromdate}) {
2223 $where .= " AND ac.transdate >= '$form->{fromdate}'";
2225 if ($form->{todate}) {
2226 $where .= " AND ac.transdate <= '$form->{todate}'";
2228 if (!$form->{fx_transaction}) {
2229 $where .= " AND ac.fx_transaction = '0'";
2234 if ($form->{reference}) {
2235 $reference = $form->like(lc $form->{reference});
2236 $invnumber = " AND lower(a.invnumber) LIKE '$reference'";
2237 $reference = " AND lower(g.reference) LIKE '$reference'";
2239 if ($form->{source}) {
2240 my $source = $form->like(lc $form->{source});
2241 $where .= " AND lower(ac.source) LIKE '$source'";
2243 if ($form->{memo}) {
2244 my $memo = $form->like(lc $form->{memo});
2245 $where .= " AND lower(ac.memo) LIKE '$memo'";
2248 my $sortorder = join ', ',
2249 $form->sort_columns(qw(name invnumber ordnumber transdate source));
2251 # cycle through each id
2252 foreach my $accno (split(/ /, $form->{paymentaccounts})) {
2254 $query = qq|SELECT c.id, c.accno, c.description
2256 WHERE c.accno = '$accno'|;
2257 $sth = $dbh->prepare($query);
2258 $sth->execute || $form->dberror($query);
2260 my $ref = $sth->fetchrow_hashref(NAME_lc);
2261 push @{ $form->{PR} }, $ref;
2264 $query = qq|SELECT c.name, a.invnumber, a.ordnumber,
2265 ac.transdate, ac.amount * $ml AS paid, ac.source,
2266 a.invoice, a.id, ac.memo, '$form->{db}' AS module
2268 JOIN $form->{db} a ON (ac.trans_id = a.id)
2269 JOIN $table c ON (c.id = a.${table}_id)
2271 WHERE ac.chart_id = $ref->{id}
2276 SELECT g.description, g.reference, NULL AS ordnumber,
2277 ac.transdate, ac.amount * $ml AS paid, ac.source,
2278 '0' as invoice, g.id, ac.memo, 'gl' AS module
2280 JOIN gl g ON (g.id = ac.trans_id)
2282 WHERE ac.chart_id = $ref->{id}
2285 AND (ac.amount * $ml) > 0
2286 ORDER BY $sortorder|;
2288 $sth = $dbh->prepare($query);
2289 $sth->execute || $form->dberror($query);
2291 while (my $pr = $sth->fetchrow_hashref(NAME_lc)) {
2292 push @{ $form->{ $ref->{id} } }, $pr;
2300 $main::lxdebug->leave_sub();
2304 $main::lxdebug->enter_sub();
2306 my ($self, $myconfig, $form) = @_;
2308 # connect to database
2309 my $dbh = $form->dbconnect($myconfig);
2311 my $last_period = 0;
2312 my $category = "pos_bwa";
2314 qw(1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40);
2316 $form->{decimalplaces} *= 1;
2318 &get_accounts_g($dbh, $last_period, $form->{fromdate}, $form->{todate},
2321 # if there are any compare dates
2322 if ($form->{fromdate} || $form->{todate}) {
2324 if ($form->{fromdate}) {
2325 $form->{fromdate} =~ /[0-9]*\.[0-9]*\.([0-9]*)/;
2328 $form->{todate} =~ /[0-9]*\.[0-9]*\.([0-9]*)/;
2331 $kummfromdate = $form->{comparefromdate};
2332 $kummtodate = $form->{comparetodate};
2333 &get_accounts_g($dbh, $last_period, $kummfromdate, $kummtodate, $form,
2337 @periods = qw(jetzt kumm);
2338 @gesamtleistung = qw(1 2 3);
2339 @gesamtkosten = qw (10 11 12 13 14 15 16 17 18 19 20);
2341 qw (rohertrag betriebrohertrag betriebsergebnis neutraleraufwand neutralerertrag ergebnisvorsteuern ergebnis gesamtleistung gesamtkosten);
2343 foreach $key (@periods) {
2344 $form->{ "$key" . "gesamtleistung" } = 0;
2345 $form->{ "$key" . "gesamtkosten" } = 0;
2347 foreach $category (@categories) {
2349 if (defined($form->{$category}{$key})) {
2350 $form->{"$key$category"} =
2351 $form->format_amount($myconfig,
2352 $form->round_amount($form->{$category}{$key}, 2
2356 foreach $item (@gesamtleistung) {
2357 $form->{ "$key" . "gesamtleistung" } += $form->{$item}{$key};
2359 foreach $item (@gesamtkosten) {
2360 $form->{ "$key" . "gesamtkosten" } += $form->{$item}{$key};
2362 $form->{ "$key" . "rohertrag" } =
2363 $form->{ "$key" . "gesamtleistung" } - $form->{4}{$key};
2364 $form->{ "$key" . "betriebrohertrag" } =
2365 $form->{ "$key" . "rohertrag" } + $form->{5}{$key};
2366 $form->{ "$key" . "betriebsergebnis" } =
2367 $form->{ "$key" . "betriebrohertrag" } -
2368 $form->{ "$key" . "gesamtkosten" };
2369 $form->{ "$key" . "neutraleraufwand" } =
2370 $form->{30}{$key} + $form->{31}{$key};
2371 $form->{ "$key" . "neutralertrag" } =
2372 $form->{32}{$key} + $form->{33}{$key} + $form->{34}{$key};
2373 $form->{ "$key" . "ergebnisvorsteuern" } =
2374 $form->{ "$key" . "betriebsergebnis" } -
2375 ($form->{ "$key" . "neutraleraufwand" } +
2376 $form->{ "$key" . "neutralertrag" });
2377 $form->{ "$key" . "ergebnis" } =
2378 $form->{ "$key" . "ergebnisvorsteuern" } + $form->{35}{$key};
2380 if ($form->{ "$key" . "gesamtleistung" } > 0) {
2381 foreach $category (@categories) {
2382 if (defined($form->{$category}{$key})) {
2383 $form->{ "$key" . "gl" . "$category" } =
2384 $form->format_amount(
2386 $form->round_amount(
2387 ($form->{$category}{$key} /
2388 $form->{ "$key" . "gesamtleistung" } * 100
2394 foreach $item (@ergebnisse) {
2395 $form->{ "$key" . "gl" . "$item" } =
2396 $form->format_amount($myconfig,
2397 $form->round_amount(
2398 ( $form->{ "$key" . "$item" } /
2399 $form->{ "$key" . "gesamtleistung" } * 100
2406 if ($form->{ "$key" . "gesamtkosten" } > 0) {
2407 foreach $category (@categories) {
2408 if (defined($form->{$category}{$key})) {
2409 $form->{ "$key" . "gk" . "$category" } =
2410 $form->format_amount($myconfig,
2411 $form->round_amount(
2412 ($form->{$category}{$key} /
2413 $form->{ "$key" . "gesamtkosten" } * 100
2419 foreach $item (@ergebnisse) {
2420 $form->{ "$key" . "gk" . "$item" } =
2421 $form->format_amount($myconfig,
2422 $form->round_amount(
2423 ($form->{ "$key" . "$item" } /
2424 $form->{ "$key" . "gesamtkosten" } * 100
2431 if ($form->{10}{$key} > 0) {
2432 foreach $category (@categories) {
2433 if (defined($form->{$category}{$key})) {
2434 $form->{ "$key" . "pk" . "$category" } =
2435 $form->format_amount(
2437 $form->round_amount(
2438 ($form->{$category}{$key} / $form->{10}{$key} * 100), 2
2442 foreach $item (@ergebnisse) {
2443 $form->{ "$key" . "pk" . "$item" } =
2444 $form->format_amount($myconfig,
2445 $form->round_amount(
2446 ($form->{ "$key" . "$item" } /
2447 $form->{10}{$key} * 100
2454 if ($form->{4}{$key} > 0) {
2455 foreach $category (@categories) {
2456 if (defined($form->{$category}{$key})) {
2457 $form->{ "$key" . "auf" . "$category" } =
2458 $form->format_amount(
2460 $form->round_amount(
2461 ($form->{$category}{$key} / $form->{4}{$key} * 100), 2
2465 foreach $item (@ergebnisse) {
2466 $form->{ "$key" . "auf" . "$item" } =
2467 $form->format_amount($myconfig,
2468 $form->round_amount(
2469 ($form->{ "$key" . "$item" } /
2470 $form->{4}{$key} * 100
2477 foreach $item (@ergebnisse) {
2478 $form->{ "$key" . "$item" } =
2479 $form->format_amount($myconfig,
2480 $form->round_amount($form->{ "$key" . "$item" }, 2
2487 $main::lxdebug->leave_sub();
2491 $main::lxdebug->enter_sub();
2493 my ($self, $myconfig, $form) = @_;
2495 # connect to database
2496 my $dbh = $form->dbconnect($myconfig);
2498 my $last_period = 0;
2499 my $category = "pos_ustva";
2500 my @categories_cent = qw(51r 86r 97r 93r 96 66 43 45 53 62 65 67);
2501 my @categories_euro = qw(48 51 86 91 97 93 94);
2502 $form->{decimalplaces} *= 1;
2504 foreach $item (@categories_cent) {
2505 $form->{"$item"} = 0;
2507 foreach $item (@categories_euro) {
2508 $form->{"$item"} = 0;
2511 &get_accounts_g($dbh, $last_period, $form->{fromdate}, $form->{todate},
2514 # foreach $item (@categories_cent) {
2515 # if ($form->{$item}{"jetzt"} > 0) {
2516 # $form->{$item} = $form->{$item}{"jetzt"};
2517 # delete $form->{$item}{"jetzt"};
2520 # foreach $item (@categories_euro) {
2521 # if ($form->{$item}{"jetzt"} > 0) {
2522 # $form->{$item} = $form->{$item}{"jetzt"};
2523 # delete $form->{$item}{"jetzt"};
2524 # } foreach $item (@categories_cent) {
2525 # if ($form->{$item}{"jetzt"} > 0) {
2526 # $form->{$item} = $form->{$item}{"jetzt"};
2527 # delete $form->{$item}{"jetzt"};
2530 # foreach $item (@categories_euro) {
2531 # if ($form->{$item}{"jetzt"} > 0) {
2532 # $form->{$item} = $form->{$item}{"jetzt"};
2533 # delete $form->{$item}{"jetzt"};
2539 $form->{"51r"} = $form->{"51"} * 0.16;
2540 $form->{"86r"} = $form->{"86"} * 0.07;
2541 $form->{"97r"} = $form->{"97"} * 0.16;
2542 $form->{"93r"} = $form->{"93"} * 0.07;
2543 $form->{"96"} = $form->{"94"} * 0.16;
2545 $form->{"51r"} + $form->{"86r"} + $form->{"97r"} + $form->{"93r"} +
2547 $form->{"45"} = $form->{"43"};
2548 $form->{"53"} = $form->{"43"};
2549 $form->{"62"} = $form->{"43"} - $form->{"66"};
2550 $form->{"65"} = $form->{"43"} - $form->{"66"};
2551 $form->{"67"} = $form->{"43"} - $form->{"66"};
2553 foreach $item (@categories_cent) {
2555 $form->format_amount($myconfig, $form->round_amount($form->{$item}, 2));
2558 foreach $item (@categories_euro) {
2560 $form->format_amount($myconfig, $form->round_amount($form->{$item}, 0));
2565 $main::lxdebug->leave_sub();
2568 sub income_statement {
2569 $main::lxdebug->enter_sub();
2571 my ($self, $myconfig, $form) = @_;
2573 # connect to database
2574 my $dbh = $form->dbconnect($myconfig);
2576 my $last_period = 0;
2577 my $category = "pos_eur";
2578 my @categories_einnahmen = qw(1 2 3 4 5 6 7);
2579 my @categories_ausgaben =
2580 qw(8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31);
2582 my @ergebnisse = qw(sumeura sumeurb guvsumme);
2584 $form->{decimalplaces} *= 1;
2586 foreach $item (@categories_einnahmen) {
2589 foreach $item (@categories_ausgaben) {
2593 foreach $item (@ergebnisse) {
2597 &get_accounts_g($dbh, $last_period, $form->{fromdate}, $form->{todate},
2600 foreach $item (@categories_einnahmen) {
2601 $form->{"eur${item}"} =
2602 $form->format_amount($myconfig, $form->round_amount($form->{$item}, 2));
2603 $form->{"sumeura"} += $form->{$item};
2605 foreach $item (@categories_ausgaben) {
2606 $form->{"eur${item}"} =
2607 $form->format_amount($myconfig, $form->round_amount($form->{$item}, 2));
2608 $form->{"sumeurb"} += $form->{$item};
2611 $form->{"guvsumme"} = $form->{"sumeura"} - $form->{"sumeurb"};
2613 foreach $item (@ergebnisse) {
2615 $form->format_amount($myconfig, $form->round_amount($form->{$item}, 2));
2617 $main::lxdebug->leave_sub();