Steuerzonen ungültig machen
[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(query => [ obsolete => 0 ]);
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   # check whether buchungsgruppe is assigned to any parts
63   my $number_of_parts_with_buchungsgruppe = SL::DB::Manager::Part->get_objects_count(where => [ buchungsgruppen_id => $self->config->id]);
64
65   $self->show_form(title     => t8('Edit Buchungsgruppe'),
66                    linked_parts  => $number_of_parts_with_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_reorder {
83   my ($self) = @_;
84
85   SL::DB::Buchungsgruppe->reorder_list(@{ $::form->{bg_id} || [] });
86
87   $self->render(\'', { type => 'json' });
88 }
89
90 #
91 # filters
92 #
93
94 sub check_auth {
95   $::auth->assert('config');
96 }
97
98 sub load_config {
99   my ($self) = @_;
100
101   $self->config(SL::DB::Buchungsgruppe->new(id => $::form->{id})->load);
102 }
103
104 #
105 # helpers
106 #
107
108 sub create_or_update {
109   my ($self) = @_;
110   my $is_new = !$self->config->id;
111
112   my $params = delete($::form->{config}) || { };
113   delete $params->{id};
114
115   $self->config->assign_attributes(%{ $params });
116
117   my @errors = $self->config->validate;
118
119   if (@errors) {
120     flash('error', @errors);
121     $self->show_form(title => $is_new ? t8('Add taxzone') : t8('Edit taxzone'));
122     return;
123   }
124
125   $self->config->save;
126
127   # check whether there are any assigned parts 
128   my $number_of_parts_with_buchungsgruppe = SL::DB::Manager::Part->get_objects_count(where => [ buchungsgruppen_id => $self->config->id]);
129
130   # Save or update taxzone_charts:
131   if ($is_new or $number_of_parts_with_buchungsgruppe == 0) {
132     my $taxzones = SL::DB::Manager::TaxZone->get_all_sorted();
133
134     foreach my $tz (@{ $taxzones }) {
135       my $taxzone_chart = SL::DB::Manager::TaxzoneChart->find_by_or_create(buchungsgruppen_id => $self->config->id, taxzone_id => $tz->id);
136       $taxzone_chart->taxzone_id($tz->id);
137       $taxzone_chart->buchungsgruppen_id($self->config->id);
138       $taxzone_chart->income_accno_id($::form->{"income_accno_id_" . $tz->id});
139       $taxzone_chart->expense_accno_id($::form->{"expense_accno_id_" . $tz->id});
140       $taxzone_chart->save;
141     }
142   }
143
144   flash_later('info', $is_new ? t8('The Buchungsgruppe has been created.') : t8('The Buchungsgruppe has been saved.'));
145   $self->redirect_to(action => 'list');
146 }
147
148 1;