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;
 
  32   $self->render('gl/yearend_filter',
 
  33                 title               => t8('CB/OB Transactions'),
 
  34                 make_title_of_chart => sub { $_[0]->accno.' '.$_[0]->description }
 
  41   $main::lxdebug->enter_sub();
 
  43   my $report     = SL::ReportGenerator->new(\%::myconfig, $::form);
 
  45   $self->prepare_report($report);
 
  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'),
 
  55   $report->generate_with_headers();
 
  56   $main::lxdebug->leave_sub();
 
  62   my $cnt = $self->make_booking();
 
  64   flash('info', $::locale->text('#1 CB transactions and #1 OB transactions generated.',$cnt)) if $cnt > 0;
 
  70   $::auth->assert('general_ledger');
 
  79   $main::lxdebug->enter_sub();
 
  80   my @ids = map { $::form->{"multi_id_$_"} } grep { $::form->{"multi_id_$_"} } (1..$::form->{rowcount});
 
  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 ) {
 
  91         $self->gl_booking($balance,$self->cb_date,$::form->{cb_reference},$::form->{cb_description},$chart,$carryoverchart,0,1);
 
  93         $self->gl_booking($balance,$self->ob_date,$::form->{ob_reference},$::form->{ob_description},$carryoverchart,$chart,1,0);
 
  98   $main::lxdebug->leave_sub();
 
 104   my ($self,$report) = @_;
 
 105   $main::lxdebug->enter_sub();
 
 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
 
 117   my @columns      = qw(ids chart description saldo sum_cb sum_ob);
 
 118   map { $column_defs{$_}->{visible} = 1 } @columns;
 
 120   my $ob_next_date = $self->ob_date->clone();
 
 121   $ob_next_date->add(years => 1)->add(days => -1);
 
 123   $self->cb_startdate($::locale->parse_date_to_object($self->get_balance_starting_date($self->cb_date)));
 
 125   my @custom_headers = ();
 
 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'},
 
 135   map { push @line_2 , $column_defs{$_} } grep { $column_defs{$_}->{visible} } @columns;
 
 136   push @custom_headers, [ @line_2 ];
 
 138   $report->set_custom_headers(@custom_headers);
 
 139   $report->set_columns(%column_defs);
 
 140   $report->set_column_order(@columns);
 
 142   my $chart9actual = SL::DB::Manager::Chart->get_first( query => [ id => $self->cbob_chart ] );
 
 143   $self->{cbob_chartaccno} = $chart9actual->accno.' '.$chart9actual->description;
 
 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 };
 
 151         'raw_data' => $self->presenter->checkbox_tag("multi_id_${idx}", value => $chart_id, "data-checkall" => 1),
 
 152         'valign'   => 'center',
 
 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";
 
 162         $row->{saldo}->{data} = $::form->format_amount(\%::myconfig,0, 2)."  ";
 
 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 }
 
 169         $sum_cb += $acc->amount;
 
 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 }
 
 176         $sum_ob += $acc->amount;
 
 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";
 
 183         $row->{sum_cb}->{data} = $::form->format_amount(\%::myconfig,0, 2)."  ";
 
 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";
 
 190         $row->{sum_ob}->{data} = $::form->format_amount(\%::myconfig,0, 2)."  ";
 
 192       $report->add_data($row);
 
 197   $self->{row_count} = $idx;
 
 198   $main::lxdebug->leave_sub();
 
 202   $main::lxdebug->enter_sub();
 
 203   my ($self,$chart) = @_;
 
 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;
 
 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,
 
 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,
 
 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,
 
 250   $gl_entry->add_transactions($kto_trans1);
 
 251   $gl_entry->add_transactions($kto_trans2);
 
 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}                                }
 
 264 sub init_charts9000 {
 
 265   SL::DB::Manager::Chart->get_all(  query => [ accno => { like => '9%'}] );
 
 269   # wie geht 'not like' in rose ?
 
 270   SL::DB::Manager::Chart->get_all(  query => [ \ "accno not like '9%'"], sort_by => 'accno ASC' );