1 package SL::Controller::Taxzones;
 
   5 use parent qw(SL::Controller::Base);
 
   7 #use List::Util qw(first);
 
  10 use SL::Helper::Flash;
 
  11 use SL::Locale::String;
 
  12 use SL::DB::Manager::Buchungsgruppe;
 
  13 use SL::DB::Manager::TaxzoneChart;
 
  15 use Rose::Object::MakeMethods::Generic (
 
  16   scalar                  => [ qw(config) ],
 
  17   'scalar --get_set_init' => [ qw(defaults) ],
 
  20 __PACKAGE__->run_before('check_auth');
 
  21 __PACKAGE__->run_before('load_config', only => [ qw(edit update) ]); #destroy
 
  30   my $taxzones = SL::DB::Manager::TaxZone->get_all_sorted();
 
  33   $self->render('taxzones/list',
 
  34                 title    => t8('List of tax zones'),
 
  35                 TAXZONES => $taxzones);
 
  41   $self->config(SL::DB::TaxZone->new());
 
  42   $self->show_form(title => t8('Add taxzone'));
 
  46   my ($self, %params) = @_;
 
  48   $self->render('taxzones/form', %params,
 
  49                 BUCHUNGSGRUPPEN => SL::DB::Manager::Buchungsgruppe->get_all_sorted);
 
  55   $self->show_form(title     => t8('Edit taxzone'),
 
  56                    CHARTLIST => SL::DB::TaxzoneChart->get_all_accounts_by_taxzone_id($self->config->id));
 
  62   $self->config(SL::DB::TaxZone->new());
 
  63   $self->create_or_update;
 
  69   $self->create_or_update;
 
  75   SL::DB::TaxZone->reorder_list(@{ $::form->{tzone_id} || [] });
 
  77   $self->render(\'', { type => 'json' });
 
  85   $::auth->assert('config');
 
  91   $self->config(SL::DB::TaxZone->new(id => $::form->{id})->load);
 
  98 sub create_or_update {
 
 100   my $is_new = !$self->config->id;
 
 102   my $params = delete($::form->{config}) || { };
 
 103   delete $params->{id};
 
 105   $self->config->assign_attributes(%{ $params });
 
 107   my @errors = $self->config->validate;
 
 110     flash('error', @errors);
 
 111     $self->show_form(title => $is_new ? t8('Add taxzone') : t8('Edit taxzone'));
 
 116   $self->config->obsolete($::form->{"obsolete"});
 
 118   #Save taxzone_charts for new taxzones:
 
 120     my $buchungsgruppen = SL::DB::Manager::Buchungsgruppe->get_all_sorted();
 
 122     foreach my $bg (@{ $buchungsgruppen }) {
 
 123       my $taxzone_chart = SL::DB::Manager::TaxzoneChart->find_by_or_create(buchungsgruppen_id => $bg->id, taxzone_id => $self->config->id);
 
 125       $taxzone_chart->taxzone_id($self->config->id);
 
 126       $taxzone_chart->buchungsgruppen_id($bg->id);
 
 127       $taxzone_chart->income_accno_id($::form->{"income_accno_id_" . $bg->id});
 
 128       $taxzone_chart->expense_accno_id($::form->{"expense_accno_id_" . $bg->id});
 
 129       $taxzone_chart->save;
 
 133   flash_later('info', $is_new ? t8('The taxzone has been created.') : t8('The taxzone has been saved.'));
 
 134   $self->redirect_to(action => 'list');
 
 141 sub init_defaults { SL::DB::Default->get };