kivitendo/SL/Controller/PriceRule.pm @ 7e7aae8d
9589ecd7 | Sven Schöling | package SL::Controller::PriceRule;
|
||
use strict;
|
||||
use parent qw(SL::Controller::Base);
|
||||
use SL::Controller::Helper::GetModels;
|
||||
use SL::Controller::Helper::ParseFilter;
|
||||
use SL::Controller::Helper::ReportGenerator;
|
||||
use SL::DB::PriceRule;
|
||||
use SL::DB::PriceRuleItem;
|
||||
use SL::DB::Pricegroup;
|
||||
use SL::DB::PartsGroup;
|
||||
use SL::DB::Business;
|
||||
use SL::Helper::Flash;
|
||||
use SL::Locale::String;
|
||||
use Rose::Object::MakeMethods::Generic
|
||||
(
|
||||
2d678531 | Sven Schöling | 'scalar --get_set_init' => [ qw(models price_rule vc pricegroups partsgroups businesses) ],
|
||
9589ecd7 | Sven Schöling | );
|
||
# __PACKAGE__->run_before('check_auth');
|
||||
__PACKAGE__->run_before('add_javascripts');
|
||||
#
|
||||
# actions
|
||||
#
|
||||
sub action_list {
|
||||
my ($self) = @_;
|
||||
$self->make_filter_summary;
|
||||
my $price_rules = $self->models->get;
|
||||
b52b09d8 | Moritz Bunkus | $self->setup_search_action_bar;
|
||
9589ecd7 | Sven Schöling | $self->prepare_report;
|
||
e7913c4c | Moritz Bunkus | $self->report_generator_list_objects(report => $self->{report}, objects => $price_rules, $::form->{inline} ? (layout => 0, header => 0) : ());
|
||
9589ecd7 | Sven Schöling | }
|
||
sub action_new {
|
||||
my ($self) = @_;
|
||||
$self->price_rule(SL::DB::PriceRule->new);
|
||||
$self->price_rule->assign_attributes(%{ $::form->{price_rule} || {} });
|
||||
$self->display_form;
|
||||
}
|
||||
sub action_edit {
|
||||
my ($self) = @_;
|
||||
$self->display_form;
|
||||
}
|
||||
sub action_create {
|
||||
my ($self) = @_;
|
||||
$self->price_rule($self->price_rule->clone_and_reset_deep);
|
||||
$self->create_or_update;
|
||||
}
|
||||
sub action_update {
|
||||
my ($self) = @_;
|
||||
$self->create_or_update;
|
||||
}
|
||||
#TODO
|
||||
sub action_destroy {
|
||||
my ($self) = @_;
|
||||
de53e9a8 | Sven Schöling | $self->price_rule->delete;
|
||
flash_later('info', $::locale->text('The price rule has been deleted.'));
|
||||
9589ecd7 | Sven Schöling | |||
de53e9a8 | Sven Schöling | $self->redirect_to($::form->{callback} || (action => 'list'));
|
||
9589ecd7 | Sven Schöling | }
|
||
sub action_add_item_row {
|
||||
my ($self, %params) = @_;
|
||||
my $item = SL::DB::PriceRuleItem->new(type => $::form->{type});
|
||||
my $html = $self->render('price_rule/item', { output => 0 }, item => $item);
|
||||
$self
|
||||
->js
|
||||
->before('#price_rule_new_items', $html)
|
||||
->reinit_widgets
|
||||
2d678531 | Sven Schöling | ->render;
|
||
9589ecd7 | Sven Schöling | }
|
||
bc8c26f3 | Sven Schöling | sub action_price_type_help {
|
||
$_[0]->render('price_rule/price_type_help', { layout => 0 });
|
||||
}
|
||||
9589ecd7 | Sven Schöling | #
|
||
# filters
|
||||
#
|
||||
sub check_auth {
|
||||
$::auth->assert('price_rule_edit');
|
||||
}
|
||||
#
|
||||
# helpers
|
||||
#
|
||||
sub display_form {
|
||||
my ($self, %params) = @_;
|
||||
my $is_new = !$self->price_rule->id;
|
||||
c383fc0b | Sven Schöling | my $title = $self->form_title(($is_new ? 'create' : 'edit'), $self->price_rule->type);
|
||
b52b09d8 | Moritz Bunkus | $self->setup_form_action_bar;
|
||
9589ecd7 | Sven Schöling | $self->render('price_rule/form',
|
||
title => $title,
|
||||
%params
|
||||
);
|
||||
}
|
||||
c383fc0b | Sven Schöling | sub form_title {
|
||
my ($self, $action, $type) = @_;
|
||||
return {
|
||||
edit => {
|
||||
customer => t8('Edit sales price rule'),
|
||||
vendor => t8('Edit purchase price rule'),
|
||||
'' => t8('Edit price rule'),
|
||||
},
|
||||
create => {
|
||||
customer => t8('Create a new sales price rule'),
|
||||
vendor => t8('Create a new purchase price rule'),
|
||||
'' => t8('Create a new price rule'),
|
||||
},
|
||||
list => {
|
||||
customer => t8('Sales Price Rules'),
|
||||
vendor => t8('Purchase Price Rules'),
|
||||
'' => t8('Price Rules'),
|
||||
},
|
||||
}->{$action}{$type};
|
||||
}
|
||||
9589ecd7 | Sven Schöling | sub create_or_update {
|
||
my $self = shift;
|
||||
my $is_new = !$self->price_rule->id;
|
||||
my $params = delete($::form->{price_rule}) || { };
|
||||
delete $params->{id};
|
||||
$self->price_rule->assign_attributes(%{ $params });
|
||||
my @errors = $self->price_rule->validate;
|
||||
if (@errors) {
|
||||
flash('error', @errors);
|
||||
$self->display_form(callback => $::form->{callback});
|
||||
return;
|
||||
}
|
||||
$self->price_rule->save;
|
||||
flash_later('info', $is_new ? $::locale->text('The price rule has been created.') : $::locale->text('The price rule has been saved.'));
|
||||
$self->redirect_to($::form->{callback} || (action => 'list', 'filter.type' => $self->price_rule->type));
|
||||
}
|
||||
sub prepare_report {
|
||||
my ($self) = @_;
|
||||
my $callback = $self->models->get_callback;
|
||||
my $report = SL::ReportGenerator->new(\%::myconfig, $::form);
|
||||
$self->{report} = $report;
|
||||
bc8c26f3 | Sven Schöling | my @columns = qw(name type priority price reduction discount items);
|
||
my @sortable = qw(name type priority price reduction discount );
|
||||
9589ecd7 | Sven Schöling | |||
my %column_defs = (
|
||||
name => { obj_link => sub { $self->url_for(action => 'edit', 'price_rule.id' => $_[0]->id, callback => $callback) } },
|
||||
72180323 | Sven Schöling | priority => { sub => sub { $_[0]->priority_as_text } },
|
||
9589ecd7 | Sven Schöling | price => { sub => sub { $_[0]->price_as_number } },
|
||
bc8c26f3 | Sven Schöling | reduction => { sub => sub { $_[0]->reduction_as_number } },
|
||
9589ecd7 | Sven Schöling | discount => { sub => sub { $_[0]->discount_as_number } },
|
||
obsolete => { sub => sub { $_[0]->obsolete_as_bool_yn } },
|
||||
5aa485a7 | Sven Schöling | items => { sub => sub { $_[0]->item_summary } },
|
||
9589ecd7 | Sven Schöling | );
|
||
map { $column_defs{$_}->{text} ||= $::locale->text( $self->models->get_sort_spec->{$_}->{title} ) } keys %column_defs;
|
||||
if ( $report->{options}{output_format} =~ /^(pdf|csv)$/i ) {
|
||||
$self->models->disable_plugin('paginated');
|
||||
}
|
||||
$report->set_options(
|
||||
std_column_visibility => 1,
|
||||
controller_class => 'PriceRule',
|
||||
output_format => 'HTML',
|
||||
c383fc0b | Sven Schöling | title => $self->form_title('list', $self->vc),
|
||
678f57a3 | Sven Schöling | allow_pdf_export => !$::form->{inline},
|
||
allow_csv_export => !$::form->{inline},
|
||||
9589ecd7 | Sven Schöling | );
|
||
$report->set_columns(%column_defs);
|
||||
$report->set_column_order(@columns);
|
||||
$report->set_export_options(qw(list filter));
|
||||
$report->set_options_from_form;
|
||||
678f57a3 | Sven Schöling | $self->models->get_models_url_params(sub{ map { $_ => $::form->{$_} } qw(inline) });
|
||
9589ecd7 | Sven Schöling | $self->models->set_report_generator_sort_options(report => $report, sortable_columns => \@sortable);
|
||
$report->set_options(
|
||||
raw_bottom_info_text => $self->render('price_rule/report_bottom', { output => 0 }),
|
||||
raw_top_info_text => $self->render('price_rule/report_top', { output => 0 }),
|
||||
);
|
||||
}
|
||||
sub make_filter_summary {
|
||||
my ($self) = @_;
|
||||
my $filter = $::form->{filter} || {};
|
||||
my @filter_strings;
|
||||
my @filters = (
|
||||
[ $filter->{"name:substr::ilike"}, t8('Name') ],
|
||||
[ $filter->{"price:number"}, t8('Price') ],
|
||||
[ $filter->{"discount:number"}, t8('Discount') ],
|
||||
);
|
||||
for (@filters) {
|
||||
push @filter_strings, "$_->[1]: $_->[0]" if $_->[0];
|
||||
}
|
||||
986282c1 | Sven Schöling | if ($filter->{has_item_type}) {
|
||
push @filter_strings, sprintf "%s: %s", t8('Has item type'), join ', ', map {
|
||||
SL::DB::Manager::PriceRuleItem->get_type($_)->{description}
|
||||
} @{ $filter->{has_item_type} || [] };
|
||||
}
|
||||
9589ecd7 | Sven Schöling | $self->{filter_summary} = join ', ', @filter_strings;
|
||
}
|
||||
sub all_price_rule_item_types {
|
||||
986282c1 | Sven Schöling | SL::DB::Manager::PriceRuleItem->get_all_types($_[0]->vc || $_[0]->price_rule->type);
|
||
9589ecd7 | Sven Schöling | }
|
||
sub add_javascripts {
|
||||
5aec18fe | Sven Schöling | $::request->{layout}->add_javascripts(qw(kivi.PriceRule.js autocomplete_customer.js autocomplete_vendor.js kivi.Part.js));
|
||
9589ecd7 | Sven Schöling | }
|
||
sub init_price_rule {
|
||||
my ($self) = @_;
|
||||
my $price_rule = $::form->{price_rule}{id} ? SL::DB::PriceRule->new(id => $::form->{price_rule}{id})->load : SL::DB::PriceRule->new;
|
||||
my $items = delete $::form->{price_rule}{items};
|
||||
$price_rule->assign_attributes(%{ $::form->{price_rule} || {} });
|
||||
my %old_items = map { $_->id => $_ } $price_rule->items;
|
||||
my @items;
|
||||
for my $raw_item (@$items) {
|
||||
my $item = $raw_item->{id} ? $old_items{ $raw_item->{id} } || SL::DB::PriceRuleItem->new(id => $raw_item->{id})->load : SL::DB::PriceRuleItem->new;
|
||||
$item->assign_attributes(%$raw_item);
|
||||
push @items, $item;
|
||||
}
|
||||
$price_rule->items(@items) if @items;
|
||||
$self->price_rule($price_rule);
|
||||
}
|
||||
sub init_vc {
|
||||
$::form->{filter}{type};
|
||||
}
|
||||
sub init_businesses {
|
||||
SL::DB::Manager::Business->get_all;
|
||||
}
|
||||
sub init_pricegroups {
|
||||
e48eb4dc | Geoffrey Richardson | SL::DB::Manager::Pricegroup->get_all_sorted;
|
||
9589ecd7 | Sven Schöling | }
|
||
sub init_partsgroups {
|
||||
SL::DB::Manager::PartsGroup->get_all;
|
||||
}
|
||||
bc8c26f3 | Sven Schöling | sub all_price_types {
|
||
SL::DB::Manager::PriceRule->all_price_types;
|
||||
}
|
||||
9589ecd7 | Sven Schöling | sub init_models {
|
||
my ($self) = @_;
|
||||
SL::Controller::Helper::GetModels->new(
|
||||
controller => $self,
|
||||
sorted => {
|
||||
name => t8('Name'),
|
||||
type => t8('Type'),
|
||||
priority => t8('Priority'),
|
||||
price => t8('Price'),
|
||||
discount => t8('Discount'),
|
||||
bc8c26f3 | Sven Schöling | reduction => t8('Reduced Master Data'),
|
||
9589ecd7 | Sven Schöling | obsolete => t8('Obsolete'),
|
||
5aa485a7 | Sven Schöling | items => t8('Rule Details'),
|
||
9589ecd7 | Sven Schöling | },
|
||
);
|
||||
}
|
||||
b52b09d8 | Moritz Bunkus | sub setup_search_action_bar {
|
||
my ($self, %params) = @_;
|
||||
return if $::form->{inline};
|
||||
for my $bar ($::request->layout->get('actionbar')) {
|
||||
$bar->add(
|
||||
action => [
|
||||
8cba44f6 | Moritz Bunkus | t8('Update'),
|
||
b52b09d8 | Moritz Bunkus | submit => [ '#search_form', { action => 'PriceRule/list' } ],
|
||
accesskey => 'enter',
|
||||
],
|
||||
combobox => [
|
||||
action => [
|
||||
t8('Add'),
|
||||
],
|
||||
link => [
|
||||
t8('New Sales Price Rule'),
|
||||
link => $self->url_for(action => 'new', 'price_rule.type' => 'customer', callback => $self->models->get_callback),
|
||||
],
|
||||
link => [
|
||||
t8('New Purchase Price Rule'),
|
||||
link => $self->url_for(action => 'new', 'price_rule.type' => 'vendor', callback => $self->models->get_callback),
|
||||
],
|
||||
], # end of combobox "Add"
|
||||
);
|
||||
}
|
||||
}
|
||||
sub setup_form_action_bar {
|
||||
my ($self) = @_;
|
||||
my $is_new = !$self->price_rule->id;
|
||||
for my $bar ($::request->layout->get('actionbar')) {
|
||||
$bar->add(
|
||||
combobox => [
|
||||
action => [
|
||||
$is_new ? t8('Create') : t8('Save'),
|
||||
submit => [ '#form', { action => 'PriceRule/' . ($is_new ? 'create' : 'update') } ],
|
||||
accesskey => 'enter',
|
||||
],
|
||||
action => [
|
||||
t8('Use as new'),
|
||||
submit => [ '#form', { action => 'PriceRule/create' } ],
|
||||
disabled => $is_new ? t8('The object has not been saved yet.') : undef,
|
||||
],
|
||||
], # end of combobox "Save"
|
||||
action => [
|
||||
t8('Delete'),
|
||||
submit => [ '#form', { action => 'PriceRule/destroy' } ],
|
||||
confirm => t8('Do you really want to delete this object?'),
|
||||
disabled => $is_new ? t8('The object has not been saved yet.')
|
||||
: $self->price_rule->in_use ? t8('This object has already been used.')
|
||||
: undef,
|
||||
],
|
||||
link => [
|
||||
t8('Abort'),
|
||||
link => $self->url_for(action => 'list', 'filter.type' => $self->price_rule->type),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
9589ecd7 | Sven Schöling | 1;
|