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