Presenter: Neue Struktur in den restlichen Bereichen umgesetzt
[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 SL::Presenter::Tag qw(checkbox_tag);
19
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) ],
22 );
23
24 __PACKAGE__->run_before('check_auth');
25
26 sub action_filter {
27   my ($self) = @_;
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;
34
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 }
39                );
40
41 }
42
43 sub action_list {
44   my ($self) = @_;
45   $main::lxdebug->enter_sub();
46
47   my $report     = SL::ReportGenerator->new(\%::myconfig, $::form);
48
49   $self->prepare_report($report);
50
51   $report->set_options(
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'),
58   );
59
60   $self->setup_list_action_bar;
61   $report->generate_with_headers();
62   $main::lxdebug->leave_sub();
63 }
64
65 sub action_generate {
66   my ($self) = @_;
67
68   my $cnt = $self->make_booking();
69
70   flash('info', $::locale->text('#1 CB transactions and #1 OB transactions generated.',$cnt)) if $cnt > 0;
71
72   $self->action_list;
73 }
74
75 sub check_auth {
76   $::auth->assert('general_ledger');
77 }
78
79 #
80 # helpers
81 #
82
83 sub make_booking {
84   my ($self) = @_;
85   $main::lxdebug->enter_sub();
86   my @ids = map { $::form->{"multi_id_$_"} } grep { $::form->{"multi_id_$_"} } (1..$::form->{rowcount});
87   my $cnt = 0;
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 ) {
96         # SB
97         $self->gl_booking($balance,$self->cb_date,$::form->{cb_reference},$::form->{cb_description},$chart,$carryoverchart,0,1);
98         # EB
99         $self->gl_booking($balance,$self->ob_date,$::form->{ob_reference},$::form->{ob_description},$carryoverchart,$chart,1,0);
100         $cnt++;
101       }
102     }
103   }
104   $main::lxdebug->leave_sub();
105   return $cnt;
106 }
107
108
109 sub prepare_report {
110   my ($self,$report) = @_;
111   $main::lxdebug->enter_sub();
112   my $idx = 1;
113
114   my %column_defs = (
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
122   );
123   my @columns      = qw(ids chart description saldo sum_cb sum_ob);
124   map { $column_defs{$_}->{visible} = 1 } @columns;
125
126   my $ob_next_date = $self->ob_date->clone();
127   $ob_next_date->add(years => 1)->add(days => -1);
128
129   $self->cb_startdate($::locale->parse_date_to_object($self->get_balance_starting_date($self->cb_date)));
130
131   my @custom_headers = ();
132   # Zeile 1:
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'},
137     ];
138
139   # Zeile 2:
140   my @line_2 = ();
141   map { push @line_2 , $column_defs{$_} } grep { $column_defs{$_}->{visible} } @columns;
142   push @custom_headers, [ @line_2 ];
143
144   $report->set_custom_headers(@custom_headers);
145   $report->set_columns(%column_defs);
146   $report->set_column_order(@columns);
147
148   my $chart9actual = SL::DB::Manager::Chart->get_first( query => [ id => $self->cbob_chart ] );
149   $self->{cbob_chartaccno} = $chart9actual->accno.' '.$chart9actual->description;
150
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 };
156       $row->{ids}  = {
157         'raw_data' => checkbox_tag("multi_id_${idx}", value => $chart_id, "data-checkall" => 1),
158         'valign'   => 'center',
159         'align'    => 'center',
160       };
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";
167       } else {
168         $row->{saldo}->{data} = $::form->format_amount(\%::myconfig,0, 2)."  ";
169       }
170       my $sum_cb = 0;
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 }
174                                                                              ]) }) {
175         $sum_cb += $acc->amount;
176       }
177       my $sum_ob = 0;
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 }
181                                                                              ]) }) {
182         $sum_ob += $acc->amount;
183       }
184       if ( $sum_cb > 0 ) {
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";
188       } else {
189         $row->{sum_cb}->{data} = $::form->format_amount(\%::myconfig,0, 2)."  ";
190       }
191       if ( $sum_ob > 0 ) {
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";
195       } else {
196         $row->{sum_ob}->{data} = $::form->format_amount(\%::myconfig,0, 2)."  ";
197       }
198       $report->add_data($row);
199     }
200     $idx++;
201   }
202
203   $self->{row_count} = $idx;
204   $main::lxdebug->leave_sub();
205 }
206
207 sub get_balance {
208   $main::lxdebug->enter_sub();
209   my ($self,$chart) = @_;
210
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;
215   return $balance;
216 }
217
218 sub gl_booking {
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,
230   );
231   #$gl_entry->save;
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,
239     tax_id         => 0,
240     taxkey         => 0,
241     amount         => $amount,
242   );
243   #$kto_trans1->save;
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,
251     tax_id         => 0,
252     taxkey         => 0,
253     amount         => -$amount,
254   );
255   #$kto_trans2->save;
256   $gl_entry->add_transactions($kto_trans1);
257   $gl_entry->add_transactions($kto_trans2);
258   $gl_entry->save;
259 }
260
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}                                }
269
270 sub init_charts9000 {
271   SL::DB::Manager::Chart->get_all(  query => [ accno => { like => '9%'}] );
272 }
273
274 sub init_charts {
275   # wie geht 'not like' in rose ?
276   SL::DB::Manager::Chart->get_all(  query => [ \ "accno not like '9%'"], sort_by => 'accno ASC' );
277 }
278
279 sub setup_filter_action_bar {
280   my ($self) = @_;
281
282   for my $bar ($::request->layout->get('actionbar')) {
283     $bar->add(
284       action => [
285         t8('Continue'),
286         submit    => [ '#filter_form', { action => 'YearEndTransactions/list' } ],
287         accesskey => 'enter',
288       ],
289     );
290   }
291 }
292
293 sub setup_list_action_bar {
294   my ($self) = @_;
295
296   for my $bar ($::request->layout->get('actionbar')) {
297     $bar->add(
298       action => [
299         t8('Post'),
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',
304       ],
305       action => [
306         t8('Back'),
307         call => [ 'kivi.history_back' ],
308       ],
309     );
310   }
311 }
312
313 1;