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;
 
  14 use SL::Controller::ClientConfig;
 
  16 use Rose::Object::MakeMethods::Generic (
 
  17   scalar                  => [ qw(config) ],
 
  18   'scalar --get_set_init' => [ qw(defaults) ],
 
  21 __PACKAGE__->run_before('check_auth');
 
  22 __PACKAGE__->run_before('load_config', only => [ qw(edit update) ]); #destroy
 
  31   my $taxzones = SL::DB::Manager::TaxZone->get_all_sorted();
 
  34   $self->render('taxzones/list',
 
  35                 title    => t8('List of tax zones'),
 
  36                 TAXZONES => $taxzones);
 
  42   $self->config(SL::DB::TaxZone->new());
 
  43   $self->show_form(title => t8('Add taxzone'));
 
  47   my ($self, %params) = @_;
 
  49   $self->render('taxzones/form', %params,
 
  50                 BUCHUNGSGRUPPEN => SL::DB::Manager::Buchungsgruppe->get_all_sorted,
 
  51                 ACCOUNTS        => SL::Controller::ClientConfig->init_accounts,
 
  52                 account_label   => sub { "$_[0]{accno}--$_[0]{description}" });
 
  58   $self->show_form(title     => t8('Edit taxzone'),
 
  59                    CHARTLIST => SL::DB::TaxzoneChart->get_all_accounts_by_taxzone_id($self->config->id));
 
  65   $self->config(SL::DB::TaxZone->new());
 
  66   $self->create_or_update;
 
  72   $self->create_or_update;
 
  78   SL::DB::TaxZone->reorder_list(@{ $::form->{tzone_id} || [] });
 
  80   $self->render(\'', { type => 'json' });
 
  88   $::auth->assert('config');
 
  94   $self->config(SL::DB::TaxZone->new(id => $::form->{id})->load);
 
 101 sub create_or_update {
 
 103   my $is_new = !$self->config->id;
 
 105   my $params = delete($::form->{config}) || { };
 
 106   delete $params->{id};
 
 108   $self->config->assign_attributes(%{ $params });
 
 110   my @errors = $self->config->validate;
 
 113     flash('error', @errors);
 
 114     $self->show_form(title => $is_new ? t8('Add taxzone') : t8('Edit taxzone'));
 
 119   $self->config->obsolete($::form->{"obsolete"});
 
 121   #Save taxzone_charts for new taxzones:
 
 123     my $buchungsgruppen = SL::DB::Manager::Buchungsgruppe->get_all_sorted();
 
 125     foreach my $bg (@{ $buchungsgruppen }) {
 
 126       my $taxzone_chart = SL::DB::Manager::TaxzoneChart->find_by_or_create(buchungsgruppen_id => $bg->id, taxzone_id => $self->config->id);
 
 128       $taxzone_chart->taxzone_id($self->config->id);
 
 129       $taxzone_chart->buchungsgruppen_id($bg->id);
 
 130       $taxzone_chart->income_accno_id($::form->{"income_accno_id_" . $bg->id});
 
 131       $taxzone_chart->expense_accno_id($::form->{"expense_accno_id_" . $bg->id});
 
 132       $taxzone_chart->save;
 
 136   flash_later('info', $is_new ? t8('The taxzone has been created.') : t8('The taxzone has been saved.'));
 
 137   $self->redirect_to(action => 'list');
 
 144 sub init_defaults { SL::DB::Default->get };