1 package SL::Controller::PriceRule;
5 use parent qw(SL::Controller::Base);
7 use SL::Controller::Helper::GetModels;
8 use SL::Controller::Helper::ParseFilter;
9 use SL::Controller::Helper::ReportGenerator;
10 use SL::DB::PriceRule;
11 use SL::DB::PriceRuleItem;
12 use SL::DB::Pricegroup;
13 use SL::DB::PartsGroup;
15 use SL::Helper::Flash;
16 use SL::Locale::String;
18 use Rose::Object::MakeMethods::Generic
20 'scalar --get_set_init' => [ qw(models price_rule vc pricegroups partsgroups businesses) ],
23 # __PACKAGE__->run_before('check_auth');
24 __PACKAGE__->run_before('add_javascripts');
33 $self->make_filter_summary;
35 my $price_rules = $self->models->get;
37 $self->setup_search_action_bar;
39 $self->prepare_report;
41 $self->report_generator_list_objects(report => $self->{report}, objects => $price_rules, $::form->{inline} ? (layout => 0, header => 0) : ());
47 $self->price_rule(SL::DB::PriceRule->new);
48 $self->price_rule->assign_attributes(%{ $::form->{price_rule} || {} });
61 $self->price_rule($self->price_rule->clone_and_reset_deep);
62 $self->create_or_update;
67 $self->create_or_update;
74 $self->price_rule->delete;
75 flash_later('info', $::locale->text('The price rule has been deleted.'));
77 $self->redirect_to($::form->{callback} || (action => 'list'));
80 sub action_add_item_row {
81 my ($self, %params) = @_;
83 my $item = SL::DB::PriceRuleItem->new(type => $::form->{type});
85 my $html = $self->render('price_rule/item', { output => 0 }, item => $item);
89 ->before('#price_rule_new_items', $html)
94 sub action_price_type_help {
95 $_[0]->render('price_rule/price_type_help', { layout => 0 });
103 $::auth->assert('price_rule_edit');
111 my ($self, %params) = @_;
112 my $is_new = !$self->price_rule->id;
113 my $title = $self->form_title(($is_new ? 'create' : 'edit'), $self->price_rule->type);
114 $self->setup_form_action_bar;
115 $self->render('price_rule/form',
122 my ($self, $action, $type) = @_;
126 customer => t8('Edit sales price rule'),
127 vendor => t8('Edit purchase price rule'),
128 '' => t8('Edit price rule'),
131 customer => t8('Create a new sales price rule'),
132 vendor => t8('Create a new purchase price rule'),
133 '' => t8('Create a new price rule'),
136 customer => t8('Sales Price Rules'),
137 vendor => t8('Purchase Price Rules'),
138 '' => t8('Price Rules'),
143 sub create_or_update {
145 my $is_new = !$self->price_rule->id;
146 my $params = delete($::form->{price_rule}) || { };
148 delete $params->{id};
149 $self->price_rule->assign_attributes(%{ $params });
151 my @errors = $self->price_rule->validate;
154 flash('error', @errors);
155 $self->display_form(callback => $::form->{callback});
159 $self->price_rule->save;
161 flash_later('info', $is_new ? $::locale->text('The price rule has been created.') : $::locale->text('The price rule has been saved.'));
163 $self->redirect_to($::form->{callback} || (action => 'list', 'filter.type' => $self->price_rule->type));
169 my $callback = $self->models->get_callback;
171 my $report = SL::ReportGenerator->new(\%::myconfig, $::form);
172 $self->{report} = $report;
174 my @columns = qw(name type priority price reduction discount items);
175 my @sortable = qw(name type priority price reduction discount );
178 name => { obj_link => sub { $self->url_for(action => 'edit', 'price_rule.id' => $_[0]->id, callback => $callback) } },
179 priority => { sub => sub { $_[0]->priority_as_text } },
180 price => { sub => sub { $_[0]->price_as_number } },
181 reduction => { sub => sub { $_[0]->reduction_as_number } },
182 discount => { sub => sub { $_[0]->discount_as_number } },
183 obsolete => { sub => sub { $_[0]->obsolete_as_bool_yn } },
184 items => { sub => sub { $_[0]->item_summary } },
187 map { $column_defs{$_}->{text} ||= $::locale->text( $self->models->get_sort_spec->{$_}->{title} ) } keys %column_defs;
189 if ( $report->{options}{output_format} =~ /^(pdf|csv)$/i ) {
190 $self->models->disable_plugin('paginated');
192 $report->set_options(
193 std_column_visibility => 1,
194 controller_class => 'PriceRule',
195 output_format => 'HTML',
196 title => $self->form_title('list', $self->vc),
197 allow_pdf_export => !$::form->{inline},
198 allow_csv_export => !$::form->{inline},
200 $report->set_columns(%column_defs);
201 $report->set_column_order(@columns);
202 $report->set_export_options(qw(list filter));
203 $report->set_options_from_form;
204 $self->models->get_models_url_params(sub{ map { $_ => $::form->{$_} } qw(inline) });
205 $self->models->set_report_generator_sort_options(report => $report, sortable_columns => \@sortable);
206 $report->set_options(
207 raw_bottom_info_text => $self->render('price_rule/report_bottom', { output => 0 }),
208 raw_top_info_text => $self->render('price_rule/report_top', { output => 0 }),
212 sub make_filter_summary {
215 my $filter = $::form->{filter} || {};
219 [ $filter->{"name:substr::ilike"}, t8('Name') ],
220 [ $filter->{"price:number"}, t8('Price') ],
221 [ $filter->{"discount:number"}, t8('Discount') ],
225 push @filter_strings, "$_->[1]: $_->[0]" if $_->[0];
228 if ($filter->{has_item_type}) {
229 push @filter_strings, sprintf "%s: %s", t8('Has item type'), join ', ', map {
230 SL::DB::Manager::PriceRuleItem->get_type($_)->{description}
231 } @{ $filter->{has_item_type} || [] };
234 $self->{filter_summary} = join ', ', @filter_strings;
237 sub all_price_rule_item_types {
238 SL::DB::Manager::PriceRuleItem->get_all_types($_[0]->vc || $_[0]->price_rule->type);
241 sub add_javascripts {
242 $::request->{layout}->add_javascripts(qw(kivi.PriceRule.js autocomplete_vendor.js kivi.Part.js kivi.CustomerVendor.js));
245 sub init_price_rule {
248 my $price_rule = $::form->{price_rule}{id} ? SL::DB::PriceRule->new(id => $::form->{price_rule}{id})->load : SL::DB::PriceRule->new;
250 my $items = delete $::form->{price_rule}{items};
252 $price_rule->assign_attributes(%{ $::form->{price_rule} || {} });
254 my %old_items = map { $_->id => $_ } $price_rule->items;
257 for my $raw_item (@$items) {
258 my $item = $raw_item->{id} ? $old_items{ $raw_item->{id} } || SL::DB::PriceRuleItem->new(id => $raw_item->{id})->load : SL::DB::PriceRuleItem->new;
259 $item->assign_attributes(%$raw_item);
263 $price_rule->items(@items) if @items;
265 $self->price_rule($price_rule);
269 $::form->{filter}{type};
272 sub init_businesses {
273 SL::DB::Manager::Business->get_all;
276 sub init_pricegroups {
277 SL::DB::Manager::Pricegroup->get_all_sorted;
280 sub init_partsgroups {
281 SL::DB::Manager::PartsGroup->get_all;
284 sub all_price_types {
285 SL::DB::Manager::PriceRule->all_price_types;
291 SL::Controller::Helper::GetModels->new(
296 priority => t8('Priority'),
297 price => t8('Price'),
298 discount => t8('Discount'),
299 reduction => t8('Reduced Master Data'),
300 obsolete => t8('Obsolete'),
301 items => t8('Rule Details'),
306 sub setup_search_action_bar {
307 my ($self, %params) = @_;
309 return if $::form->{inline};
311 for my $bar ($::request->layout->get('actionbar')) {
315 submit => [ '#search_form', { action => 'PriceRule/list' } ],
316 accesskey => 'enter',
324 t8('New Sales Price Rule'),
325 link => $self->url_for(action => 'new', 'price_rule.type' => 'customer', callback => $self->models->get_callback),
328 t8('New Purchase Price Rule'),
329 link => $self->url_for(action => 'new', 'price_rule.type' => 'vendor', callback => $self->models->get_callback),
331 ], # end of combobox "Add"
336 sub setup_form_action_bar {
339 my $is_new = !$self->price_rule->id;
341 for my $bar ($::request->layout->get('actionbar')) {
345 $is_new ? t8('Create') : t8('Save'),
346 submit => [ '#form', { action => 'PriceRule/' . ($is_new ? 'create' : 'update') } ],
347 accesskey => 'enter',
351 submit => [ '#form', { action => 'PriceRule/create' } ],
352 disabled => $is_new ? t8('The object has not been saved yet.') : undef,
354 ], # end of combobox "Save"
358 submit => [ '#form', { action => 'PriceRule/destroy' } ],
359 confirm => t8('Do you really want to delete this object?'),
360 disabled => $is_new ? t8('The object has not been saved yet.')
361 : $self->price_rule->in_use ? t8('This object has already been used.')
367 link => $self->url_for(action => 'list', 'filter.type' => $self->price_rule->type),