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;
17 use SL::Locale::String;
19 use Rose::Object::MakeMethods::Generic
21 'scalar --get_set_init' => [ qw(models price_rule vc js pricegroups partsgroups businesses) ],
24 # __PACKAGE__->run_before('check_auth');
25 __PACKAGE__->run_before('add_javascripts');
34 $self->make_filter_summary;
36 my $price_rules = $self->models->get;
38 $self->prepare_report;
40 $self->report_generator_list_objects(report => $self->{report}, objects => $price_rules, $::form->{inline} ? (layout => 0, header => 0) : ());
46 $self->price_rule(SL::DB::PriceRule->new);
47 $self->price_rule->assign_attributes(%{ $::form->{price_rule} || {} });
60 $self->price_rule($self->price_rule->clone_and_reset_deep);
61 $self->create_or_update;
66 $self->create_or_update;
73 $self->price_rule->delete;
74 flash_later('info', $::locale->text('The price rule has been deleted.'));
76 $self->redirect_to($::form->{callback} || (action => 'list'));
79 sub action_add_item_row {
80 my ($self, %params) = @_;
82 my $item = SL::DB::PriceRuleItem->new(type => $::form->{type});
84 my $html = $self->render('price_rule/item', { output => 0 }, item => $item);
88 ->before('#price_rule_new_items', $html)
93 sub action_price_type_help {
94 $_[0]->render('price_rule/price_type_help', { layout => 0 });
102 $::auth->assert('price_rule_edit');
110 my ($self, %params) = @_;
111 my $is_new = !$self->price_rule->id;
112 my $title = $self->form_title(($is_new ? 'create' : 'edit'), $self->price_rule->type);
113 $self->render('price_rule/form',
120 my ($self, $action, $type) = @_;
124 customer => t8('Edit sales price rule'),
125 vendor => t8('Edit purchase price rule'),
126 '' => t8('Edit price rule'),
129 customer => t8('Create a new sales price rule'),
130 vendor => t8('Create a new purchase price rule'),
131 '' => t8('Create a new price rule'),
134 customer => t8('Sales Price Rules'),
135 vendor => t8('Purchase Price Rules'),
136 '' => t8('Price Rules'),
141 sub create_or_update {
143 my $is_new = !$self->price_rule->id;
144 my $params = delete($::form->{price_rule}) || { };
146 delete $params->{id};
147 $self->price_rule->assign_attributes(%{ $params });
149 my @errors = $self->price_rule->validate;
152 flash('error', @errors);
153 $self->display_form(callback => $::form->{callback});
157 $self->price_rule->save;
159 flash_later('info', $is_new ? $::locale->text('The price rule has been created.') : $::locale->text('The price rule has been saved.'));
161 $self->redirect_to($::form->{callback} || (action => 'list', 'filter.type' => $self->price_rule->type));
167 my $callback = $self->models->get_callback;
169 my $report = SL::ReportGenerator->new(\%::myconfig, $::form);
170 $self->{report} = $report;
172 my @columns = qw(name type priority price reduction discount items);
173 my @sortable = qw(name type priority price reduction discount );
176 name => { obj_link => sub { $self->url_for(action => 'edit', 'price_rule.id' => $_[0]->id, callback => $callback) } },
177 priority => { sub => sub { $_[0]->priority_as_text } },
178 price => { sub => sub { $_[0]->price_as_number } },
179 reduction => { sub => sub { $_[0]->reduction_as_number } },
180 discount => { sub => sub { $_[0]->discount_as_number } },
181 obsolete => { sub => sub { $_[0]->obsolete_as_bool_yn } },
182 items => { sub => sub { $_[0]->item_summary } },
185 map { $column_defs{$_}->{text} ||= $::locale->text( $self->models->get_sort_spec->{$_}->{title} ) } keys %column_defs;
187 if ( $report->{options}{output_format} =~ /^(pdf|csv)$/i ) {
188 $self->models->disable_plugin('paginated');
190 $report->set_options(
191 std_column_visibility => 1,
192 controller_class => 'PriceRule',
193 output_format => 'HTML',
194 title => $self->form_title('list', $self->vc),
195 allow_pdf_export => !$::form->{inline},
196 allow_csv_export => !$::form->{inline},
198 $report->set_columns(%column_defs);
199 $report->set_column_order(@columns);
200 $report->set_export_options(qw(list filter));
201 $report->set_options_from_form;
202 $self->models->get_models_url_params(sub{ map { $_ => $::form->{$_} } qw(inline) });
203 $self->models->set_report_generator_sort_options(report => $report, sortable_columns => \@sortable);
204 $report->set_options(
205 raw_bottom_info_text => $self->render('price_rule/report_bottom', { output => 0 }),
206 raw_top_info_text => $self->render('price_rule/report_top', { output => 0 }),
210 sub make_filter_summary {
213 my $filter = $::form->{filter} || {};
217 [ $filter->{"name:substr::ilike"}, t8('Name') ],
218 [ $filter->{"price:number"}, t8('Price') ],
219 [ $filter->{"discount:number"}, t8('Discount') ],
223 push @filter_strings, "$_->[1]: $_->[0]" if $_->[0];
226 if ($filter->{has_item_type}) {
227 push @filter_strings, sprintf "%s: %s", t8('Has item type'), join ', ', map {
228 SL::DB::Manager::PriceRuleItem->get_type($_)->{description}
229 } @{ $filter->{has_item_type} || [] };
232 $self->{filter_summary} = join ', ', @filter_strings;
235 sub all_price_rule_item_types {
236 SL::DB::Manager::PriceRuleItem->get_all_types($_[0]->vc || $_[0]->price_rule->type);
239 sub add_javascripts {
240 $::request->{layout}->add_javascripts(qw(kivi.PriceRule.js autocomplete_customer.js autocomplete_vendor.js autocomplete_part.js));
243 sub init_price_rule {
246 my $price_rule = $::form->{price_rule}{id} ? SL::DB::PriceRule->new(id => $::form->{price_rule}{id})->load : SL::DB::PriceRule->new;
248 my $items = delete $::form->{price_rule}{items};
250 $price_rule->assign_attributes(%{ $::form->{price_rule} || {} });
252 my %old_items = map { $_->id => $_ } $price_rule->items;
255 for my $raw_item (@$items) {
256 my $item = $raw_item->{id} ? $old_items{ $raw_item->{id} } || SL::DB::PriceRuleItem->new(id => $raw_item->{id})->load : SL::DB::PriceRuleItem->new;
257 $item->assign_attributes(%$raw_item);
261 $price_rule->items(@items) if @items;
263 $self->price_rule($price_rule);
267 $::form->{filter}{type};
274 sub init_businesses {
275 SL::DB::Manager::Business->get_all;
278 sub init_pricegroups {
279 SL::DB::Manager::Pricegroup->get_all;
282 sub init_partsgroups {
283 SL::DB::Manager::PartsGroup->get_all;
286 sub all_price_types {
287 SL::DB::Manager::PriceRule->all_price_types;
293 SL::Controller::Helper::GetModels->new(
298 priority => t8('Priority'),
299 price => t8('Price'),
300 discount => t8('Discount'),
301 reduction => t8('Reduced Master Data'),
302 obsolete => t8('Obsolete'),
303 items => t8('Rule Details'),