X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FRP.pm;h=f98d47561b002ce250850a4a24b0b9ef28c1a39a;hb=72be9c763f3b7f7df1fae4fe10011e45f9e2ad1d;hp=1ee39843c34fccd69b24e96940e47b2ce5c4d51f;hpb=403165257f804b83499bde346539a25f32482ac6;p=kivitendo-erp.git diff --git a/SL/RP.pm b/SL/RP.pm index 1ee39843c..f98d47561 100644 --- a/SL/RP.pm +++ b/SL/RP.pm @@ -49,12 +49,23 @@ use strict; # - subdescription # - proper testing for heading charts # - transmission from $form to TMPL realm is not as clear as i'd like + +sub get_openbalance_date { + my ($closedto, $target) = map { $::locale->parse_date_to_object(\%::myconfig, $_) } @_; + + return unless $closedto; + + $closedto->subtract(years => 1) while ($target - $closedto)->is_negative; + $closedto->add(days => 1); + return $::locale->format_date(\%::myconfig, $closedto); +} + sub balance_sheet { $main::lxdebug->enter_sub(); my $myconfig = \%main::myconfig; my $form = $main::form; - my $dbh = $form->get_standard_dbh($myconfig); + my $dbh = $::form->get_standard_dbh; my $last_period = 0; my @categories = qw(A C L Q); @@ -64,12 +75,21 @@ sub balance_sheet { $form->{period} = $form->{this_period} = conv_dateq($form->{asofdate}); } - get_accounts($dbh, $last_period, "", $form->{asofdate}, $form, \@categories); + # get end of financial year and convert to Date format + my ($closedto) = selectfirst_array_query($form, $dbh, 'SELECT closedto FROM defaults'); + + # get date of last opening balance + my $startdate = get_openbalance_date($closedto, $form->{asofdate}); + + get_accounts($dbh, $last_period, $startdate, $form->{asofdate}, $form, \@categories); # if there are any compare dates if ($form->{compareasofdate}) { $last_period = 1; - get_accounts($dbh, $last_period, "", $form->{compareasofdate}, $form, \@categories); + + $startdate = get_openbalance_date($closedto, $form->{compareasofdate}); + + get_accounts($dbh, $last_period, $startdate, $form->{compareasofdate}, $form, \@categories); $form->{last_period} = conv_dateq($form->{compareasofdate}); } @@ -118,7 +138,6 @@ sub balance_sheet { next if ($period eq 'last' && !$last_period); # only add assets $row->{$period} *= $ml; - $form->{total}{$category}{$period} += $row->{$period}; # if ($row->{charttype} eq 'A') { # why?? } push @{ $TMPL_DATA->{$category} }, $row; @@ -138,12 +157,11 @@ sub balance_sheet { for my $period (qw(this last)) { next if ($period eq 'last' && !$last_period); - $form->{E}{$period} = $form->{total}{A}{$period} - $form->{total}{L}{$period} - $form->{total}{Q}{$period}; - $form->{total}{Q}{$period} += $form->{E}{$period}; - $TMPL_DATA->{total}{Q}{$period} = $form->{total}{Q}{$period}; - $TMPL_DATA->{total}{$period} = $form->{total}{L}{$period} + $form->{total}{Q}{$period}; + $form->{E}{$period} = $TMPL_DATA->{total}{A}{$period} - $TMPL_DATA->{total}{L}{$period} - $TMPL_DATA->{total}{Q}{$period}; + $TMPL_DATA->{total}{Q}{$period} += $form->{E}{$period}; + $TMPL_DATA->{total}{$period} = $TMPL_DATA->{total}{L}{$period} + $TMPL_DATA->{total}{Q}{$period}; } - + $form->{E}{description}='nicht verbuchter Gewinn/Verlust'; push @{ $TMPL_DATA->{Q} }, $form->{E}; $main::lxdebug->leave_sub(); @@ -328,12 +346,10 @@ sub get_accounts { } } else { # if ($form->{method} eq 'cash') - # ich sehe keinen sinn das nochmal explizit ohne conv_i aufzurufen - # bitte prüfen und löschen jan 15.11.2009 - # if ($department_id) { - # $dpt_join = qq| JOIN dpt_trans t ON (t.trans_id = ac.trans_id) |; - # $dpt_where = qq| AND t.department_id = $department_id |; - # } + if ($department_id) { + $dpt_join = qq| JOIN dpt_trans t ON (t.trans_id = ac.trans_id) |; + $dpt_where = qq| AND t.department_id = | . conv_i($department_id); + } $query = qq| SELECT c.accno, sum(ac.amount) AS amount, c.description, c.category @@ -579,10 +595,10 @@ sub get_accounts_g { } } else { # if ($form->{method} eq 'cash') - # s.o. jan 15.11.2009 -# if ($department_id) { -# ($dpt_join, $dpt_where) = sql_department($department_id); -# } + if ($department_id) { + $dpt_join = qq| JOIN dpt_trans t ON (t.trans_id = ac.trans_id) |; + $dpt_where = qq| AND (t.department_id = | . conv_i($department_id, 'NULL') . qq|) |; + } $query = qq| SELECT sum(ac.amount * chart_category_to_sgn(c.category)) AS amount, c.$category @@ -1177,21 +1193,33 @@ sub aging { } my $dpt_join; + my $where_dpt; if ($form->{department}) { my ($null, $department_id) = split /--/, $form->{department}; $dpt_join = qq| JOIN department d ON (a.department_id = d.id) |; $where .= qq| AND (a.department_id = | . conv_i($department_id, 'NULL') . qq|)|; + $where_dpt = qq| AND (${arap}.department_id = | . conv_i($department_id, 'NULL') . qq|)|; + } + my $review_of_aging_list; + if ($form->{review_of_aging_list}) { + if ($form->{review_of_aging_list} =~ m "-"){ + my @period = split(/-/, $form->{review_of_aging_list}); + $review_of_aging_list = " AND $period[0] < date_part('days', now() - duedate) + AND date_part('days', now() - duedate) < $period[1]"; + } else { + $form->{review_of_aging_list} =~ s/[^0-9]//g; + $review_of_aging_list = " AND $form->{review_of_aging_list} < date_part('days', now() - duedate)"; + } } my $q_details = qq| - -- between 0-30 days SELECT ${ct}.id AS ctid, ${ct}.name, street, zipcode, city, country, contact, email, phone as customerphone, fax as customerfax, ${ct}number, "invnumber", "transdate", (amount - COALESCE((SELECT sum(amount)*$ml FROM acc_trans LEFT JOIN chart ON (acc_trans.chart_id=chart.id) WHERE link ilike '%paid%' AND acc_trans.trans_id=${arap}.id AND acc_trans.transdate <= (date $todate)),0)) as "open", "amount", - "duedate", invoice, ${arap}.id, + "duedate", invoice, ${arap}.id, date_part('days', now() - duedate) as overduedays, (SELECT $buysell FROM exchangerate WHERE (${arap}.curr = exchangerate.curr) @@ -1200,9 +1228,10 @@ sub aging { WHERE ((paid != amount) OR (datepaid > (date $todate) AND datepaid is not null)) AND NOT COALESCE (${arap}.storno, 'f') AND (${arap}.${ct}_id = ${ct}.id) + $where_dpt AND (${ct}.id = ?) AND (transdate <= (date $todate) $fromwhere ) - + $review_of_aging_list ORDER BY ctid, transdate, invnumber |; my $sth_details = prepare_query($form, $dbh, $q_details); @@ -1264,27 +1293,6 @@ sub get_customer { $main::lxdebug->leave_sub(); } -sub get_taxaccounts { - $main::lxdebug->enter_sub(); - - my ($self, $myconfig, $form) = @_; - - # connect to database - my $dbh = $form->dbconnect($myconfig); - - # get tax accounts - my $query = - qq|SELECT c.accno, c.description, t.rate - FROM chart c, tax t - WHERE (c.link LIKE '%CT_tax%') AND (c.id = t.chart_id) - ORDER BY c.accno|; - $form->{taxaccounts} = selectall_hashref_quert($form, $dbh, $query); - - $dbh->disconnect; - - $main::lxdebug->leave_sub(); -} - sub tax_report { $main::lxdebug->enter_sub();