Merge branch 'master' of github.com:kivitendo/kivitendo-erp
[kivitendo-erp.git] / SL / Controller / Business.pm
1 package SL::Controller::Business;
2
3 use strict;
4
5 use parent qw(SL::Controller::Base);
6
7 use SL::DB::Business;
8 use SL::Helper::Flash;
9
10 use Rose::Object::MakeMethods::Generic
11 (
12  scalar => [ qw(business) ],
13 );
14
15 __PACKAGE__->run_before('check_auth');
16 __PACKAGE__->run_before('load_business', only => [ qw(edit update destroy) ]);
17
18 #
19 # actions
20 #
21
22 sub action_list {
23   my ($self) = @_;
24
25   $self->render('business/list',
26                 title       => $::locale->text('Businesses'),
27                 BUSINESSS => SL::DB::Manager::Business->get_all_sorted);
28 }
29
30 sub action_new {
31   my ($self) = @_;
32
33   $self->{business} = SL::DB::Business->new;
34   $self->render('business/form', title => $::locale->text('Create a new business'));
35 }
36
37 sub action_edit {
38   my ($self) = @_;
39   $self->render('business/form', title => $::locale->text('Edit business'));
40 }
41
42 sub action_create {
43   my ($self) = @_;
44
45   $self->{business} = SL::DB::Business->new;
46   $self->create_or_update;
47 }
48
49 sub action_update {
50   my ($self) = @_;
51   $self->create_or_update;
52 }
53
54 sub action_destroy {
55   my ($self) = @_;
56
57   if (eval { $self->{business}->delete; 1; }) {
58     flash_later('info',  $::locale->text('The business has been deleted.'));
59   } else {
60     flash_later('error', $::locale->text('The business is in use and cannot be deleted.'));
61   }
62
63   $self->redirect_to(action => 'list');
64 }
65
66 #
67 # filters
68 #
69
70 sub check_auth {
71   $::auth->assert('config');
72 }
73
74 #
75 # helpers
76 #
77
78 sub create_or_update {
79   my $self   = shift;
80   my $is_new = !$self->{business}->id;
81   my $params = delete($::form->{business}) || { };
82
83   $self->{business}->assign_attributes(%{ $params });
84
85   my @errors = $self->{business}->validate;
86
87   if (@errors) {
88     flash('error', @errors);
89     $self->render('business/form', title => $is_new ? $::locale->text('Create a new business') : $::locale->text('Edit business'));
90     return;
91   }
92
93   $self->{business}->save;
94
95   flash_later('info', $is_new ? $::locale->text('The business has been created.') : $::locale->text('The business has been saved.'));
96   $self->redirect_to(action => 'list');
97 }
98
99 sub load_business {
100   my ($self) = @_;
101   $self->{business} = SL::DB::Business->new(id => $::form->{id})->load;
102 }
103
104 1;