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