6f5adf7f15adf09fa92ab9fc8e5d5d7f88ccf42d
[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
15 use Rose::Object::MakeMethods::Generic (
16   scalar                  => [ qw(config) ],
17   'scalar --get_set_init' => [ qw(defaults) ],
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 }
51
52 sub action_edit {
53   my ($self) = @_;
54
55   $self->show_form(title     => t8('Edit taxzone'),
56                    CHARTLIST => SL::DB::TaxzoneChart->get_all_accounts_by_taxzone_id($self->config->id));
57 }
58
59 sub action_create {
60   my ($self) = @_;
61
62   $self->config(SL::DB::TaxZone->new());
63   $self->create_or_update;
64 }
65
66 sub action_update {
67   my ($self) = @_;
68
69   $self->create_or_update;
70 }
71
72 sub action_reorder {
73   my ($self) = @_;
74
75   SL::DB::TaxZone->reorder_list(@{ $::form->{tzone_id} || [] });
76
77   $self->render(\'', { type => 'json' });
78 }
79
80 #
81 # filters
82 #
83
84 sub check_auth {
85   $::auth->assert('config');
86 }
87
88 sub load_config {
89   my ($self) = @_;
90
91   $self->config(SL::DB::TaxZone->new(id => $::form->{id})->load);
92 }
93
94 #
95 # helpers
96 #
97
98 sub create_or_update {
99   my ($self) = @_;
100   my $is_new = !$self->config->id;
101
102   my $params = delete($::form->{config}) || { };
103   delete $params->{id};
104
105   $self->config->assign_attributes(%{ $params });
106
107   my @errors = $self->config->validate;
108
109   if (@errors) {
110     flash('error', @errors);
111     $self->show_form(title => $is_new ? t8('Add taxzone') : t8('Edit taxzone'));
112     return;
113   }
114
115   $self->config->save;
116   $self->config->obsolete($::form->{"obsolete"});
117
118   #Save taxzone_charts for new taxzones:
119   if ($is_new) {
120     my $buchungsgruppen = SL::DB::Manager::Buchungsgruppe->get_all_sorted();
121
122     foreach my $bg (@{ $buchungsgruppen }) {
123       my $taxzone_chart = SL::DB::Manager::TaxzoneChart->find_by_or_create(buchungsgruppen_id => $bg->id, taxzone_id => $self->config->id);
124
125       $taxzone_chart->taxzone_id($self->config->id);
126       $taxzone_chart->buchungsgruppen_id($bg->id);
127       $taxzone_chart->income_accno_id($::form->{"income_accno_id_" . $bg->id});
128       $taxzone_chart->expense_accno_id($::form->{"expense_accno_id_" . $bg->id});
129       $taxzone_chart->save;
130     }
131   }
132
133   flash_later('info', $is_new ? t8('The taxzone has been created.') : t8('The taxzone has been saved.'));
134   $self->redirect_to(action => 'list');
135 }
136
137 #
138 # initializers
139 #
140
141 sub init_defaults { SL::DB::Default->get };
142
143 1;