From: G. Richardson
Date: Thu, 29 Sep 2016 05:19:51 +0000 (+0200)
Subject: Preisgruppen - Umstellung auf Controller, sortkey, obsolete
X-Git-Tag: release-3.5.4~1997
X-Git-Url: http://wagnertech.de/git?a=commitdiff_plain;h=e48eb4dc7e721dbdf15417167e9320fd12decf69;p=kivitendo-erp.git
Preisgruppen - Umstellung auf Controller, sortkey, obsolete
Neuer CRUD-Controller nur für Preisgruppen.
Die Reihenfolge der Preisgruppen kann nun eingestellt werden, und man
kann Preisgruppen auf ungültig setzen, sofern sie nicht mehr aktiv bei
Kunden in Verwendung sind, so daß sie bei Kunden oder neuen Belegen
nicht mehr ausgewählt werden können.
---
diff --git a/SL/Controller/CsvImport/Part.pm b/SL/Controller/CsvImport/Part.pm
index 80fd3cec4..9ccaf83b5 100644
--- a/SL/Controller/CsvImport/Part.pm
+++ b/SL/Controller/CsvImport/Part.pm
@@ -118,7 +118,7 @@ sub init_parts_by {
sub init_all_pricegroups {
my ($self) = @_;
- return SL::DB::Manager::Pricegroup->get_all(sort => 'id');
+ return SL::DB::Manager::Pricegroup->get_all_sorted;
}
sub init_settings {
diff --git a/SL/Controller/CustomerVendor.pm b/SL/Controller/CustomerVendor.pm
index d12fc6edf..2e8b2f5a0 100644
--- a/SL/Controller/CustomerVendor.pm
+++ b/SL/Controller/CustomerVendor.pm
@@ -905,7 +905,7 @@ sub _pre_render {
$self->{all_delivery_terms} = SL::DB::Manager::DeliveryTerm->get_all();
- $self->{all_pricegroups} = SL::DB::Manager::Pricegroup->get_all();
+ $self->{all_pricegroups} = SL::DB::Manager::Pricegroup->get_all_sorted(query => [ or => [ id => $self->{cv}->pricegroup_id, obsolete => 0 ] ]);
$query =
'SELECT DISTINCT(cp_abteilung) AS department
diff --git a/SL/Controller/PriceRule.pm b/SL/Controller/PriceRule.pm
index 1a42a71a2..d9c397847 100644
--- a/SL/Controller/PriceRule.pm
+++ b/SL/Controller/PriceRule.pm
@@ -271,7 +271,7 @@ sub init_businesses {
}
sub init_pricegroups {
- SL::DB::Manager::Pricegroup->get_all;
+ SL::DB::Manager::Pricegroup->get_all_sorted;
}
sub init_partsgroups {
diff --git a/SL/Controller/Pricegroup.pm b/SL/Controller/Pricegroup.pm
new file mode 100644
index 000000000..047ed658c
--- /dev/null
+++ b/SL/Controller/Pricegroup.pm
@@ -0,0 +1,152 @@
+package SL::Controller::Pricegroup;
+
+use strict;
+
+use parent qw(SL::Controller::Base);
+
+use SL::Helper::Flash;
+use SL::Locale::String;
+use SL::DB::Default;
+use SL::DB::Manager::Pricegroup;
+
+use Rose::Object::MakeMethods::Generic (
+ scalar => [ qw(pricegroup) ],
+ 'scalar --get_set_init' => [ qw(all_pricegroups) ],
+);
+
+__PACKAGE__->run_before('check_auth');
+__PACKAGE__->run_before('load_pricegroup', only => [ qw(edit update delete) ]);
+
+#
+# actions
+#
+
+sub action_list {
+ my ($self) = @_;
+
+ $self->render('pricegroup/list',
+ title => t8('Pricegroups'),
+ );
+}
+
+sub action_new {
+ my ($self) = @_;
+
+ $self->pricegroup( SL::DB::Pricegroup->new );
+ $self->render('pricegroup/form',
+ title => t8('Add pricegroup'),
+ );
+}
+
+sub action_edit {
+ my ($self) = @_;
+
+ $self->render('pricegroup/form',
+ title => t8('Edit pricegroup'),
+ );
+}
+
+sub action_create {
+ my ($self) = @_;
+
+ $self->pricegroup( SL::DB::Pricegroup->new );
+ $self->create_or_update;
+}
+
+sub action_update {
+ my ($self) = @_;
+ $self->create_or_update;
+}
+
+sub action_delete {
+ my ($self) = @_;
+
+ if ( !$self->pricegroup->orphaned ) {
+ flash_later('error', $::locale->text('The pricegroup has been used and cannot be deleted.'));
+ } elsif ( eval { $self->pricegroup->delete; 1; } ) {
+ flash_later('info', $::locale->text('The pricegroup has been deleted.'));
+ } else {
+ flash_later('error', $::locale->text('The pricegroup has been used and cannot be deleted.'));
+ };
+ $self->redirect_to(action => 'list');
+}
+
+sub action_reorder {
+ my ($self) = @_;
+
+ SL::DB::Pricegroup->reorder_list(@{ $::form->{pricegroup_id} || [] });
+ $self->render(\'', { type => 'json' });
+}
+
+#
+# filters
+#
+
+sub check_auth {
+ $::auth->assert('config');
+}
+
+sub load_pricegroup {
+ my ($self) = @_;
+
+ $self->pricegroup( SL::DB::Pricegroup->new(id => $::form->{id})->load );
+}
+
+sub init_all_pricegroups { SL::DB::Manager::Pricegroup->get_all_sorted }
+
+#
+# helpers
+#
+
+sub create_or_update {
+ my ($self) = @_;
+ my $is_new = !$self->pricegroup->id;
+
+ my $params = delete($::form->{pricegroup}) || { };
+
+ $self->pricegroup->assign_attributes(%{ $params });
+
+ my @errors = $self->pricegroup->validate;
+
+ if (@errors) {
+ flash('error', @errors);
+ $self->render('pricegroup/form',
+ title => $is_new ? t8('Add pricegroup') : t8('Edit pricegroup'),
+ );
+ return;
+ }
+
+ $self->pricegroup->save;
+
+ flash_later('info', $is_new ? t8('The pricegroup has been created.') : t8('The pricegroup has been saved.'));
+ $self->redirect_to(action => 'list');
+}
+
+1;
+
+__END__
+
+=encoding utf-8
+
+=head1 NAME
+
+SL::Controller::Pricegroup - CRUD controller for pricegroups
+
+=head1 SYNOPSIS
+
+A new controller to create / edit / delete pricegroups.
+
+Pricegroups can only be deleted if they haven't been used anywhere.
+
+=head1 OBSOLETE PRICEGROUPS
+
+Pricegroups can't be obsoleted while any of the customers still use that
+pricegroup as their default pricegroup. Obsoleting a pricegroup means it can't
+be selected when editing customers and it can't be selected as a price source
+for new records.
+
+=head1 AUTHOR
+
+G. Richardson Egrichardson@kivitendo-premium.deE
+
+=cut
diff --git a/SL/DB/Manager/Pricegroup.pm b/SL/DB/Manager/Pricegroup.pm
index 6da4f216b..0e656091b 100644
--- a/SL/DB/Manager/Pricegroup.pm
+++ b/SL/DB/Manager/Pricegroup.pm
@@ -12,10 +12,8 @@ sub object_class { 'SL::DB::Pricegroup' }
__PACKAGE__->make_manager_methods;
sub _sort_spec {
- return ( default => [ 'pricegroup', 1 ],
- columns => { SIMPLE => 'ALL',
- map { ( $_ => "lower(pricegroup.${_})" ) } qw(pricegroup),
- });
+ return ( default => [ 'sortkey', 1 ],
+ columns => { SIMPLE => 'ALL' });
}
1;
diff --git a/SL/DB/MetaSetup/Pricegroup.pm b/SL/DB/MetaSetup/Pricegroup.pm
index 60a7f2038..0f3595299 100644
--- a/SL/DB/MetaSetup/Pricegroup.pm
+++ b/SL/DB/MetaSetup/Pricegroup.pm
@@ -10,7 +10,9 @@ __PACKAGE__->meta->table('pricegroup');
__PACKAGE__->meta->columns(
id => { type => 'integer', not_null => 1, sequence => 'id' },
+ obsolete => { type => 'boolean', default => 'false' },
pricegroup => { type => 'text', not_null => 1 },
+ sortkey => { type => 'integer', not_null => 1 },
);
__PACKAGE__->meta->primary_key_columns([ 'id' ]);
diff --git a/SL/DB/Pricegroup.pm b/SL/DB/Pricegroup.pm
index b3183005f..76c644d88 100644
--- a/SL/DB/Pricegroup.pm
+++ b/SL/DB/Pricegroup.pm
@@ -4,6 +4,8 @@ use strict;
use SL::DB::MetaSetup::Pricegroup;
use SL::DB::Manager::Pricegroup;
+use SL::DB::Helper::ActsAsList;
+use SL::DB::Customer;
__PACKAGE__->meta->initialize;
@@ -13,5 +15,50 @@ sub displayable_name {
return join ' ', grep $_, $self->id, $self->pricegroup;
}
+sub validate {
+ my ($self) = @_;
+
+ my @errors;
+
+ if ( $self->obsolete && SL::DB::Manager::Customer->get_all_count(query => [ pricegroup_id => $self->id ]) ) {
+ push @errors, $::locale->text('The pricegroup is being used by customers.');
+ }
+
+ return @errors;
+}
+
+sub orphaned {
+ my ($self) = @_;
+ die 'not an accessor' if @_ > 1;
+
+ return 1 unless $self->id;
+
+ my @relations = qw(
+ SL::DB::Customer
+ SL::DB::Price
+ );
+
+ # check if pricegroup is the default pricegroup for any customers and has any
+ # prices assigned.
+
+ for my $class (@relations) {
+ eval "require $class";
+ return 0 if $class->_get_manager_class->get_all_count(query => [ pricegroup_id => $self->id ]);
+ }
+
+ # check if pricegroup was used in any pricesource
+ my @item_relations = qw(
+ SL::DB::OrderItem
+ SL::DB::DeliveryOrderItem
+ SL::DB::InvoiceItem
+ );
+
+ for my $class (@item_relations) {
+ eval "require $class";
+ return 0 if $class->_get_manager_class->get_all_count(query => [ active_price_source => 'pricegroup/' . $self->id ]);
+ }
+
+ return 1;
+}
1;
diff --git a/SL/PriceSource/Pricegroup.pm b/SL/PriceSource/Pricegroup.pm
index 532b1fffa..62fabf518 100644
--- a/SL/PriceSource/Pricegroup.pm
+++ b/SL/PriceSource/Pricegroup.pm
@@ -20,10 +20,21 @@ sub available_prices {
my $item = $self->record_item;
+ my $query = [ parts_id => $item->parts_id, price => { gt => 0 } ];
+
+ # add a pricegroup_filter for obsolete pricegroups, unless part of an
+ # existing pricegroup where that pricegroup was actually used.
+ if ( $self->record->id and $item->active_price_source =~ m/^pricegroup/ ) {
+ my ($pricegroup_id) = $item->active_price_source =~ m/^pricegroup\/(\d+)$/;
+ push(@{$query}, or => [ 'pricegroup.obsolete' => 0, 'pricegroup_id' => $pricegroup_id ]);
+ } else {
+ push(@{$query}, 'pricegroup.obsolete' => 0);
+ }
+
my $prices = SL::DB::Manager::Price->get_all(
- query => [ parts_id => $item->parts_id, price => { gt => 0 } ],
+ query => $query,
with_objects => 'pricegroup',
- sort_by => 'pricegroup.id',
+ sort_by => 'pricegroup.sortkey',
);
return () unless @$prices;
diff --git a/bin/mozilla/ic.pl b/bin/mozilla/ic.pl
index cf94b7bc9..702e2b0f9 100644
--- a/bin/mozilla/ic.pl
+++ b/bin/mozilla/ic.pl
@@ -478,7 +478,7 @@ sub generate_report {
insertdate shop
);
- my $pricegroups = SL::DB::Manager::Pricegroup->get_all(sort => 'id');
+ my $pricegroups = SL::DB::Manager::Pricegroup->get_all_sorted;
my @pricegroup_columns;
my %column_defs_pricegroups;
if ($form->{l_pricegroups}) {
diff --git a/bin/mozilla/pe.pl b/bin/mozilla/pe.pl
index a9cee792d..5dc9b89ee 100644
--- a/bin/mozilla/pe.pl
+++ b/bin/mozilla/pe.pl
@@ -27,7 +27,7 @@
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#======================================================================
#
-# partsgroup, pricegroup administration
+# partsgroup administration
#
#======================================================================
@@ -62,9 +62,6 @@ sub edit {
if ($::form->{type} eq 'partsgroup') {
PE->get_partsgroup(\%::myconfig, $::form);
}
- if ($::form->{type} eq 'pricegroup') {
- PE->get_pricegroup(\%::myconfig, $::form);
- }
call_sub("form_$::form->{type}");
$::lxdebug->leave_sub;
@@ -92,12 +89,6 @@ sub save {
$::form->redirect($::locale->text('Group saved!'));
}
- # choice pricegroup and save
- if ($::form->{type} eq 'pricegroup') {
- $::form->isblank("pricegroup", $::locale->text('Pricegroup missing!'));
- PE->save_pricegroup(\%::myconfig, $::form);
- $::form->redirect($::locale->text('Pricegroup saved!'));
- }
# saving the history
if(!exists $::form->{addition} && $::form->{id} ne "") {
$::form->{snumbers} = qq|projectnumber_| . $::form->{projectnumber};
@@ -118,16 +109,6 @@ sub delete {
if ($::form->{type} eq 'partsgroup') {
$::form->redirect($::locale->text('Group deleted!'));
}
- if ($::form->{type} eq 'pricegroup') {
- $::form->redirect($::locale->text('Pricegroup deleted!'));
- }
- # saving the history
- if(!exists $::form->{addition}) {
- $::form->{snumbers} = qq|projectnumber_| . $::form->{projectnumber};
- $::form->{addition} = "DELETED";
- $::form->save_history;
- }
- # /saving the history
$::lxdebug->leave_sub;
}
@@ -177,48 +158,3 @@ sub form_partsgroup {
$::lxdebug->leave_sub;
}
-
-sub pricegroup_report {
- $::lxdebug->enter_sub;
- $::auth->assert('config');
-
- $::form->{$_} = $::form->unescape($::form->{$_}) for qw(pricegroup);
- PE->pricegroups(\%::myconfig, $::form);
-
- my $callback = build_std_url('action=pricegroup_report', qw(type status));
-
- my $option = '';
- $option .= $::locale->text('All') if $::form->{status} eq 'all';
- $option .= $::locale->text('Orphaned') if $::form->{status} eq 'orphaned';
-
- if ($::form->{pricegroup}) {
- $callback .= "&pricegroup=$::form->{pricegroup}";
- $option .= ", " . $::locale->text('Pricegroup') . " : $::form->{pricegroup}";
- }
-
- # escape callback
- $::form->{callback} = $callback;
-
- $::form->header;
- print $::form->parse_html_template('pe/pricegroup_report', {
- option => $option,
- callback => $callback,
- editlink => build_std_url('action=edit', qw(type status callback)),
- });
-
- $::lxdebug->leave_sub;
-}
-
-sub form_pricegroup {
- $::lxdebug->enter_sub;
- $::auth->assert('config');
-
- # $locale->text('Add Pricegroup')
- # $locale->text('Edit Pricegroup')
- $::form->{title} = $::locale->text("$::form->{title} Pricegroup");
-
- $::form->header;
- print $::form->parse_html_template('pe/pricegroup_form');
-
- $::lxdebug->leave_sub;
-}
diff --git a/doc/changelog b/doc/changelog
index 9bf0a7c8b..b0e70fb65 100644
--- a/doc/changelog
+++ b/doc/changelog
@@ -38,6 +38,9 @@ kleinere neue Features und Detailverbesserungen:
- Briefe sind jetzt auch für Lieferanten verfügbar. Die neuen Rechte dafür
sind für Gruppen vergeben, die auch Einkaufsbelege bearbeiten dürfen.
+ - Neuer Controller für Preisgruppen, die nun sortiert und ungültig gesetzt
+ werden können.
+
Administrative Ãnderungen
- Diverse Textsuchen werden jetzt durch eine neue Klasse Indizes
diff --git a/locale/de/all b/locale/de/all
index ec161b194..3dfa0390d 100755
--- a/locale/de/all
+++ b/locale/de/all
@@ -160,7 +160,6 @@ $self->{texts} = {
'Add Letter' => 'Brief hinzufügen',
'Add Part' => 'Ware erfassen',
'Add Price Factor' => 'Preisfaktor erfassen',
- 'Add Pricegroup' => 'Preisgruppe erfassen',
'Add Printer' => 'Drucker hinzufügen',
'Add Project' => 'Projekt erfassen',
'Add Purchase Delivery Order' => 'Lieferschein (Einkauf) erfassen',
@@ -202,6 +201,7 @@ $self->{texts} = {
'Add part' => 'Artikel hinzufügen',
'Add picture' => 'Bild hinzufügen',
'Add picture to text block' => 'Bild dem Textblock hinzufügen',
+ 'Add pricegroup' => 'Preisgruppe hinzufügen',
'Add section' => 'Abschnitt hinzufügen',
'Add sub function block' => 'Unterfunktionsblock hinzufügen',
'Add taxzone' => 'Steuerzone hinzufügen',
@@ -1055,7 +1055,6 @@ $self->{texts} = {
'Edit Part' => 'Ware bearbeiten',
'Edit Preferences for #1' => 'Einstellungen von #1 bearbeiten',
'Edit Price Factor' => 'Preisfaktor bearbeiten',
- 'Edit Pricegroup' => 'Preisgruppe bearbeiten',
'Edit Printer' => 'Drucker bearbeiten',
'Edit Purchase Delivery Order' => 'Lieferschein (Einkauf) bearbeiten',
'Edit Purchase Order' => 'Lieferantenauftrag bearbeiten',
@@ -1093,6 +1092,7 @@ $self->{texts} = {
'Edit picture' => 'Bild bearbeiten',
'Edit predefined text' => 'Vordefinierten Textblock bearbeiten',
'Edit price rule' => 'Preisregel bearbeiten',
+ 'Edit pricegroup' => 'Preisgruppe bearbeiten',
'Edit prices and discount (if not used, textfield is ONLY set readonly)' => 'Preise und Rabatt in Formularen frei anpassen (falls deaktiviert, wird allerdings NUR das textfield auf READONLY gesetzt / kann je nach Browserversion und technischen Fähigkeiten des Anwenders noch umgangen werden)',
'Edit project' => 'Projekt bearbeiten',
'Edit project #1' => 'Projekt #1 bearbeiten',
@@ -2139,9 +2139,6 @@ $self->{texts} = {
'Price sources deactivated in this client' => 'Preisquellen die in diesem Mandanten deaktiviert sind',
'Price type explanation' => 'Preistyp Erklärung',
'Pricegroup' => 'Preisgruppe',
- 'Pricegroup deleted!' => 'Preisgruppe gelöscht!',
- 'Pricegroup missing!' => 'Preisgruppe fehlt!',
- 'Pricegroup saved!' => 'Preisgruppe gespeichert!',
'Pricegroups' => 'Preisgruppen',
'Prices' => 'Preise',
'Print' => 'Drucken',
@@ -2957,6 +2954,11 @@ $self->{texts} = {
'The price rule has been saved.' => 'Die Preisregel wurde gespeichert.',
'The price rule is not a rule for discounts' => 'Die Preisregel ist keine Regel für Rabatte',
'The price rule is not a rule for prices' => 'Die Preisregel ist keine Regel für Preise',
+ 'The pricegroup has been created.' => 'Die Preisgruppe wurde erstellt.',
+ 'The pricegroup has been deleted.' => 'Die Preisgruppe wurde gelöscht.',
+ 'The pricegroup has been saved.' => 'Die Preisgruppe wurde gespeichert.',
+ 'The pricegroup has been used and cannot be deleted.' => 'Die Preisgruppe wurde bereits verwendet und kann nicht gelöscht werden.',
+ 'The pricegroup is being used by customers.' => 'Die Preisgruppe wird von Kunden verwendet.',
'The printer could not be deleted.' => 'Der Drucker konnte nicht gelöscht werden.',
'The printer has been created.' => 'Der Drucker wurde angelegt.',
'The printer has been deleted.' => 'Der Drucker wurde entfernt.',
diff --git a/menus/user/00-erp.yaml b/menus/user/00-erp.yaml
index 485cd826f..5f2af6572 100644
--- a/menus/user/00-erp.yaml
+++ b/menus/user/00-erp.yaml
@@ -1065,10 +1065,8 @@
id: system_pricegroups
name: Pricegroups
order: 1000
- module: pe.pl
params:
- action: search
- type: pricegroup
+ action: Pricegroup/list
- parent: system
id: system_edit_units
name: Edit units
diff --git a/sql/Pg-upgrade2/pricegroup_sortkey_obsolete.sql b/sql/Pg-upgrade2/pricegroup_sortkey_obsolete.sql
new file mode 100644
index 000000000..dc6b33166
--- /dev/null
+++ b/sql/Pg-upgrade2/pricegroup_sortkey_obsolete.sql
@@ -0,0 +1,13 @@
+-- @tag: pricegroup_sortkey_obsolete
+-- @description: Sortierreihenfolge und ungültig für Preisgruppen
+-- @charset: UTF-8
+-- @depends: release_3_4_1
+-- @ignore: 0
+
+ALTER TABLE pricegroup ADD COLUMN obsolete BOOLEAN DEFAULT FALSE;
+ALTER TABLE pricegroup ADD COLUMN sortkey INTEGER;
+
+CREATE SEQUENCE tmp_counter;
+UPDATE pricegroup SET sortkey = nextval('tmp_counter');
+DROP SEQUENCE tmp_counter;
+ALTER TABLE pricegroup ALTER COLUMN sortkey SET NOT NULL;
diff --git a/templates/webpages/pe/pricegroup_form.html b/templates/webpages/pe/pricegroup_form.html
deleted file mode 100644
index 850275fbf..000000000
--- a/templates/webpages/pe/pricegroup_form.html
+++ /dev/null
@@ -1,41 +0,0 @@
-[%- USE L %]
-[%- USE T8 %]
-[%- USE HTML %]
-[% title %]
-[% L.javascript_tag('show_history.js') %]
-
-
-
diff --git a/templates/webpages/pe/pricegroup_report.html b/templates/webpages/pe/pricegroup_report.html
deleted file mode 100644
index 0b04b9d95..000000000
--- a/templates/webpages/pe/pricegroup_report.html
+++ /dev/null
@@ -1,35 +0,0 @@
-[%- USE HTML %]
-[%- USE T8 %]
-[% 'Pricegroup' | $T8 %]
-
-
-
- [% option %] |
-
-
-
-
-
- [% 'Pricegroup' | $T8 %] |
-
-[%- FOREACH row = item_list %]
-
- [% row.pricegroup %] |
-
-[%- END %]
-
- |
-
-
-
|
-
-
-
-
-
-
-
diff --git a/templates/webpages/pricegroup/form.html b/templates/webpages/pricegroup/form.html
new file mode 100644
index 000000000..7552caa25
--- /dev/null
+++ b/templates/webpages/pricegroup/form.html
@@ -0,0 +1,50 @@
+[%- USE HTML -%][%- USE LxERP -%][%- USE L -%][%- USE T8 -%]
+
+[% SET style="width: 400px" %]
+[% SET size=15 %]
+
+[% HTML.escape(title) %]
+
+
+
+
+
+[% L.sortable_element('#pricegroup_list tbody', url=SELF.url_for(action='reorder'), with='pricegroup_id') %]
+
+
+ [%- 'Add' | $T8 %]
+