ActionBar: Verwendung bei Jahresendbuchungen
[kivitendo-erp.git] / SL / Controller / YearEndTransactions.pm
1 package SL::Controller::YearEndTransactions;
2
3 use strict;
4
5 use parent qw(SL::Controller::Base);
6
7 use DateTime;
8 use SL::Locale::String qw(t8);
9 use SL::ReportGenerator;
10 use SL::Helper::Flash;
11 use SL::DBUtils;
12
13 use SL::DB::Chart;
14 use SL::DB::GLTransaction;
15 use SL::DB::AccTransaction;
16 use SL::DB::Helper::AccountingPeriod qw(get_balance_starting_date);
17
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) ],
20 );
21
22 __PACKAGE__->run_before('check_auth');
23
24 sub action_filter {
25   my ($self) = @_;
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;
32
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 }
37                );
38
39 }
40
41 sub action_list {
42   my ($self) = @_;
43   $main::lxdebug->enter_sub();
44
45   my $report     = SL::ReportGenerator->new(\%::myconfig, $::form);
46
47   $self->prepare_report($report);
48
49   $report->set_options(
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'),
56   );
57
58   $self->setup_list_action_bar;
59   $report->generate_with_headers(action_bar => 1);
60   $main::lxdebug->leave_sub();
61 }
62
63 sub action_generate {
64   my ($self) = @_;
65
66   my $cnt = $self->make_booking();
67
68   flash('info', $::locale->text('#1 CB transactions and #1 OB transactions generated.',$cnt)) if $cnt > 0;
69
70   $self->action_list;
71 }
72
73 sub check_auth {
74   $::auth->assert('general_ledger');
75 }
76
77 #
78 # helpers
79 #
80
81 sub make_booking {
82   my ($self) = @_;
83   $main::lxdebug->enter_sub();
84   my @ids = map { $::form->{"multi_id_$_"} } grep { $::form->{"multi_id_$_"} } (1..$::form->{rowcount});
85   my $cnt = 0;
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 ) {
94         # SB
95         $self->gl_booking($balance,$self->cb_date,$::form->{cb_reference},$::form->{cb_description},$chart,$carryoverchart,0,1);
96         # EB
97         $self->gl_booking($balance,$self->ob_date,$::form->{ob_reference},$::form->{ob_description},$carryoverchart,$chart,1,0);
98         $cnt++;
99       }
100     }
101   }
102   $main::lxdebug->leave_sub();
103   return $cnt;
104 }
105
106
107 sub prepare_report {
108   my ($self,$report) = @_;
109   $main::lxdebug->enter_sub();
110   my $idx = 1;
111
112   my %column_defs = (
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
120   );
121   my @columns      = qw(ids chart description saldo sum_cb sum_ob);
122   map { $column_defs{$_}->{visible} = 1 } @columns;
123
124   my $ob_next_date = $self->ob_date->clone();
125   $ob_next_date->add(years => 1)->add(days => -1);
126
127   $self->cb_startdate($::locale->parse_date_to_object($self->get_balance_starting_date($self->cb_date)));
128
129   my @custom_headers = ();
130   # Zeile 1:
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'},
135     ];
136
137   # Zeile 2:
138   my @line_2 = ();
139   map { push @line_2 , $column_defs{$_} } grep { $column_defs{$_}->{visible} } @columns;
140   push @custom_headers, [ @line_2 ];
141
142   $report->set_custom_headers(@custom_headers);
143   $report->set_columns(%column_defs);
144   $report->set_column_order(@columns);
145
146   my $chart9actual = SL::DB::Manager::Chart->get_first( query => [ id => $self->cbob_chart ] );
147   $self->{cbob_chartaccno} = $chart9actual->accno.' '.$chart9actual->description;
148
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 };
154       $row->{ids}  = {
155         'raw_data' => $self->presenter->checkbox_tag("multi_id_${idx}", value => $chart_id, "data-checkall" => 1),
156         'valign'   => 'center',
157         'align'    => 'center',
158       };
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";
165       } else {
166         $row->{saldo}->{data} = $::form->format_amount(\%::myconfig,0, 2)."  ";
167       }
168       my $sum_cb = 0;
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 }
172                                                                              ]) }) {
173         $sum_cb += $acc->amount;
174       }
175       my $sum_ob = 0;
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 }
179                                                                              ]) }) {
180         $sum_ob += $acc->amount;
181       }
182       if ( $sum_cb > 0 ) {
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";
186       } else {
187         $row->{sum_cb}->{data} = $::form->format_amount(\%::myconfig,0, 2)."  ";
188       }
189       if ( $sum_ob > 0 ) {
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";
193       } else {
194         $row->{sum_ob}->{data} = $::form->format_amount(\%::myconfig,0, 2)."  ";
195       }
196       $report->add_data($row);
197     }
198     $idx++;
199   }
200
201   $self->{row_count} = $idx;
202   $main::lxdebug->leave_sub();
203 }
204
205 sub get_balance {
206   $main::lxdebug->enter_sub();
207   my ($self,$chart) = @_;
208
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;
213   return $balance;
214 }
215
216 sub gl_booking {
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,
228   );
229   #$gl_entry->save;
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,
237     tax_id         => 0,
238     taxkey         => 0,
239     amount         => $amount,
240   );
241   #$kto_trans1->save;
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,
249     tax_id         => 0,
250     taxkey         => 0,
251     amount         => -$amount,
252   );
253   #$kto_trans2->save;
254   $gl_entry->add_transactions($kto_trans1);
255   $gl_entry->add_transactions($kto_trans2);
256   $gl_entry->save;
257 }
258
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}                                }
267
268 sub init_charts9000 {
269   SL::DB::Manager::Chart->get_all(  query => [ accno => { like => '9%'}] );
270 }
271
272 sub init_charts {
273   # wie geht 'not like' in rose ?
274   SL::DB::Manager::Chart->get_all(  query => [ \ "accno not like '9%'"], sort_by => 'accno ASC' );
275 }
276
277 sub setup_filter_action_bar {
278   my ($self) = @_;
279
280   for my $bar ($::request->layout->get('actionbar')) {
281     $bar->add(
282       action => [
283         t8('Continue'),
284         submit    => [ '#filter_form', { action => 'YearEndTransactions/list' } ],
285         accesskey => 'enter',
286       ],
287     );
288   }
289 }
290
291 sub setup_list_action_bar {
292   my ($self) = @_;
293
294   for my $bar ($::request->layout->get('actionbar')) {
295     $bar->add(
296       action => [
297         t8('Post'),
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',
302       ],
303       action => [
304         t8('Back'),
305         call => [ 'kivi.history_back' ],
306       ],
307     );
308   }
309 }
310
311 1;