3f935824c9e97da7067077a95d90c081ac530064
[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   $::form->header;
37   $self->render('buchungsgruppen/list',
38                 title           => t8('Buchungsgruppen'),
39                 BUCHUNGSGRUPPEN => $buchungsgruppen,
40                 CHARTLIST       => \%chartlist,
41                 TAXZONES        => $taxzones);
42 }
43
44 sub action_new {
45   my ($self) = @_;
46
47   $self->config(SL::DB::Buchungsgruppe->new());
48   $self->show_form(title => t8('Add Buchungsgruppe'));
49 }
50
51 sub show_form {
52   my ($self, %params) = @_;
53
54   $self->render('buchungsgruppen/form', %params,
55                  TAXZONES       => SL::DB::Manager::TaxZone->get_all_sorted());
56 }
57
58 sub action_edit {
59   my ($self) = @_;
60
61   # Allow editing of the charts of the Buchungsgruppe if it isn't assigned to
62   # any parts. This is checked inside the template via the Buchungsgruppen
63   # orphaned method, where an IF-ELSE statement toggles between L.select_tag
64   # and text.
65
66   $self->show_form(title     => t8('Edit Buchungsgruppe'),
67                    CHARTLIST => SL::DB::TaxzoneChart->get_all_accounts_by_buchungsgruppen_id($self->config->id));
68 }
69
70 sub action_create {
71   my ($self) = @_;
72
73   $self->config(SL::DB::Buchungsgruppe->new());
74   $self->create_or_update;
75 }
76
77 sub action_update {
78   my ($self) = @_;
79   $self->create_or_update;
80 }
81
82 sub action_delete {
83   my ($self) = @_;
84
85   # allow deletion of unused Buchungsgruppen. Will fail, due to database
86   # constraint, if Buchungsgruppe is connected to a part
87
88   my $db = $self->{config}->db;
89   $db->do_transaction(sub {
90         my $taxzone_charts = SL::DB::Manager::TaxzoneChart->get_all(where => [ buchungsgruppen_id => $self->config->id ]);
91         foreach my $taxzonechart ( @{$taxzone_charts} ) { $taxzonechart->delete };
92         $self->config->delete();
93         flash_later('info',  $::locale->text('The buchungsgruppe has been deleted.'));
94   }) || flash_later('error', $::locale->text('The buchungsgruppe is in use and cannot be deleted.'));
95
96   $self->redirect_to(action => 'list');
97
98 }
99
100 sub action_reorder {
101   my ($self) = @_;
102
103   SL::DB::Buchungsgruppe->reorder_list(@{ $::form->{bg_id} || [] });
104
105   $self->render(\'', { type => 'json' });
106 }
107
108 #
109 # filters
110 #
111
112 sub check_auth {
113   $::auth->assert('config');
114 }
115
116 sub load_config {
117   my ($self) = @_;
118
119   $self->config(SL::DB::Buchungsgruppe->new(id => $::form->{id})->load);
120 }
121
122 #
123 # helpers
124 #
125
126 sub create_or_update {
127   my ($self) = @_;
128   my $is_new = !$self->config->id;
129
130   my $params = delete($::form->{config}) || { };
131   delete $params->{id};
132
133   my @errors;
134
135   my $db = $self->config->db;
136   $db->do_transaction( sub {
137
138     $self->config->assign_attributes(%{ $params }); # assign description and inventory_accno_id
139
140     @errors = $self->config->validate; # check for description and inventory_accno_id
141
142     if (@errors) {
143       die "foo" . @errors . "\n";
144     };
145
146     $self->config->save;
147
148     # Save or update taxzone_charts for new or unused Buchungsgruppen
149     if ($is_new or $self->config->orphaned) {
150       my $taxzones = SL::DB::Manager::TaxZone->get_all_sorted();
151
152       foreach my $tz (@{ $taxzones }) {
153
154         my $income_accno_id    = $::form->{"income_accno_id_"  . $tz->id};
155         my $expense_accno_id   = $::form->{"expense_accno_id_" . $tz->id};
156
157         my ($income_accno, $expense_accno);
158         $income_accno    = SL::DB::Manager::Chart->find_by( id => $income_accno_id  ) if $income_accno_id;
159         $expense_accno   = SL::DB::Manager::Chart->find_by( id => $expense_accno_id ) if $expense_accno_id;
160
161         push(@errors, t8('Tax zone #1 needs a valid income account'   , $tz->description)) unless $income_accno;
162         push(@errors, t8('Tax zone #1 needs a valid expense account'  , $tz->description)) unless $expense_accno;
163
164         my $taxzone_chart = SL::DB::Manager::TaxzoneChart->find_by_or_create(buchungsgruppen_id => $self->config->id, taxzone_id => $tz->id);
165         $taxzone_chart->taxzone_id($tz->id);
166         $taxzone_chart->buchungsgruppen_id($self->config->id);
167         $taxzone_chart->income_accno_id($income_accno->id);
168         $taxzone_chart->expense_accno_id($expense_accno->id);
169         $taxzone_chart->save;
170       }
171     }
172   } ) || die @errors ? join("\n", @errors) . "\n" : $db->error . "\n";
173          # die with rollback of taxzone save if saving of any of the taxzone_charts fails
174          # only show the $db->error if we haven't already identified the likely error ourselves
175
176   flash_later('info', $is_new ? t8('The Buchungsgruppe has been created.') : t8('The Buchungsgruppe has been saved.'));
177   $self->redirect_to(action => 'list');
178 }
179
180 #
181 # initializers
182 #
183
184 sub init_defaults        { SL::DB::Default->get }
185
186 1;