e0125695c10bcc5656fb535c8a955af2e5b39d94
[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->setup_list_action_bar;
31   $self->render('taxzones/list',
32                 title    => t8('List of tax zones'),
33                 TAXZONES => $taxzones);
34 }
35
36 sub action_new {
37   my ($self) = @_;
38
39   $self->config(SL::DB::TaxZone->new());
40   $self->show_form(title => t8('Add taxzone'));
41 }
42
43 sub show_form {
44   my ($self, %params) = @_;
45
46   $self->setup_show_form_action_bar;
47   $self->render('taxzones/form', %params,
48                 BUCHUNGSGRUPPEN => SL::DB::Manager::Buchungsgruppe->get_all_sorted);
49 }
50
51 sub action_edit {
52   my ($self) = @_;
53
54   $self->show_form(title     => t8('Edit taxzone'),
55                    CHARTLIST => SL::DB::TaxzoneChart->get_all_accounts_by_taxzone_id($self->config->id));
56 }
57
58 sub action_create {
59   my ($self) = @_;
60
61   $self->config(SL::DB::TaxZone->new());
62   $self->create_or_update;
63 }
64
65 sub action_update {
66   my ($self) = @_;
67
68   $self->create_or_update;
69 }
70
71 sub action_delete {
72   my ($self) = @_;
73
74   # allow deletion of unused tax zones. Will fail, due to database
75   # constraints, if tax zone is used anywhere
76
77   $self->{config}->db->with_transaction(sub {
78     my $taxzone_charts = SL::DB::Manager::TaxzoneChart->get_all(where => [ taxzone_id => $self->config->id ]);
79     foreach my $taxzonechart ( @{$taxzone_charts} ) { $taxzonechart->delete };
80     $self->config->delete();
81     flash_later('info',  $::locale->text('The tax zone has been deleted.'));
82
83     1;
84   }) || flash_later('error', $::locale->text('The tax zone is in use and cannot be deleted.'));
85
86   $self->redirect_to(action => 'list');
87
88 }
89
90 sub action_reorder {
91   my ($self) = @_;
92
93   SL::DB::TaxZone->reorder_list(@{ $::form->{tzone_id} || [] });
94
95   $self->render(\'', { type => 'json' });
96 }
97
98 #
99 # filters
100 #
101
102 sub check_auth {
103   $::auth->assert('config');
104 }
105
106 sub load_config {
107   my ($self) = @_;
108
109   $self->config(SL::DB::TaxZone->new(id => $::form->{id})->load);
110 }
111
112 #
113 # helpers
114 #
115
116 sub create_or_update {
117   my ($self) = @_;
118   my $is_new = !$self->config->id;
119
120   my $params = delete($::form->{config}) || { };
121
122   delete $params->{id};
123
124   my @errors;
125
126   my $db = $self->config->db;
127   if (!$db->with_transaction(sub {
128
129     # always allow editing of description and obsolete
130     $self->config->assign_attributes( %{$params} ) ;
131
132     push(@errors, $self->config->validate); # check for description
133
134     if (@errors) {
135       die @errors . "\n";
136     };
137
138     $self->config->save;
139
140     if ( $is_new or $self->config->orphaned ) {
141       # Save taxzone_charts
142       my $buchungsgruppen = SL::DB::Manager::Buchungsgruppe->get_all_sorted();
143
144       foreach my $bg (@{ $buchungsgruppen }) {
145         my $income_accno_id  = $::form->{"income_accno_id_"  . $bg->id};
146         my $expense_accno_id = $::form->{"expense_accno_id_" . $bg->id};
147
148         my ($income_accno, $expense_accno);
149         $income_accno  = SL::DB::Manager::Chart->find_by( id => $income_accno_id )  if $income_accno_id;
150         $expense_accno = SL::DB::Manager::Chart->find_by( id => $expense_accno_id ) if $expense_accno_id;
151
152         push(@errors, t8('Booking group #1 needs a valid income account' , $bg->description)) unless $income_accno;
153         push(@errors, t8('Booking group #1 needs a valid expense account', $bg->description)) unless $expense_accno;
154
155         my $taxzone_chart = SL::DB::Manager::TaxzoneChart->find_by_or_create(buchungsgruppen_id => $bg->id, taxzone_id => $self->config->id);
156         # if taxzonechart doesn't exist an empty new TaxzoneChart object is
157         # created by find_by_or_create, so we have to assign buchungsgruppe and
158         # taxzone again for the new case to work
159         $taxzone_chart->taxzone_id($self->config->id);
160         $taxzone_chart->buchungsgruppen_id($bg->id);
161         $taxzone_chart->income_accno_id($income_accno->id);
162         $taxzone_chart->expense_accno_id($expense_accno->id);
163         $taxzone_chart->save;
164       }
165     }
166
167     1;
168   })) {
169     die @errors ? join("\n", @errors) . "\n" : $db->error . "\n";
170     # die with rollback of taxzone save if saving of any of the taxzone_charts fails
171     # only show the $db->error if we haven't already identified the likely error ourselves
172   }
173
174   flash_later('info', $is_new ? t8('The taxzone has been created.') : t8('The taxzone has been saved.'));
175   $self->redirect_to(action => 'list');
176 }
177
178 #
179 # initializers
180 #
181
182 sub init_defaults { SL::DB::Default->get };
183
184 #
185 # helpers
186 #
187
188 sub setup_show_form_action_bar {
189   my ($self) = @_;
190
191   my $is_new = !$self->config->id;
192
193   for my $bar ($::request->layout->get('actionbar')) {
194     $bar->add(
195       action => [
196         t8('Save'),
197         submit    => [ '#form', { action => 'Taxzones/' . ($is_new ? 'create' : 'update') } ],
198         checks    => [ 'kivi.validate_form' ],
199         accesskey => 'enter',
200       ],
201
202       action => [
203         t8('Delete'),
204         submit   => [ '#form', { action => 'Taxzones/delete' } ],
205         confirm  => t8('Do you really want to delete this object?'),
206         disabled => $is_new                  ? t8('This object has not been saved yet.')
207                   : !$self->config->orphaned ? t8('The object is in use and cannot be deleted.')
208                   :                            undef,
209       ],
210
211       link => [
212         t8('Abort'),
213         link => $self->url_for(action => 'list'),
214       ],
215     );
216   }
217 }
218
219 sub setup_list_action_bar {
220   my ($self) = @_;
221
222   for my $bar ($::request->layout->get('actionbar')) {
223     $bar->add(
224       link => [
225         t8('Add'),
226         link => $self->url_for(action => 'new'),
227       ],
228     );
229   }
230 }
231
232 1;