Verbrauchsbericht: Lager->Berichte->Lagerentnahme
[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   $self->render('gl/yearend_filter',
33                 title               => t8('CB/OB Transactions'),
34                 make_title_of_chart => sub { $_[0]->accno.' '.$_[0]->description }
35                );
36
37 }
38
39 sub action_list {
40   my ($self) = @_;
41   $main::lxdebug->enter_sub();
42
43   my $report     = SL::ReportGenerator->new(\%::myconfig, $::form);
44
45   $self->prepare_report($report);
46
47   $report->set_options(
48     output_format        => 'HTML',
49     raw_top_info_text    => $::form->parse_html_template('gl/yearend_top',    { SELF => $self }),
50     raw_bottom_info_text => $::form->parse_html_template('gl/yearend_bottom', { SELF => $self }),
51     allow_pdf_export     => 0,
52     allow_csv_export     => 0,
53     title                => $::locale->text('CB/OB Transactions'),
54   );
55   $report->generate_with_headers();
56   $main::lxdebug->leave_sub();
57 }
58
59 sub action_generate {
60   my ($self) = @_;
61
62   my $cnt = $self->make_booking();
63
64   flash('info', $::locale->text('#1 CB transactions and #1 OB transactions generated.',$cnt)) if $cnt > 0;
65
66   $self->action_list;
67 }
68
69 sub check_auth {
70   $::auth->assert('general_ledger');
71 }
72
73 #
74 # helpers
75 #
76
77 sub make_booking {
78   my ($self) = @_;
79   $main::lxdebug->enter_sub();
80   my @ids = map { $::form->{"multi_id_$_"} } grep { $::form->{"multi_id_$_"} } (1..$::form->{rowcount});
81   my $cnt = 0;
82   $main::lxdebug->message(LXDebug->DEBUG2(),"generate for ".$::form->{cbob_chart}." # ".scalar(@ids)." charts");
83   if (scalar(@ids) && $::form->{cbob_chart}) {
84     my $carryoverchart = SL::DB::Manager::Chart->get_first(  query => [ id => $::form->{cbob_chart} ] );
85     my $charts = SL::DB::Manager::Chart->get_all(  query => [ id => \@ids ] );
86     foreach my $chart (@{ $charts }) {
87       $main::lxdebug->message(LXDebug->DEBUG2(),"chart_id=".$chart->id." accno=".$chart->accno);
88       my $balance = $self->get_balance($chart);
89       if ( $balance != 0 ) {
90         # SB
91         $self->gl_booking($balance,$self->cb_date,$::form->{cb_reference},$::form->{cb_description},$chart,$carryoverchart,0,1);
92         # EB
93         $self->gl_booking($balance,$self->ob_date,$::form->{ob_reference},$::form->{ob_description},$carryoverchart,$chart,1,0);
94         $cnt++;
95       }
96     }
97   }
98   $main::lxdebug->leave_sub();
99   return $cnt;
100 }
101
102
103 sub prepare_report {
104   my ($self,$report) = @_;
105   $main::lxdebug->enter_sub();
106   my $idx = 1;
107
108   my %column_defs = (
109     'ids'         => { raw_header_data => $self->presenter->checkbox_tag("", id => "check_all",
110                                                                           checkall => "[data-checkall=1]"), 'align' => 'center' },
111     'chart'       => { text => $::locale->text('Account'), },
112     'description' => { text => $::locale->text('Description'), },
113     'saldo'       => { text => $::locale->text('Saldo'),  'align' => 'right'},
114     'sum_cb'      => { text => $::locale->text('Sum CB Transactions'), 'align' => 'right'},  ##close == Schluss
115     'sum_ob'      => { text => $::locale->text('Sum OB Transactions'), 'align' => 'right'},  ##open  == Eingang
116   );
117   my @columns      = qw(ids chart description saldo sum_cb sum_ob);
118   map { $column_defs{$_}->{visible} = 1 } @columns;
119
120   my $ob_next_date = $self->ob_date->clone();
121   $ob_next_date->add(years => 1)->add(days => -1);
122
123   $self->cb_startdate($::locale->parse_date_to_object($self->get_balance_starting_date($self->cb_date)));
124
125   my @custom_headers = ();
126   # Zeile 1:
127   push @custom_headers, [
128       { 'text' => '   ', 'colspan' => 3 },
129       { 'text' => $::locale->text("Timerange")."<br />".$self->cb_startdate->to_kivitendo." - ".$self->cb_date->to_kivitendo, 'colspan' => 2, 'align' => 'center'},
130       { 'text' => $::locale->text("Timerange")."<br />".$self->ob_date->to_kivitendo." - ".$ob_next_date->to_kivitendo, 'align' => 'center'},
131     ];
132
133   # Zeile 2:
134   my @line_2 = ();
135   map { push @line_2 , $column_defs{$_} } grep { $column_defs{$_}->{visible} } @columns;
136   push @custom_headers, [ @line_2 ];
137
138   $report->set_custom_headers(@custom_headers);
139   $report->set_columns(%column_defs);
140   $report->set_column_order(@columns);
141
142   my $chart9actual = SL::DB::Manager::Chart->get_first( query => [ id => $self->cbob_chart ] );
143   $self->{cbob_chartaccno} = $chart9actual->accno.' '.$chart9actual->description;
144
145   foreach my $chart (@{ $self->charts }) {
146     my $balance = $self->get_balance($chart);
147     if ( $balance != 0 ) {
148       my $chart_id = $chart->id;
149       my $row = { map { $_ => { 'data' => '' } } @columns };
150       $row->{ids}  = {
151         'raw_data' => $self->presenter->checkbox_tag("multi_id_${idx}", value => $chart_id, "data-checkall" => 1),
152         'valign'   => 'center',
153         'align'    => 'center',
154       };
155       $row->{chart}->{data}       = $chart->accno;
156       $row->{description}->{data} = $chart->description;
157       if ( $balance > 0 ) {
158         $row->{saldo}->{data} = $::form->format_amount(\%::myconfig, $balance, 2)." H";
159       } elsif ( $balance < 0 )  {
160         $row->{saldo}->{data} = $::form->format_amount(\%::myconfig,-$balance, 2)." S";
161       } else {
162         $row->{saldo}->{data} = $::form->format_amount(\%::myconfig,0, 2)."  ";
163       }
164       my $sum_cb = 0;
165       foreach my $acc ( @{ SL::DB::Manager::AccTransaction->get_all(where => [ chart_id  => $chart->id, cb_transaction => 't',
166                                                                                transdate => { ge => $self->cb_startdate},
167                                                                                transdate => { le => $self->cb_date }
168                                                                              ]) }) {
169         $sum_cb += $acc->amount;
170       }
171       my $sum_ob = 0;
172       foreach my $acc ( @{ SL::DB::Manager::AccTransaction->get_all(where => [ chart_id  => $chart->id, ob_transaction => 't',
173                                                                                transdate => { ge => $self->ob_date},
174                                                                                transdate => { le => $ob_next_date }
175                                                                              ]) }) {
176         $sum_ob += $acc->amount;
177       }
178       if ( $sum_cb > 0 ) {
179         $row->{sum_cb}->{data} = $::form->format_amount(\%::myconfig, $sum_cb, 2)." H";
180       } elsif ( $sum_cb < 0 )  {
181         $row->{sum_cb}->{data} = $::form->format_amount(\%::myconfig,-$sum_cb, 2)." S";
182       } else {
183         $row->{sum_cb}->{data} = $::form->format_amount(\%::myconfig,0, 2)."  ";
184       }
185       if ( $sum_ob > 0 ) {
186         $row->{sum_ob}->{data} = $::form->format_amount(\%::myconfig, $sum_ob, 2)." H";
187       } elsif ( $sum_ob < 0 )  {
188         $row->{sum_ob}->{data} = $::form->format_amount(\%::myconfig,-$sum_ob, 2)." S";
189       } else {
190         $row->{sum_ob}->{data} = $::form->format_amount(\%::myconfig,0, 2)."  ";
191       }
192       $report->add_data($row);
193     }
194     $idx++;
195   }
196
197   $self->{row_count} = $idx;
198   $main::lxdebug->leave_sub();
199 }
200
201 sub get_balance {
202   $main::lxdebug->enter_sub();
203   my ($self,$chart) = @_;
204
205   #$main::lxdebug->message(LXDebug->DEBUG2(),"get_balance from=".$self->cb_startdate->to_kivitendo." to=".$self->cb_date->to_kivitendo);
206   my $balance = $chart->get_balance(fromdate => $self->cb_startdate, todate => $self->cb_date);
207   $main::lxdebug->leave_sub();
208   return 0 unless $balance != 0;
209   return $balance;
210 }
211
212 sub gl_booking {
213   my ($self, $amount, $transdate, $reference, $description, $konto, $gegenkonto, $ob, $cb) = @_;
214   $::form->get_employee();
215   my $employee_id = $::form->{employee_id};
216   $main::lxdebug->message(LXDebug->DEBUG2(),"employee_id=".$employee_id." ob=".$ob." cb=".$cb);
217   my $gl_entry = SL::DB::GLTransaction->new(
218     employee_id    => $employee_id,
219     transdate      => $transdate,
220     reference      => $reference,
221     description    => $description,
222     ob_transaction => $ob,
223     cb_transaction => $cb,
224   );
225   #$gl_entry->save;
226   my $kto_trans1 = SL::DB::AccTransaction->new(
227     trans_id       => $gl_entry->id,
228     transdate      => $transdate,
229     ob_transaction => $ob,
230     cb_transaction => $cb,
231     chart_id       => $gegenkonto->id,
232     chart_link     => $konto->link,
233     tax_id         => 0,
234     taxkey         => 0,
235     amount         => $amount,
236   );
237   #$kto_trans1->save;
238   my $kto_trans2 = SL::DB::AccTransaction->new(
239     trans_id       => $gl_entry->id,
240     transdate      => $transdate,
241     ob_transaction => $ob,
242     cb_transaction => $cb,
243     chart_id       => $konto->id,
244     chart_link     => $konto->link,
245     tax_id         => 0,
246     taxkey         => 0,
247     amount         => -$amount,
248   );
249   #$kto_trans2->save;
250   $gl_entry->add_transactions($kto_trans1);
251   $gl_entry->add_transactions($kto_trans2);
252   $gl_entry->save;
253 }
254
255 sub init_cbob_chart     { $::form->{cbob_chart}                                    }
256 sub init_ob_date        { $::locale->parse_date_to_object($::form->{ob_date})      }
257 sub init_ob_reference   { $::form->{ob_reference}                                  }
258 sub init_ob_description { $::form->{ob_description}                                }
259 sub init_cb_startdate   { $::locale->parse_date_to_object($::form->{cb_startdate}) }
260 sub init_cb_date        { $::locale->parse_date_to_object($::form->{cb_date})      }
261 sub init_cb_reference   { $::form->{cb_reference}                                  }
262 sub init_cb_description { $::form->{cb_description}                                }
263
264 sub init_charts9000 {
265   SL::DB::Manager::Chart->get_all(  query => [ accno => { like => '9%'}] );
266 }
267
268 sub init_charts {
269   # wie geht 'not like' in rose ?
270   SL::DB::Manager::Chart->get_all(  query => [ \ "accno not like '9%'"], sort_by => 'accno ASC' );
271 }
272
273 1;