1 package SL::Controller::Taxzones;
5 use parent qw(SL::Controller::Base);
9 use SL::Locale::String;
10 use SL::DB::Manager::Buchungsgruppe;
11 use SL::DB::Manager::TaxzoneChart;
13 use Rose::Object::MakeMethods::Generic (
14 scalar => [ qw(config) ],
15 'scalar --get_set_init' => [ qw(defaults) ],
18 __PACKAGE__->run_before('check_auth');
19 __PACKAGE__->run_before('load_config', only => [ qw(edit update delete) ]);
28 my $taxzones = SL::DB::Manager::TaxZone->get_all_sorted();
30 $self->render('taxzones/list',
31 title => t8('List of tax zones'),
32 TAXZONES => $taxzones);
38 $self->config(SL::DB::TaxZone->new());
39 $self->show_form(title => t8('Add taxzone'));
43 my ($self, %params) = @_;
45 $self->render('taxzones/form', %params,
46 BUCHUNGSGRUPPEN => SL::DB::Manager::Buchungsgruppe->get_all_sorted);
52 $self->show_form(title => t8('Edit taxzone'),
53 CHARTLIST => SL::DB::TaxzoneChart->get_all_accounts_by_taxzone_id($self->config->id));
59 $self->config(SL::DB::TaxZone->new());
60 $self->create_or_update;
66 $self->create_or_update;
72 # allow deletion of unused tax zones. Will fail, due to database
73 # constraints, if tax zone is used anywhere
75 my $db = $self->{config}->db;
76 $db->do_transaction(sub {
77 my $taxzone_charts = SL::DB::Manager::TaxzoneChart->get_all(where => [ taxzone_id => $self->config->id ]);
78 foreach my $taxzonechart ( @{$taxzone_charts} ) { $taxzonechart->delete };
79 $self->config->delete();
80 flash_later('info', $::locale->text('The tax zone has been deleted.'));
81 }) || flash_later('error', $::locale->text('The tax zone is in use and cannot be deleted.'));
83 $self->redirect_to(action => 'list');
90 SL::DB::TaxZone->reorder_list(@{ $::form->{tzone_id} || [] });
92 $self->render(\'', { type => 'json' });
100 $::auth->assert('config');
106 $self->config(SL::DB::TaxZone->new(id => $::form->{id})->load);
113 sub create_or_update {
115 my $is_new = !$self->config->id;
117 my $params = delete($::form->{config}) || { };
119 delete $params->{id};
123 my $db = $self->config->db;
124 $db->do_transaction( sub {
126 # always allow editing of description and obsolete
127 $self->config->assign_attributes( %{$params} ) ;
129 push(@errors, $self->config->validate); # check for description
137 if ( $is_new or $self->config->orphaned ) {
138 # Save taxzone_charts
139 my $buchungsgruppen = SL::DB::Manager::Buchungsgruppe->get_all_sorted();
141 foreach my $bg (@{ $buchungsgruppen }) {
142 my $income_accno_id = $::form->{"income_accno_id_" . $bg->id};
143 my $expense_accno_id = $::form->{"expense_accno_id_" . $bg->id};
145 my ($income_accno, $expense_accno);
146 $income_accno = SL::DB::Manager::Chart->find_by( id => $income_accno_id ) if $income_accno_id;
147 $expense_accno = SL::DB::Manager::Chart->find_by( id => $expense_accno_id ) if $expense_accno_id;
149 push(@errors, t8('Booking group #1 needs a valid income account' , $bg->description)) unless $income_accno;
150 push(@errors, t8('Booking group #1 needs a valid expense account', $bg->description)) unless $expense_accno;
152 my $taxzone_chart = SL::DB::Manager::TaxzoneChart->find_by_or_create(buchungsgruppen_id => $bg->id, taxzone_id => $self->config->id);
153 # if taxzonechart doesn't exist an empty new TaxzoneChart object is
154 # created by find_by_or_create, so we have to assign buchungsgruppe and
155 # taxzone again for the new case to work
156 $taxzone_chart->taxzone_id($self->config->id);
157 $taxzone_chart->buchungsgruppen_id($bg->id);
158 $taxzone_chart->income_accno_id($income_accno->id);
159 $taxzone_chart->expense_accno_id($expense_accno->id);
160 $taxzone_chart->save;
163 } ) || die @errors ? join("\n", @errors) . "\n" : $db->error . "\n";
164 # die with rollback of taxzone save if saving of any of the taxzone_charts fails
165 # only show the $db->error if we haven't already identified the likely error ourselves
167 flash_later('info', $is_new ? t8('The taxzone has been created.') : t8('The taxzone has been saved.'));
168 $self->redirect_to(action => 'list');
175 sub init_defaults { SL::DB::Default->get };