3ecba1369e45605938c5e6a4ce2956191571d9e6
[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::Controller::ClientConfig;
12 use SL::DB::Default;
13
14 use Rose::Object::MakeMethods::Generic (
15   scalar                  => [ qw(config) ],
16   'scalar --get_set_init' => [ qw(defaults) ],
17 );
18
19 __PACKAGE__->run_before('check_auth');
20 __PACKAGE__->run_before('load_config', only => [ qw(edit update delete) ]);
21
22 #
23 # actions
24 #
25
26 sub action_list {
27   my ($self) = @_;
28
29   my $buchungsgruppen = SL::DB::Manager::Buchungsgruppe->get_all_sorted();
30   my $taxzones        = SL::DB::Manager::TaxZone->get_all_sorted(query => [ obsolete => 0 ]);
31
32   my %chartlist = ();
33   foreach my $gruppe (@{ $buchungsgruppen }) {
34       $chartlist{ $gruppe->id } = SL::DB::TaxzoneChart->get_all_accounts_by_buchungsgruppen_id($gruppe->id);
35   }
36
37   $::form->header;
38   $self->render('buchungsgruppen/list',
39                 title           => t8('Buchungsgruppen'),
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 Buchungsgruppe'));
50 }
51
52 sub show_form {
53   my ($self, %params) = @_;
54
55   $self->render('buchungsgruppen/form', %params,
56                  TAXZONES       => SL::DB::Manager::TaxZone->get_all_sorted(),
57                  ACCOUNTS       => SL::Controller::ClientConfig->init_accounts(),
58                  account_label  => sub { "$_[0]{accno}--$_[0]{description}" });
59 }
60
61 sub action_edit {
62   my ($self) = @_;
63
64   # Allow editing of Buchungsgruppe if it isn't assigned to any parts. The
65   # variable is checked in the template, which toggles between L.select_tag and
66   # text.
67
68   my $number_of_parts_with_buchungsgruppe = SL::DB::Manager::Part->get_objects_count(where => [ buchungsgruppen_id => $self->config->id]);
69
70   $self->show_form(title     => t8('Edit Buchungsgruppe'),
71                    linked_parts  => $number_of_parts_with_buchungsgruppe,
72                    CHARTLIST => SL::DB::TaxzoneChart->get_all_accounts_by_buchungsgruppen_id($self->config->id));
73 }
74
75 sub action_create {
76   my ($self) = @_;
77
78   $self->config(SL::DB::Buchungsgruppe->new());
79   $self->create_or_update;
80 }
81
82 sub action_update {
83   my ($self) = @_;
84   $self->create_or_update;
85 }
86
87 sub action_delete {
88   my ($self) = @_;
89
90   # allow deletion of unused Buchungsgruppen. Will fail, due to database
91   # constraint, if Buchungsgruppe is connected to a part
92
93   my $db = $self->{config}->db;
94   $db->do_transaction(sub {    
95         my $taxzone_charts = SL::DB::Manager::TaxzoneChart->get_all(where => [ buchungsgruppen_id => $self->config->id ]);
96         foreach my $taxzonechart ( @{$taxzone_charts} ) { $taxzonechart->delete };
97         $self->config->delete();
98         flash_later('info',  $::locale->text('The buchungsgruppe has been deleted.'));
99   }) || flash_later('error', $::locale->text('The buchungsgruppe is in use and cannot be deleted.'));
100
101   $self->redirect_to(action => 'list');
102
103 }
104
105 sub action_reorder {
106   my ($self) = @_;
107
108   SL::DB::Buchungsgruppe->reorder_list(@{ $::form->{bg_id} || [] });
109
110   $self->render(\'', { type => 'json' });
111 }
112
113 #
114 # filters
115 #
116
117 sub check_auth {
118   $::auth->assert('config');
119 }
120
121 sub load_config {
122   my ($self) = @_;
123
124   $self->config(SL::DB::Buchungsgruppe->new(id => $::form->{id})->load);
125 }
126
127 #
128 # helpers
129 #
130
131 sub create_or_update {
132   my ($self) = @_;
133   my $is_new = !$self->config->id;
134
135   my $params = delete($::form->{config}) || { };
136   delete $params->{id};
137
138   $self->config->assign_attributes(%{ $params });
139
140   my @errors = $self->config->validate;
141
142   if (@errors) {
143     flash('error', @errors);
144     $self->show_form(title => $is_new ? t8('Add taxzone') : t8('Edit taxzone'));
145     return;
146   }
147
148   $self->config->save;
149
150   # check whether there are any assigned parts 
151   my $number_of_parts_with_buchungsgruppe = SL::DB::Manager::Part->get_objects_count(where => [ buchungsgruppen_id => $self->config->id]);
152
153   # Save or update taxzone_charts:
154   if ($is_new or $number_of_parts_with_buchungsgruppe == 0) {
155     my $taxzones = SL::DB::Manager::TaxZone->get_all_sorted();
156
157     foreach my $tz (@{ $taxzones }) {
158       my $taxzone_chart = SL::DB::Manager::TaxzoneChart->find_by_or_create(buchungsgruppen_id => $self->config->id, taxzone_id => $tz->id);
159       $taxzone_chart->taxzone_id($tz->id);
160       $taxzone_chart->buchungsgruppen_id($self->config->id);
161       $taxzone_chart->income_accno_id($::form->{"income_accno_id_" . $tz->id});
162       $taxzone_chart->expense_accno_id($::form->{"expense_accno_id_" . $tz->id});
163       $taxzone_chart->save;
164     }
165   }
166
167   flash_later('info', $is_new ? t8('The Buchungsgruppe has been created.') : t8('The Buchungsgruppe has been saved.'));
168   $self->redirect_to(action => 'list');
169 }
170
171 #
172 # initializers
173 #
174
175 sub init_defaults        { SL::DB::Default->get }
176
177 1;