1 package SL::Controller::YearEndTransactions;
5 use parent qw(SL::Controller::Base);
8 use SL::Locale::String qw(t8);
9 use SL::ReportGenerator;
10 use SL::Helper::Flash;
14 use SL::DB::GLTransaction;
15 use SL::DB::AccTransaction;
16 use SL::DB::Helper::AccountingPeriod qw(get_balance_starting_date);
18 use Rose::Object::MakeMethods::Generic (
19 'scalar --get_set_init' => [ qw(charts charts9000 cbob_chart cb_date cb_startdate ob_date cb_reference ob_reference cb_description ob_description) ],
22 __PACKAGE__->run_before('check_auth');
26 $self->ob_date(DateTime->today->truncate(to => 'year')) if !$self->ob_date;
27 $self->cb_date(DateTime->today->truncate(to => 'year')->add(days => -1)) if !$self->cb_date;
28 $self->ob_reference(t8('OB Transaction')) if !$self->ob_reference;
29 $self->cb_reference(t8('CB Transaction')) if !$self->cb_reference;
30 $self->ob_description(t8('OB Transaction')) if !$self->ob_description;
31 $self->cb_description(t8('CB Transaction')) if !$self->cb_description;
33 $self->setup_filter_action_bar;
34 $self->render('gl/yearend_filter',
35 title => t8('CB/OB Transactions'),
36 make_title_of_chart => sub { $_[0]->accno.' '.$_[0]->description }
43 $main::lxdebug->enter_sub();
45 my $report = SL::ReportGenerator->new(\%::myconfig, $::form);
47 $self->prepare_report($report);
50 output_format => 'HTML',
51 raw_top_info_text => $::form->parse_html_template('gl/yearend_top', { SELF => $self }),
52 raw_bottom_info_text => $::form->parse_html_template('gl/yearend_bottom', { SELF => $self }),
53 allow_pdf_export => 0,
54 allow_csv_export => 0,
55 title => $::locale->text('CB/OB Transactions'),
58 $self->setup_list_action_bar;
59 $report->generate_with_headers();
60 $main::lxdebug->leave_sub();
66 my $cnt = $self->make_booking();
68 flash('info', $::locale->text('#1 CB transactions and #1 OB transactions generated.',$cnt)) if $cnt > 0;
74 $::auth->assert('general_ledger');
83 $main::lxdebug->enter_sub();
84 my @ids = map { $::form->{"multi_id_$_"} } grep { $::form->{"multi_id_$_"} } (1..$::form->{rowcount});
86 $main::lxdebug->message(LXDebug->DEBUG2(),"generate for ".$::form->{cbob_chart}." # ".scalar(@ids)." charts");
87 if (scalar(@ids) && $::form->{cbob_chart}) {
88 my $carryoverchart = SL::DB::Manager::Chart->get_first( query => [ id => $::form->{cbob_chart} ] );
89 my $charts = SL::DB::Manager::Chart->get_all( query => [ id => \@ids ] );
90 foreach my $chart (@{ $charts }) {
91 $main::lxdebug->message(LXDebug->DEBUG2(),"chart_id=".$chart->id." accno=".$chart->accno);
92 my $balance = $self->get_balance($chart);
93 if ( $balance != 0 ) {
95 $self->gl_booking($balance,$self->cb_date,$::form->{cb_reference},$::form->{cb_description},$chart,$carryoverchart,0,1);
97 $self->gl_booking($balance,$self->ob_date,$::form->{ob_reference},$::form->{ob_description},$carryoverchart,$chart,1,0);
102 $main::lxdebug->leave_sub();
108 my ($self,$report) = @_;
109 $main::lxdebug->enter_sub();
113 'ids' => { raw_header_data => $self->presenter->checkbox_tag("", id => "check_all",
114 checkall => "[data-checkall=1]"), 'align' => 'center' },
115 'chart' => { text => $::locale->text('Account'), },
116 'description' => { text => $::locale->text('Description'), },
117 'saldo' => { text => $::locale->text('Saldo'), 'align' => 'right'},
118 'sum_cb' => { text => $::locale->text('Sum CB Transactions'), 'align' => 'right'}, ##close == Schluss
119 'sum_ob' => { text => $::locale->text('Sum OB Transactions'), 'align' => 'right'}, ##open == Eingang
121 my @columns = qw(ids chart description saldo sum_cb sum_ob);
122 map { $column_defs{$_}->{visible} = 1 } @columns;
124 my $ob_next_date = $self->ob_date->clone();
125 $ob_next_date->add(years => 1)->add(days => -1);
127 $self->cb_startdate($::locale->parse_date_to_object($self->get_balance_starting_date($self->cb_date)));
129 my @custom_headers = ();
131 push @custom_headers, [
132 { 'text' => ' ', 'colspan' => 3 },
133 { 'text' => $::locale->text("Timerange")."<br />".$self->cb_startdate->to_kivitendo." - ".$self->cb_date->to_kivitendo, 'colspan' => 2, 'align' => 'center'},
134 { 'text' => $::locale->text("Timerange")."<br />".$self->ob_date->to_kivitendo." - ".$ob_next_date->to_kivitendo, 'align' => 'center'},
139 map { push @line_2 , $column_defs{$_} } grep { $column_defs{$_}->{visible} } @columns;
140 push @custom_headers, [ @line_2 ];
142 $report->set_custom_headers(@custom_headers);
143 $report->set_columns(%column_defs);
144 $report->set_column_order(@columns);
146 my $chart9actual = SL::DB::Manager::Chart->get_first( query => [ id => $self->cbob_chart ] );
147 $self->{cbob_chartaccno} = $chart9actual->accno.' '.$chart9actual->description;
149 foreach my $chart (@{ $self->charts }) {
150 my $balance = $self->get_balance($chart);
151 if ( $balance != 0 ) {
152 my $chart_id = $chart->id;
153 my $row = { map { $_ => { 'data' => '' } } @columns };
155 'raw_data' => $self->presenter->checkbox_tag("multi_id_${idx}", value => $chart_id, "data-checkall" => 1),
156 'valign' => 'center',
159 $row->{chart}->{data} = $chart->accno;
160 $row->{description}->{data} = $chart->description;
161 if ( $balance > 0 ) {
162 $row->{saldo}->{data} = $::form->format_amount(\%::myconfig, $balance, 2)." H";
163 } elsif ( $balance < 0 ) {
164 $row->{saldo}->{data} = $::form->format_amount(\%::myconfig,-$balance, 2)." S";
166 $row->{saldo}->{data} = $::form->format_amount(\%::myconfig,0, 2)." ";
169 foreach my $acc ( @{ SL::DB::Manager::AccTransaction->get_all(where => [ chart_id => $chart->id, cb_transaction => 't',
170 transdate => { ge => $self->cb_startdate},
171 transdate => { le => $self->cb_date }
173 $sum_cb += $acc->amount;
176 foreach my $acc ( @{ SL::DB::Manager::AccTransaction->get_all(where => [ chart_id => $chart->id, ob_transaction => 't',
177 transdate => { ge => $self->ob_date},
178 transdate => { le => $ob_next_date }
180 $sum_ob += $acc->amount;
183 $row->{sum_cb}->{data} = $::form->format_amount(\%::myconfig, $sum_cb, 2)." H";
184 } elsif ( $sum_cb < 0 ) {
185 $row->{sum_cb}->{data} = $::form->format_amount(\%::myconfig,-$sum_cb, 2)." S";
187 $row->{sum_cb}->{data} = $::form->format_amount(\%::myconfig,0, 2)." ";
190 $row->{sum_ob}->{data} = $::form->format_amount(\%::myconfig, $sum_ob, 2)." H";
191 } elsif ( $sum_ob < 0 ) {
192 $row->{sum_ob}->{data} = $::form->format_amount(\%::myconfig,-$sum_ob, 2)." S";
194 $row->{sum_ob}->{data} = $::form->format_amount(\%::myconfig,0, 2)." ";
196 $report->add_data($row);
201 $self->{row_count} = $idx;
202 $main::lxdebug->leave_sub();
206 $main::lxdebug->enter_sub();
207 my ($self,$chart) = @_;
209 #$main::lxdebug->message(LXDebug->DEBUG2(),"get_balance from=".$self->cb_startdate->to_kivitendo." to=".$self->cb_date->to_kivitendo);
210 my $balance = $chart->get_balance(fromdate => $self->cb_startdate, todate => $self->cb_date);
211 $main::lxdebug->leave_sub();
212 return 0 if !defined $balance || $balance == 0;
217 my ($self, $amount, $transdate, $reference, $description, $konto, $gegenkonto, $ob, $cb) = @_;
218 $::form->get_employee();
219 my $employee_id = $::form->{employee_id};
220 $main::lxdebug->message(LXDebug->DEBUG2(),"employee_id=".$employee_id." ob=".$ob." cb=".$cb);
221 my $gl_entry = SL::DB::GLTransaction->new(
222 employee_id => $employee_id,
223 transdate => $transdate,
224 reference => $reference,
225 description => $description,
226 ob_transaction => $ob,
227 cb_transaction => $cb,
230 my $kto_trans1 = SL::DB::AccTransaction->new(
231 trans_id => $gl_entry->id,
232 transdate => $transdate,
233 ob_transaction => $ob,
234 cb_transaction => $cb,
235 chart_id => $gegenkonto->id,
236 chart_link => $konto->link,
242 my $kto_trans2 = SL::DB::AccTransaction->new(
243 trans_id => $gl_entry->id,
244 transdate => $transdate,
245 ob_transaction => $ob,
246 cb_transaction => $cb,
247 chart_id => $konto->id,
248 chart_link => $konto->link,
254 $gl_entry->add_transactions($kto_trans1);
255 $gl_entry->add_transactions($kto_trans2);
259 sub init_cbob_chart { $::form->{cbob_chart} }
260 sub init_ob_date { $::locale->parse_date_to_object($::form->{ob_date}) }
261 sub init_ob_reference { $::form->{ob_reference} }
262 sub init_ob_description { $::form->{ob_description} }
263 sub init_cb_startdate { $::locale->parse_date_to_object($::form->{cb_startdate}) }
264 sub init_cb_date { $::locale->parse_date_to_object($::form->{cb_date}) }
265 sub init_cb_reference { $::form->{cb_reference} }
266 sub init_cb_description { $::form->{cb_description} }
268 sub init_charts9000 {
269 SL::DB::Manager::Chart->get_all( query => [ accno => { like => '9%'}] );
273 # wie geht 'not like' in rose ?
274 SL::DB::Manager::Chart->get_all( query => [ \ "accno not like '9%'"], sort_by => 'accno ASC' );
277 sub setup_filter_action_bar {
280 for my $bar ($::request->layout->get('actionbar')) {
284 submit => [ '#filter_form', { action => 'YearEndTransactions/list' } ],
285 accesskey => 'enter',
291 sub setup_list_action_bar {
294 for my $bar ($::request->layout->get('actionbar')) {
298 submit => [ '#form', { action => 'YearEndTransactions/generate' } ],
299 tooltip => t8('generate cb/ob transactions for selected charts'),
300 confirm => t8('Are you sure to generate cb/ob transactions?'),
301 accesskey => 'enter',
305 call => [ 'kivi.history_back' ],