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