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->prepare_report;
39 $self->report_generator_list_objects(report => $self->{report}, objects => $price_rules, $::form->{inline} ? (layout => 0, header => 0) : ());
45 $self->price_rule(SL::DB::PriceRule->new);
46 $self->price_rule->assign_attributes(%{ $::form->{price_rule} || {} });
59 $self->price_rule($self->price_rule->clone_and_reset_deep);
60 $self->create_or_update;
65 $self->create_or_update;
72 $self->price_rule->delete;
73 flash_later('info', $::locale->text('The price rule has been deleted.'));
75 $self->redirect_to($::form->{callback} || (action => 'list'));
78 sub action_add_item_row {
79 my ($self, %params) = @_;
81 my $item = SL::DB::PriceRuleItem->new(type => $::form->{type});
83 my $html = $self->render('price_rule/item', { output => 0 }, item => $item);
87 ->before('#price_rule_new_items', $html)
92 sub action_price_type_help {
93 $_[0]->render('price_rule/price_type_help', { layout => 0 });
101 $::auth->assert('price_rule_edit');
109 my ($self, %params) = @_;
110 my $is_new = !$self->price_rule->id;
111 my $title = $self->form_title(($is_new ? 'create' : 'edit'), $self->price_rule->type);
112 $self->render('price_rule/form',
119 my ($self, $action, $type) = @_;
123 customer => t8('Edit sales price rule'),
124 vendor => t8('Edit purchase price rule'),
125 '' => t8('Edit price rule'),
128 customer => t8('Create a new sales price rule'),
129 vendor => t8('Create a new purchase price rule'),
130 '' => t8('Create a new price rule'),
133 customer => t8('Sales Price Rules'),
134 vendor => t8('Purchase Price Rules'),
135 '' => t8('Price Rules'),
140 sub create_or_update {
142 my $is_new = !$self->price_rule->id;
143 my $params = delete($::form->{price_rule}) || { };
145 delete $params->{id};
146 $self->price_rule->assign_attributes(%{ $params });
148 my @errors = $self->price_rule->validate;
151 flash('error', @errors);
152 $self->display_form(callback => $::form->{callback});
156 $self->price_rule->save;
158 flash_later('info', $is_new ? $::locale->text('The price rule has been created.') : $::locale->text('The price rule has been saved.'));
160 $self->redirect_to($::form->{callback} || (action => 'list', 'filter.type' => $self->price_rule->type));
166 my $callback = $self->models->get_callback;
168 my $report = SL::ReportGenerator->new(\%::myconfig, $::form);
169 $self->{report} = $report;
171 my @columns = qw(name type priority price reduction discount items);
172 my @sortable = qw(name type priority price reduction discount );
175 name => { obj_link => sub { $self->url_for(action => 'edit', 'price_rule.id' => $_[0]->id, callback => $callback) } },
176 priority => { sub => sub { $_[0]->priority_as_text } },
177 price => { sub => sub { $_[0]->price_as_number } },
178 reduction => { sub => sub { $_[0]->reduction_as_number } },
179 discount => { sub => sub { $_[0]->discount_as_number } },
180 obsolete => { sub => sub { $_[0]->obsolete_as_bool_yn } },
181 items => { sub => sub { $_[0]->item_summary } },
184 map { $column_defs{$_}->{text} ||= $::locale->text( $self->models->get_sort_spec->{$_}->{title} ) } keys %column_defs;
186 if ( $report->{options}{output_format} =~ /^(pdf|csv)$/i ) {
187 $self->models->disable_plugin('paginated');
189 $report->set_options(
190 std_column_visibility => 1,
191 controller_class => 'PriceRule',
192 output_format => 'HTML',
193 title => $self->form_title('list', $self->vc),
194 allow_pdf_export => !$::form->{inline},
195 allow_csv_export => !$::form->{inline},
197 $report->set_columns(%column_defs);
198 $report->set_column_order(@columns);
199 $report->set_export_options(qw(list filter));
200 $report->set_options_from_form;
201 $self->models->get_models_url_params(sub{ map { $_ => $::form->{$_} } qw(inline) });
202 $self->models->set_report_generator_sort_options(report => $report, sortable_columns => \@sortable);
203 $report->set_options(
204 raw_bottom_info_text => $self->render('price_rule/report_bottom', { output => 0 }),
205 raw_top_info_text => $self->render('price_rule/report_top', { output => 0 }),
209 sub make_filter_summary {
212 my $filter = $::form->{filter} || {};
216 [ $filter->{"name:substr::ilike"}, t8('Name') ],
217 [ $filter->{"price:number"}, t8('Price') ],
218 [ $filter->{"discount:number"}, t8('Discount') ],
222 push @filter_strings, "$_->[1]: $_->[0]" if $_->[0];
225 if ($filter->{has_item_type}) {
226 push @filter_strings, sprintf "%s: %s", t8('Has item type'), join ', ', map {
227 SL::DB::Manager::PriceRuleItem->get_type($_)->{description}
228 } @{ $filter->{has_item_type} || [] };
231 $self->{filter_summary} = join ', ', @filter_strings;
234 sub all_price_rule_item_types {
235 SL::DB::Manager::PriceRuleItem->get_all_types($_[0]->vc || $_[0]->price_rule->type);
238 sub add_javascripts {
239 $::request->{layout}->add_javascripts(qw(kivi.PriceRule.js autocomplete_customer.js autocomplete_vendor.js autocomplete_part.js));
242 sub init_price_rule {
245 my $price_rule = $::form->{price_rule}{id} ? SL::DB::PriceRule->new(id => $::form->{price_rule}{id})->load : SL::DB::PriceRule->new;
247 my $items = delete $::form->{price_rule}{items};
249 $price_rule->assign_attributes(%{ $::form->{price_rule} || {} });
251 my %old_items = map { $_->id => $_ } $price_rule->items;
254 for my $raw_item (@$items) {
255 my $item = $raw_item->{id} ? $old_items{ $raw_item->{id} } || SL::DB::PriceRuleItem->new(id => $raw_item->{id})->load : SL::DB::PriceRuleItem->new;
256 $item->assign_attributes(%$raw_item);
260 $price_rule->items(@items) if @items;
262 $self->price_rule($price_rule);
266 $::form->{filter}{type};
269 sub init_businesses {
270 SL::DB::Manager::Business->get_all;
273 sub init_pricegroups {
274 SL::DB::Manager::Pricegroup->get_all_sorted;
277 sub init_partsgroups {
278 SL::DB::Manager::PartsGroup->get_all;
281 sub all_price_types {
282 SL::DB::Manager::PriceRule->all_price_types;
288 SL::Controller::Helper::GetModels->new(
293 priority => t8('Priority'),
294 price => t8('Price'),
295 discount => t8('Discount'),
296 reduction => t8('Reduced Master Data'),
297 obsolete => t8('Obsolete'),
298 items => t8('Rule Details'),