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) ],
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,
50 ACCOUNTS => SL::Controller::ClientConfig->init_accounts,
51 account_label => sub { "$_[0]{accno}--$_[0]{description}" });
57 $self->show_form(title => t8('Edit custom variable'),
58 CHARTLIST => SL::DB::TaxzoneChart->get_all_accounts_by_taxzone_id($self->config->id));
64 $self->config(SL::DB::TaxZone->new());
65 $self->create_or_update;
71 $self->create_or_update;
77 SL::DB::TaxZone->reorder_list(@{ $::form->{tzone_id} || [] });
79 $self->render(\'', { type => 'json' });
87 $::auth->assert('config');
93 $self->config(SL::DB::TaxZone->new(id => $::form->{id})->load);
100 sub create_or_update {
102 my $is_new = !$self->config->id;
104 my $params = delete($::form->{config}) || { };
105 delete $params->{id};
107 $self->config->assign_attributes(%{ $params });
109 my @errors = $self->config->validate;
112 flash('error', @errors);
113 $self->show_form(title => $is_new ? t8('Add taxzone') : t8('Edit taxzone'));
119 #Save taxzone_charts for new taxzones:
121 my $buchungsgruppen = SL::DB::Manager::Buchungsgruppe->get_all_sorted();
123 foreach my $bg (@{ $buchungsgruppen }) {
124 my $taxzone_chart = SL::DB::Manager::TaxzoneChart->find_by_or_create(buchungsgruppen_id => $bg->id, taxzone_id => $self->config->id);
126 $taxzone_chart->taxzone_id($self->config->id);
127 $taxzone_chart->buchungsgruppen_id($bg->id);
128 $taxzone_chart->income_accno_id($::form->{"income_accno_id_" . $bg->id});
129 $taxzone_chart->expense_accno_id($::form->{"expense_accno_id_" . $bg->id});
130 $taxzone_chart->save;
134 flash_later('info', $is_new ? t8('The taxzone has been created.') : t8('The taxzone has been saved.'));
135 $self->redirect_to(action => 'list');