From f63af42d0b025b20a5836dad9da6c07388b5da7a Mon Sep 17 00:00:00 2001 From: Moritz Bunkus Date: Mon, 27 Apr 2015 11:27:27 +0200 Subject: [PATCH] =?utf8?q?Beleg-Rose-Objekte:=20items=5Fsorted=20f=C3=BCr?= =?utf8?q?=20nicht=20gespeicherte=20Items=20gefixt?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Die bisherigen items_sorted-Routinen verlangen, dass die Positionsspalte gesetzt ist. Das ist bei noch nicht gespeicherten Belegen oder bei gerade hinzugefügten Positionen aber noch nicht der Fall. Diese neuen Positionen werden nun stabil ans Ende sortiert. --- SL/DB/DeliveryOrder.pm | 13 +---- SL/DB/Helper/AttrSorted.pm | 110 +++++++++++++++++++++++++++++++++++++ SL/DB/Invoice.pm | 11 +--- SL/DB/Order.pm | 8 +-- SL/DB/PurchaseInvoice.pm | 8 +-- 5 files changed, 118 insertions(+), 32 deletions(-) create mode 100644 SL/DB/Helper/AttrSorted.pm diff --git a/SL/DB/DeliveryOrder.pm b/SL/DB/DeliveryOrder.pm index b530db731..436c9b1a5 100644 --- a/SL/DB/DeliveryOrder.pm +++ b/SL/DB/DeliveryOrder.pm @@ -9,6 +9,7 @@ use Rose::DB::Object::Helpers (); use SL::DB::MetaSetup::DeliveryOrder; use SL::DB::Manager::DeliveryOrder; use SL::DB::Helper::AttrHTML; +use SL::DB::Helper::AttrSorted; use SL::DB::Helper::FlattenToForm; use SL::DB::Helper::LinkedRecords; use SL::DB::Helper::TransNumberGenerator; @@ -31,6 +32,7 @@ __PACKAGE__->meta->add_relationship(orderitems => { type => 'one to many __PACKAGE__->meta->initialize; __PACKAGE__->attr_html('notes'); +__PACKAGE__->attr_sorted('items'); __PACKAGE__->before_save('_before_save_set_donumber'); @@ -49,12 +51,6 @@ sub _before_save_set_donumber { sub items { goto &orderitems; } sub add_items { goto &add_orderitems; } -sub items_sorted { - my ($self) = @_; - - return [ sort {$a->position <=> $b->position } @{ $self->items } ]; -} - sub sales_order { my $self = shift; my %params = @_; @@ -208,11 +204,6 @@ closed and delivered. An alias for C for compatibility with other sales/purchase models. -=item C - -Returns the delivery order items sorted by their ID (same order they -appear in the frontend delivery order masks). - =item C Creates a new C instance and copies as much diff --git a/SL/DB/Helper/AttrSorted.pm b/SL/DB/Helper/AttrSorted.pm new file mode 100644 index 000000000..77c5f19bc --- /dev/null +++ b/SL/DB/Helper/AttrSorted.pm @@ -0,0 +1,110 @@ +package SL::DB::Helper::AttrSorted; + +use Carp; +use List::Util qw(max); + +use strict; + +use parent qw(Exporter); +our @EXPORT = qw(attr_sorted); + +sub attr_sorted { + my ($package, @attributes) = @_; + + _make_sorted($package, $_) for @attributes; +} + +sub _make_sorted { + my ($package, $attribute) = @_; + + my %params = ref($attribute) eq 'HASH' ? %{ $attribute } : ( unsorted => $attribute ); + my $unsorted_sub = $params{unsorted}; + my $sorted_sub = $params{sorted} // $params{unsorted} . '_sorted'; + my $position_sub = $params{position} // 'position'; + + no strict 'refs'; + + *{ $package . '::' . $sorted_sub } = sub { + my ($self) = @_; + + croak 'not an accessor' if @_ > 1; + + my $next_position = (max map { $_->$position_sub // 0 } @{ $self->$unsorted_sub }) + 1; + return [ + map { $_->[1] } + sort { $a->[0] <=> $b->[0] } + map { [ $_->$position_sub // ($next_position++), $_ ] } + @{ $self->$unsorted_sub } + ]; + }; +} + +1; +__END__ + +=pod + +=encoding utf8 + +=head1 NAME + +SL::DB::Helper::AttrSorted - Attribute helper for sorting to-many +relationships by a positional attribute + +=head1 SYNOPSIS + + # In a Rose model: + use SL::DB::Helper::AttrSorted; + __PACKAGE__->attr_sorted('items'); + + # Use in controller or whereever: + my $items = @{ $invoice->items_sorted }; + +=head1 OVERVIEW + +Creates a function that returns a sorted relationship. Requires that +the linked objects have some kind of positional column. + +Items for which no position has been set (e.g. because they haven't +been saved yet) are sorted last but kept in the order they appear in +the unsorted list. + +=head1 FUNCTIONS + +=over 4 + +=item C + +Package method. Call with the names of the attributes for which the +helper methods should be created. Each attribute name can be either a +scalar or a hash reference if you need custom options. + +If it's a hash reference then the following keys are supported: + +=over 2 + +=item * C is the name of the relationship accessor that +returns the list to be sorted. This is required, and if only a scalar +is given instead of a hash reference then that scalar value is +interpreted as C. + +=item * C is the name of the new function to create. It +defaults to the unsorted name postfixed with C<_sorted>. + +=item * C must be a function name to be called on the +objects to be sorted. It is supposed to return either C (no +position has been set yet) or a numeric value. Defaults to C. + +=back + +=back + +=head1 BUGS + +Nothing here yet. + +=head1 AUTHOR + +Moritz Bunkus Em.bunkus@linet-services.deE + +=cut diff --git a/SL/DB/Invoice.pm b/SL/DB/Invoice.pm index 3e6c65a49..0added1c0 100644 --- a/SL/DB/Invoice.pm +++ b/SL/DB/Invoice.pm @@ -1,6 +1,3 @@ -# This file has been auto-generated only because it didn't exist. -# Feel free to modify it at will; it will not be overwritten automatically. - package SL::DB::Invoice; use strict; @@ -13,6 +10,7 @@ use Rose::DB::Object::Helpers (); use SL::DB::MetaSetup::Invoice; use SL::DB::Manager::Invoice; use SL::DB::Helper::AttrHTML; +use SL::DB::Helper::AttrSorted; use SL::DB::Helper::FlattenToForm; use SL::DB::Helper::LinkedRecords; use SL::DB::Helper::PriceTaxCalculator; @@ -60,6 +58,7 @@ __PACKAGE__->meta->add_relationship( __PACKAGE__->meta->initialize; __PACKAGE__->attr_html('notes'); +__PACKAGE__->attr_sorted('items'); __PACKAGE__->before_save('_before_save_set_invnumber'); @@ -78,12 +77,6 @@ sub _before_save_set_invnumber { sub items { goto &invoiceitems; } sub add_items { goto &add_invoiceitems; } -sub items_sorted { - my ($self) = @_; - - return [ sort {$a->position <=> $b->position } @{ $self->items } ]; -} - sub is_sales { # For compatibility with Order, DeliveryOrder croak 'not an accessor' if @_ > 1; diff --git a/SL/DB/Order.pm b/SL/DB/Order.pm index 48e2be664..f73e32ca0 100644 --- a/SL/DB/Order.pm +++ b/SL/DB/Order.pm @@ -10,6 +10,7 @@ use List::Util qw(max); use SL::DB::MetaSetup::Order; use SL::DB::Manager::Order; use SL::DB::Helper::AttrHTML; +use SL::DB::Helper::AttrSorted; use SL::DB::Helper::FlattenToForm; use SL::DB::Helper::LinkedRecords; use SL::DB::Helper::PriceTaxCalculator; @@ -43,6 +44,7 @@ __PACKAGE__->meta->add_relationship( __PACKAGE__->meta->initialize; __PACKAGE__->attr_html('notes'); +__PACKAGE__->attr_sorted('items'); __PACKAGE__->before_save('_before_save_set_ord_quo_number'); @@ -66,12 +68,6 @@ sub _before_save_set_ord_quo_number { sub items { goto &orderitems; } sub add_items { goto &add_orderitems; } -sub items_sorted { - my ($self) = @_; - - return [ sort {$a->position <=> $b->position } @{ $self->items } ]; -} - sub type { my $self = shift; diff --git a/SL/DB/PurchaseInvoice.pm b/SL/DB/PurchaseInvoice.pm index 19808eba5..fbb990329 100644 --- a/SL/DB/PurchaseInvoice.pm +++ b/SL/DB/PurchaseInvoice.pm @@ -7,6 +7,7 @@ use Carp; use SL::DB::MetaSetup::PurchaseInvoice; use SL::DB::Manager::PurchaseInvoice; use SL::DB::Helper::AttrHTML; +use SL::DB::Helper::AttrSorted; use SL::DB::Helper::LinkedRecords; use SL::Locale::String qw(t8); @@ -44,16 +45,11 @@ __PACKAGE__->meta->add_relationship( __PACKAGE__->meta->initialize; __PACKAGE__->attr_html('notes'); +__PACKAGE__->attr_sorted('items'); sub items { goto &invoiceitems; } sub add_items { goto &add_invoiceitems; } -sub items_sorted { - my ($self) = @_; - - return [ sort {$a->position <=> $b->position } @{ $self->items } ]; -} - sub is_sales { # For compatibility with Order, DeliveryOrder croak 'not an accessor' if @_ > 1; -- 2.20.1