Steuerzonen und Buchungsgruppen bearbeiten
[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
13 use Rose::Object::MakeMethods::Generic (
14   scalar                  => [ qw(config) ],
15 );
16
17 __PACKAGE__->run_before('check_auth');
18 __PACKAGE__->run_before('load_config', only => [ qw(edit update) ]); #destroy
19
20 #
21 # actions
22 #
23
24 sub action_list {
25   my ($self) = @_;
26
27   my $buchungsgruppen = SL::DB::Manager::Buchungsgruppe->get_all_sorted();
28   my $taxzones        = SL::DB::Manager::TaxZone->get_all_sorted();
29
30   my %chartlist = ();
31   foreach my $gruppe (@{ $buchungsgruppen }) {
32       $chartlist{ $gruppe->id } = SL::DB::TaxzoneChart->get_all_accounts_by_buchungsgruppen_id($gruppe->id);
33   }
34
35   $::form->header;
36   $self->render('buchungsgruppen/list',
37                 title           => t8('Buchungsgruppen'),
38                 BUCHUNGSGRUPPEN => $buchungsgruppen,
39                 CHARTLIST       => \%chartlist,
40                 TAXZONES        => $taxzones);
41 }
42
43 sub action_new {
44   my ($self) = @_;
45
46   $self->config(SL::DB::Buchungsgruppe->new());
47   $self->show_form(title => t8('Add Buchungsgruppe'));
48 }
49
50 sub show_form {
51   my ($self, %params) = @_;
52
53   $self->render('buchungsgruppen/form', %params,
54                  TAXZONES       => SL::DB::Manager::TaxZone->get_all_sorted(),
55                  ACCOUNTS       => SL::Controller::ClientConfig->init_accounts(),
56                  account_label  => sub { "$_[0]{accno}--$_[0]{description}" });
57 }
58
59 sub action_edit {
60   my ($self) = @_;
61
62   $self->show_form(title     => t8('Edit Buchungsgruppe'),
63                    CHARTLIST => SL::DB::TaxzoneChart->get_all_accounts_by_buchungsgruppen_id($self->config->id));
64 }
65
66 sub action_create {
67   my ($self) = @_;
68
69   $self->config(SL::DB::Buchungsgruppe->new());
70   $self->create_or_update;
71 }
72
73 sub action_update {
74   my ($self) = @_;
75   $self->create_or_update;
76 }
77
78 sub action_reorder {
79   my ($self) = @_;
80
81   SL::DB::Buchungsgruppe->reorder_list(@{ $::form->{bg_id} || [] });
82
83   $self->render(\'', { type => 'json' });
84 }
85
86 #
87 # filters
88 #
89
90 sub check_auth {
91   $::auth->assert('config');
92 }
93
94 sub load_config {
95   my ($self) = @_;
96
97   $self->config(SL::DB::Buchungsgruppe->new(id => $::form->{id})->load);
98 }
99
100 #
101 # helpers
102 #
103
104 sub create_or_update {
105   my ($self) = @_;
106   my $is_new = !$self->config->id;
107
108   my $params = delete($::form->{config}) || { };
109   delete $params->{id};
110
111   $self->config->assign_attributes(%{ $params });
112
113   my @errors = $self->config->validate;
114
115   if (@errors) {
116     flash('error', @errors);
117     $self->show_form(title => $is_new ? t8('Add taxzone') : t8('Edit taxzone'));
118     return;
119   }
120
121   $self->config->save;
122
123   #Save taxzone_charts:
124   if ($is_new) {
125     my $taxzones = SL::DB::Manager::TaxZone->get_all_sorted();
126
127     foreach my $tz (@{ $taxzones }) {
128       my $taxzone_chart = SL::DB::Manager::TaxzoneChart->find_by_or_create(buchungsgruppen_id => $self->config->id, taxzone_id => $tz->id);
129       $taxzone_chart->taxzone_id($tz->id);
130       $taxzone_chart->buchungsgruppen_id($self->config->id);
131       $taxzone_chart->income_accno_id($::form->{"income_accno_id_" . $tz->id});
132       $taxzone_chart->expense_accno_id($::form->{"expense_accno_id_" . $tz->id});
133       $taxzone_chart->save;
134     }
135   }
136
137   flash_later('info', $is_new ? t8('The Buchungsgruppe has been created.') : t8('The Buchungsgruppe has been saved.'));
138   $self->redirect_to(action => 'list');
139 }
140
141 1;