epic-s6ts
[kivitendo-erp.git] / SL / Controller / Shop.pm
1 package SL::Controller::Shop;
2
3 use strict;
4
5 use parent qw(SL::Controller::Base);
6
7 use SL::Helper::Flash;
8 use SL::Locale::String;
9 use SL::DB::Default;
10 use SL::DB::Manager::Shop;
11 use SL::DB::Pricegroup;
12 use SL::DB::TaxZone;
13
14 use Rose::Object::MakeMethods::Generic (
15   scalar                  => [ qw(connectors price_types price_sources taxzone_id protocols) ],
16   'scalar --get_set_init' => [ qw(shop) ]
17 );
18
19 __PACKAGE__->run_before('check_auth');
20 __PACKAGE__->run_before('load_types',    only => [ qw(new edit) ]);
21
22 #
23 # actions
24 #
25
26 sub action_list {
27   my ($self) = @_;
28
29   $self->_setup_list_action_bar;
30   $self->render('shops/list',
31                 title => t8('Shops'),
32                 SHOPS => SL::DB::Manager::Shop->get_all_sorted,
33                );
34 }
35
36 sub action_edit {
37   my ($self) = @_;
38
39   my $is_new = !$self->shop->id;
40   $self->_setup_form_action_bar;
41   $self->render('shops/form', title => ($is_new ? t8('Add shop') : t8('Edit shop')));
42 }
43
44 sub action_save {
45   my ($self) = @_;
46
47   $self->create_or_update;
48 }
49
50 sub action_delete {
51   my ($self) = @_;
52
53   if ( eval { $self->shop->delete; 1; } ) {
54     flash_later('info',  $::locale->text('The shop has been deleted.'));
55   } else {
56     flash_later('error', $::locale->text('The shop is in use and cannot be deleted.'));
57   };
58   $self->redirect_to(action => 'list');
59 }
60
61 sub action_reorder {
62   my ($self) = @_;
63
64   SL::DB::Shop->reorder_list(@{ $::form->{shop_id} || [] });
65   $self->render(\'', { type => 'json' }); # ' emacs happy again
66 }
67
68 sub action_check_connectivity {
69   my ($self) = @_;
70
71   my $ok = 0;
72   require SL::Shop;
73   my $shop = SL::Shop->new( config => $self->shop );
74   my $connect = $shop->check_connectivity;
75   $ok       = $connect->{success};
76   my  $version = $connect->{data}->{version};
77   $self->render('shops/test_shop_connection', { layout => 0 },
78                 title   => t8('Shop Connection Test'),
79                 ok      => $ok,
80                 version => $version);
81 }
82
83 sub check_auth {
84   $::auth->assert('config');
85 }
86
87 sub init_shop {
88   SL::DB::Manager::Shop->find_by_or_create(id => $::form->{id} || 0)->assign_attributes(%{ $::form->{shop} });
89 }
90
91 #
92 # helpers
93 #
94
95 sub create_or_update {
96   my ($self) = @_;
97
98   my $is_new = !$self->shop->id;
99
100   my @errors = $self->shop->validate;
101   if (@errors) {
102     flash('error', @errors);
103     $self->load_types();
104     $self->action_edit();
105     return;
106   }
107
108   $self->shop->save;
109
110   flash_later('info', $is_new ? t8('The shop has been created.') : t8('The shop has been saved.'));
111   $self->redirect_to(action => 'list');
112 }
113
114 sub load_types {
115   my ($self) = @_;
116   # data for the dropdowns when editing Shop configs
117
118   require SL::ShopConnector::ALL;
119   $self->connectors(SL::ShopConnector::ALL->connectors);
120
121   $self->price_types( [ { id => "brutto", name => t8('brutto') },
122                         { id => "netto",  name => t8('netto')  } ] );
123
124   $self->protocols(   [ { id => "http",  name => t8('http') },
125                         { id => "https", name => t8('https') } ] );
126
127   my $pricesources;
128   push(@{ $pricesources } , { id => "master_data/sellprice", name => t8("Master Data") . " - " . t8("Sellprice") },
129                             { id => "master_data/listprice", name => t8("Master Data") . " - " . t8("Listprice") },
130                             { id => "master_data/lastcost",  name => t8("Master Data") . " - " . t8("Lastcost")  });
131   my $pricegroups = SL::DB::Manager::Pricegroup->get_all;
132   foreach my $pg ( @$pricegroups ) {
133     push( @{ $pricesources } , { id => "pricegroup/" . $pg->id, name => t8("Pricegroup") . " - " . $pg->pricegroup} );
134   };
135
136   $self->price_sources($pricesources);
137
138   #Buchungsgruppen for calculate the tax for an article
139   my $taxkey_ids;
140   my $taxzones = SL::DB::Manager::TaxZone->get_all_sorted();
141
142   foreach my $tz (@$taxzones) {
143     push  @{ $taxkey_ids }, { id => $tz->id, name => $tz->description };
144   }
145   $self->taxzone_id( $taxkey_ids );
146 };
147
148 sub _setup_form_action_bar {
149   my ($self) = @_;
150
151   for my $bar ($::request->layout->get('actionbar')) {
152     $bar->add(
153       combobox => [
154         action => [
155           t8('Save'),
156           submit    => [ '#form', { action => "Shop/save" } ],
157           accesskey => 'enter',
158         ],
159          action => [
160           t8('Delete'),
161           submit => [ '#form', { action => "Shop/delete" } ],
162         ],
163       ],
164       action => [
165         t8('Check Api'),
166         call => [ 'kivi.Shop.check_connectivity', id => "form" ],
167         tooltip => t8('Check connectivity'),
168       ],
169       action => [
170         t8('Cancel'),
171         submit => [ '#form', { action => "Shop/list" } ],
172       ],
173     );
174   }
175 }
176
177 sub _setup_list_action_bar {
178   my ($self) = @_;
179
180   for my $bar ($::request->layout->get('actionbar')) {
181     $bar->add(
182       link => [
183         t8('Add'),
184         link => $self->url_for(action => 'edit'),
185       ],
186     )
187   };
188 }
189
190 1;
191
192 __END__
193
194 =encoding utf-8
195
196 =head1 NAME
197
198   SL::Controller::Shop
199
200 =head1 SYNOPSIS
201
202
203 =head1 DESCRIPTION
204
205
206 =head1 BUGS
207
208 None yet. :)
209
210 =head1 AUTHOR
211
212 G. Richardson E<lt>information@kivitendo-premium.deE<gt>
213 W. Hahn E<lt>wh@futureworldsearch.netE<gt>