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 SL::Presenter::Tag qw(checkbox_tag);
20 use Rose::Object::MakeMethods::Generic (
21 'scalar --get_set_init' => [ qw(charts charts9000 cbob_chart cb_date cb_startdate ob_date cb_reference ob_reference cb_description ob_description) ],
24 __PACKAGE__->run_before('check_auth');
28 $self->ob_date(DateTime->today->truncate(to => 'year')) if !$self->ob_date;
29 $self->cb_date(DateTime->today->truncate(to => 'year')->add(days => -1)) if !$self->cb_date;
30 $self->ob_reference(t8('OB Transaction')) if !$self->ob_reference;
31 $self->cb_reference(t8('CB Transaction')) if !$self->cb_reference;
32 $self->ob_description(t8('OB Transaction')) if !$self->ob_description;
33 $self->cb_description(t8('CB Transaction')) if !$self->cb_description;
35 $self->setup_filter_action_bar;
36 $self->render('gl/yearend_filter',
37 title => t8('CB/OB Transactions'),
38 make_title_of_chart => sub { $_[0]->accno.' '.$_[0]->description }
45 $main::lxdebug->enter_sub();
47 my $report = SL::ReportGenerator->new(\%::myconfig, $::form);
49 $self->prepare_report($report);
52 output_format => 'HTML',
53 raw_top_info_text => $::form->parse_html_template('gl/yearend_top', { SELF => $self }),
54 raw_bottom_info_text => $::form->parse_html_template('gl/yearend_bottom', { SELF => $self }),
55 allow_pdf_export => 0,
56 allow_csv_export => 0,
57 title => $::locale->text('CB/OB Transactions'),
60 $self->setup_list_action_bar;
61 $report->generate_with_headers();
62 $main::lxdebug->leave_sub();
68 my $cnt = $self->make_booking();
70 flash('info', $::locale->text('#1 CB transactions and #1 OB transactions generated.',$cnt)) if $cnt > 0;
76 $::auth->assert('general_ledger');
85 $main::lxdebug->enter_sub();
86 my @ids = map { $::form->{"multi_id_$_"} } grep { $::form->{"multi_id_$_"} } (1..$::form->{rowcount});
88 $main::lxdebug->message(LXDebug->DEBUG2(),"generate for ".$::form->{cbob_chart}." # ".scalar(@ids)." charts");
89 if (scalar(@ids) && $::form->{cbob_chart}) {
90 my $carryoverchart = SL::DB::Manager::Chart->get_first( query => [ id => $::form->{cbob_chart} ] );
91 my $charts = SL::DB::Manager::Chart->get_all( query => [ id => \@ids ] );
92 foreach my $chart (@{ $charts }) {
93 $main::lxdebug->message(LXDebug->DEBUG2(),"chart_id=".$chart->id." accno=".$chart->accno);
94 my $balance = $self->get_balance($chart);
95 if ( $balance != 0 ) {
97 $self->gl_booking($balance,$self->cb_date,$::form->{cb_reference},$::form->{cb_description},$chart,$carryoverchart,0,1);
99 $self->gl_booking($balance,$self->ob_date,$::form->{ob_reference},$::form->{ob_description},$carryoverchart,$chart,1,0);
104 $main::lxdebug->leave_sub();
110 my ($self,$report) = @_;
111 $main::lxdebug->enter_sub();
115 'ids' => { raw_header_data => checkbox_tag("", id => "check_all",
116 checkall => "[data-checkall=1]"), 'align' => 'center' },
117 'chart' => { text => $::locale->text('Account'), },
118 'description' => { text => $::locale->text('Description'), },
119 'saldo' => { text => $::locale->text('Saldo'), 'align' => 'right'},
120 'sum_cb' => { text => $::locale->text('Sum CB Transactions'), 'align' => 'right'}, ##close == Schluss
121 'sum_ob' => { text => $::locale->text('Sum OB Transactions'), 'align' => 'right'}, ##open == Eingang
123 my @columns = qw(ids chart description saldo sum_cb sum_ob);
124 map { $column_defs{$_}->{visible} = 1 } @columns;
126 my $ob_next_date = $self->ob_date->clone();
127 $ob_next_date->add(years => 1)->add(days => -1);
129 $self->cb_startdate($::locale->parse_date_to_object($self->get_balance_starting_date($self->cb_date)));
131 my @custom_headers = ();
133 push @custom_headers, [
134 { 'text' => ' ', 'colspan' => 3 },
135 { 'text' => $::locale->text("Timerange")."<br />".$self->cb_startdate->to_kivitendo." - ".$self->cb_date->to_kivitendo, 'colspan' => 2, 'align' => 'center'},
136 { 'text' => $::locale->text("Timerange")."<br />".$self->ob_date->to_kivitendo." - ".$ob_next_date->to_kivitendo, 'align' => 'center'},
141 map { push @line_2 , $column_defs{$_} } grep { $column_defs{$_}->{visible} } @columns;
142 push @custom_headers, [ @line_2 ];
144 $report->set_custom_headers(@custom_headers);
145 $report->set_columns(%column_defs);
146 $report->set_column_order(@columns);
148 my $chart9actual = SL::DB::Manager::Chart->get_first( query => [ id => $self->cbob_chart ] );
149 $self->{cbob_chartaccno} = $chart9actual->accno.' '.$chart9actual->description;
151 foreach my $chart (@{ $self->charts }) {
152 my $balance = $self->get_balance($chart);
153 if ( $balance != 0 ) {
154 my $chart_id = $chart->id;
155 my $row = { map { $_ => { 'data' => '' } } @columns };
157 'raw_data' => checkbox_tag("multi_id_${idx}", value => $chart_id, "data-checkall" => 1),
158 'valign' => 'center',
161 $row->{chart}->{data} = $chart->accno;
162 $row->{description}->{data} = $chart->description;
163 if ( $balance > 0 ) {
164 $row->{saldo}->{data} = $::form->format_amount(\%::myconfig, $balance, 2)." H";
165 } elsif ( $balance < 0 ) {
166 $row->{saldo}->{data} = $::form->format_amount(\%::myconfig,-$balance, 2)." S";
168 $row->{saldo}->{data} = $::form->format_amount(\%::myconfig,0, 2)." ";
171 foreach my $acc ( @{ SL::DB::Manager::AccTransaction->get_all(where => [ chart_id => $chart->id, cb_transaction => 't',
172 transdate => { ge => $self->cb_startdate},
173 transdate => { le => $self->cb_date }
175 $sum_cb += $acc->amount;
178 foreach my $acc ( @{ SL::DB::Manager::AccTransaction->get_all(where => [ chart_id => $chart->id, ob_transaction => 't',
179 transdate => { ge => $self->ob_date},
180 transdate => { le => $ob_next_date }
182 $sum_ob += $acc->amount;
185 $row->{sum_cb}->{data} = $::form->format_amount(\%::myconfig, $sum_cb, 2)." H";
186 } elsif ( $sum_cb < 0 ) {
187 $row->{sum_cb}->{data} = $::form->format_amount(\%::myconfig,-$sum_cb, 2)." S";
189 $row->{sum_cb}->{data} = $::form->format_amount(\%::myconfig,0, 2)." ";
192 $row->{sum_ob}->{data} = $::form->format_amount(\%::myconfig, $sum_ob, 2)." H";
193 } elsif ( $sum_ob < 0 ) {
194 $row->{sum_ob}->{data} = $::form->format_amount(\%::myconfig,-$sum_ob, 2)." S";
196 $row->{sum_ob}->{data} = $::form->format_amount(\%::myconfig,0, 2)." ";
198 $report->add_data($row);
203 $self->{row_count} = $idx;
204 $main::lxdebug->leave_sub();
208 $main::lxdebug->enter_sub();
209 my ($self,$chart) = @_;
211 #$main::lxdebug->message(LXDebug->DEBUG2(),"get_balance from=".$self->cb_startdate->to_kivitendo." to=".$self->cb_date->to_kivitendo);
212 my $balance = $chart->get_balance(fromdate => $self->cb_startdate, todate => $self->cb_date);
213 $main::lxdebug->leave_sub();
214 return 0 if !defined $balance || $balance == 0;
219 my ($self, $amount, $transdate, $reference, $description, $konto, $gegenkonto, $ob, $cb) = @_;
220 $::form->get_employee();
221 my $employee_id = $::form->{employee_id};
222 $main::lxdebug->message(LXDebug->DEBUG2(),"employee_id=".$employee_id." ob=".$ob." cb=".$cb);
223 my $gl_entry = SL::DB::GLTransaction->new(
224 employee_id => $employee_id,
225 transdate => $transdate,
226 reference => $reference,
227 description => $description,
228 ob_transaction => $ob,
229 cb_transaction => $cb,
232 my $kto_trans1 = SL::DB::AccTransaction->new(
233 trans_id => $gl_entry->id,
234 transdate => $transdate,
235 ob_transaction => $ob,
236 cb_transaction => $cb,
237 chart_id => $gegenkonto->id,
238 chart_link => $konto->link,
244 my $kto_trans2 = SL::DB::AccTransaction->new(
245 trans_id => $gl_entry->id,
246 transdate => $transdate,
247 ob_transaction => $ob,
248 cb_transaction => $cb,
249 chart_id => $konto->id,
250 chart_link => $konto->link,
256 $gl_entry->add_transactions($kto_trans1);
257 $gl_entry->add_transactions($kto_trans2);
261 sub init_cbob_chart { $::form->{cbob_chart} }
262 sub init_ob_date { $::locale->parse_date_to_object($::form->{ob_date}) }
263 sub init_ob_reference { $::form->{ob_reference} }
264 sub init_ob_description { $::form->{ob_description} }
265 sub init_cb_startdate { $::locale->parse_date_to_object($::form->{cb_startdate}) }
266 sub init_cb_date { $::locale->parse_date_to_object($::form->{cb_date}) }
267 sub init_cb_reference { $::form->{cb_reference} }
268 sub init_cb_description { $::form->{cb_description} }
270 sub init_charts9000 {
271 SL::DB::Manager::Chart->get_all( query => [ accno => { like => '9%'}] );
275 # wie geht 'not like' in rose ?
276 SL::DB::Manager::Chart->get_all( query => [ \ "accno not like '9%'"], sort_by => 'accno ASC' );
279 sub setup_filter_action_bar {
282 for my $bar ($::request->layout->get('actionbar')) {
286 submit => [ '#filter_form', { action => 'YearEndTransactions/list' } ],
287 accesskey => 'enter',
293 sub setup_list_action_bar {
296 for my $bar ($::request->layout->get('actionbar')) {
300 submit => [ '#form', { action => 'YearEndTransactions/generate' } ],
301 tooltip => t8('generate cb/ob transactions for selected charts'),
302 confirm => t8('Are you sure to generate cb/ob transactions?'),
303 accesskey => 'enter',
307 call => [ 'kivi.history_back' ],