»with_transaction« anstelle von »do_transaction« verwenden
[kivitendo-erp.git] / SL / Controller / Taxzones.pm
1 package SL::Controller::Taxzones;
2
3 use strict;
4
5 use parent qw(SL::Controller::Base);
6
7 use SL::DB::TaxZone;
8 use SL::Helper::Flash;
9 use SL::Locale::String;
10 use SL::DB::Manager::Buchungsgruppe;
11 use SL::DB::Manager::TaxzoneChart;
12
13 use Rose::Object::MakeMethods::Generic (
14   scalar                  => [ qw(config) ],
15   'scalar --get_set_init' => [ qw(defaults) ],
16 );
17
18 __PACKAGE__->run_before('check_auth');
19 __PACKAGE__->run_before('load_config', only => [ qw(edit update delete) ]);
20
21 #
22 # actions
23 #
24
25 sub action_list {
26   my ($self) = @_;
27
28   my $taxzones = SL::DB::Manager::TaxZone->get_all_sorted();
29
30   $self->render('taxzones/list',
31                 title    => t8('List of tax zones'),
32                 TAXZONES => $taxzones);
33 }
34
35 sub action_new {
36   my ($self) = @_;
37
38   $self->config(SL::DB::TaxZone->new());
39   $self->show_form(title => t8('Add taxzone'));
40 }
41
42 sub show_form {
43   my ($self, %params) = @_;
44
45   $self->render('taxzones/form', %params,
46                 BUCHUNGSGRUPPEN => SL::DB::Manager::Buchungsgruppe->get_all_sorted);
47 }
48
49 sub action_edit {
50   my ($self) = @_;
51
52   $self->show_form(title     => t8('Edit taxzone'),
53                    CHARTLIST => SL::DB::TaxzoneChart->get_all_accounts_by_taxzone_id($self->config->id));
54 }
55
56 sub action_create {
57   my ($self) = @_;
58
59   $self->config(SL::DB::TaxZone->new());
60   $self->create_or_update;
61 }
62
63 sub action_update {
64   my ($self) = @_;
65
66   $self->create_or_update;
67 }
68
69 sub action_delete {
70   my ($self) = @_;
71
72   # allow deletion of unused tax zones. Will fail, due to database
73   # constraints, if tax zone is used anywhere
74
75   $self->{config}->db->with_transaction(sub {
76     my $taxzone_charts = SL::DB::Manager::TaxzoneChart->get_all(where => [ taxzone_id => $self->config->id ]);
77     foreach my $taxzonechart ( @{$taxzone_charts} ) { $taxzonechart->delete };
78     $self->config->delete();
79     flash_later('info',  $::locale->text('The tax zone has been deleted.'));
80
81     1;
82   }) || flash_later('error', $::locale->text('The tax zone is in use and cannot be deleted.'));
83
84   $self->redirect_to(action => 'list');
85
86 }
87
88 sub action_reorder {
89   my ($self) = @_;
90
91   SL::DB::TaxZone->reorder_list(@{ $::form->{tzone_id} || [] });
92
93   $self->render(\'', { type => 'json' });
94 }
95
96 #
97 # filters
98 #
99
100 sub check_auth {
101   $::auth->assert('config');
102 }
103
104 sub load_config {
105   my ($self) = @_;
106
107   $self->config(SL::DB::TaxZone->new(id => $::form->{id})->load);
108 }
109
110 #
111 # helpers
112 #
113
114 sub create_or_update {
115   my ($self) = @_;
116   my $is_new = !$self->config->id;
117
118   my $params = delete($::form->{config}) || { };
119
120   delete $params->{id};
121
122   my @errors;
123
124   my $db = $self->config->db;
125   if (!$db->with_transaction(sub {
126
127     # always allow editing of description and obsolete
128     $self->config->assign_attributes( %{$params} ) ;
129
130     push(@errors, $self->config->validate); # check for description
131
132     if (@errors) {
133       die @errors . "\n";
134     };
135
136     $self->config->save;
137
138     if ( $is_new or $self->config->orphaned ) {
139       # Save taxzone_charts
140       my $buchungsgruppen = SL::DB::Manager::Buchungsgruppe->get_all_sorted();
141
142       foreach my $bg (@{ $buchungsgruppen }) {
143         my $income_accno_id  = $::form->{"income_accno_id_"  . $bg->id};
144         my $expense_accno_id = $::form->{"expense_accno_id_" . $bg->id};
145
146         my ($income_accno, $expense_accno);
147         $income_accno  = SL::DB::Manager::Chart->find_by( id => $income_accno_id )  if $income_accno_id;
148         $expense_accno = SL::DB::Manager::Chart->find_by( id => $expense_accno_id ) if $expense_accno_id;
149
150         push(@errors, t8('Booking group #1 needs a valid income account' , $bg->description)) unless $income_accno;
151         push(@errors, t8('Booking group #1 needs a valid expense account', $bg->description)) unless $expense_accno;
152
153         my $taxzone_chart = SL::DB::Manager::TaxzoneChart->find_by_or_create(buchungsgruppen_id => $bg->id, taxzone_id => $self->config->id);
154         # if taxzonechart doesn't exist an empty new TaxzoneChart object is
155         # created by find_by_or_create, so we have to assign buchungsgruppe and
156         # taxzone again for the new case to work
157         $taxzone_chart->taxzone_id($self->config->id);
158         $taxzone_chart->buchungsgruppen_id($bg->id);
159         $taxzone_chart->income_accno_id($income_accno->id);
160         $taxzone_chart->expense_accno_id($expense_accno->id);
161         $taxzone_chart->save;
162       }
163     }
164
165     1;
166   })) {
167     die @errors ? join("\n", @errors) . "\n" : $db->error . "\n";
168     # die with rollback of taxzone save if saving of any of the taxzone_charts fails
169     # only show the $db->error if we haven't already identified the likely error ourselves
170   }
171
172   flash_later('info', $is_new ? t8('The taxzone has been created.') : t8('The taxzone has been saved.'));
173   $self->redirect_to(action => 'list');
174 }
175
176 #
177 # initializers
178 #
179
180 sub init_defaults { SL::DB::Default->get };
181
182 1;