From 0409db7c50da81e1eb613bd33663fb5b07935a01 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Sven=20Sch=C3=B6ling?= Date: Mon, 28 Jul 2014 11:15:38 +0200 Subject: [PATCH] =?utf8?q?PriceSource:=20Mehr=20Informationen=20an=20Preis?= =?utf8?q?e=20=C3=BCbergeben?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit record + record_item verfügbar gemacht best_price für pricegroups Price: spec/source entzerrt --- SL/DB/Order.pm | 1 + SL/PriceSource.pm | 4 +-- SL/PriceSource/MasterData.pm | 2 +- SL/PriceSource/Price.pm | 8 +++++- SL/PriceSource/Pricegroup.pm | 18 +++++++++++- bin/mozilla/io.pl | 56 ++++++++++++++++++++++++++++++++++-- bin/mozilla/oe.pl | 27 ----------------- 7 files changed, 82 insertions(+), 34 deletions(-) diff --git a/SL/DB/Order.pm b/SL/DB/Order.pm index 9e7817344..2546c4fc6 100644 --- a/SL/DB/Order.pm +++ b/SL/DB/Order.pm @@ -15,6 +15,7 @@ use SL::DB::Helper::PriceTaxCalculator; use SL::DB::Helper::PriceUpdater; use SL::DB::Helper::TransNumberGenerator; use SL::RecordLinks; +use Rose::DB::Object::Helpers qw(as_tree); __PACKAGE__->meta->add_relationship( orderitems => { diff --git a/SL/PriceSource.pm b/SL/PriceSource.pm index 9a3a978c5..7a631bab4 100644 --- a/SL/PriceSource.pm +++ b/SL/PriceSource.pm @@ -3,7 +3,7 @@ package SL::PriceSource; use strict; use parent 'SL::DB::Object'; use Rose::Object::MakeMethods::Generic ( - scalar => [ qw(record_item) ], + scalar => [ qw(record_item record) ], ); use List::UtilsBy qw(min_by); @@ -15,7 +15,7 @@ sub all_price_sources { my ($self) = @_; return map { - $_->new(record_item => $self->record_item) + $_->new(record_item => $self->record_item, record => $self->record) } SL::PriceSource::ALL->all_price_sources } diff --git a/SL/PriceSource/MasterData.pm b/SL/PriceSource/MasterData.pm index 56b0265ad..c639ccb49 100644 --- a/SL/PriceSource/MasterData.pm +++ b/SL/PriceSource/MasterData.pm @@ -34,7 +34,7 @@ sub make_sellprice { return SL::PriceSource::Price->new( price => $part->sellprice, - source => 'master_data/sellprice', + spec => 'sellprice', description => t8('Sellprice'), price_source => $self, ); diff --git a/SL/PriceSource/Price.pm b/SL/PriceSource/Price.pm index ccfdb07ba..0a8fc4f5f 100644 --- a/SL/PriceSource/Price.pm +++ b/SL/PriceSource/Price.pm @@ -4,10 +4,16 @@ use strict; use parent 'SL::DB::Object'; use Rose::Object::MakeMethods::Generic ( - scalar => [ qw(price description source price_source) ], + scalar => [ qw(price description spec price_source) ], array => [ qw(depends_on) ] ); +sub source { + $_[0]->price_source + ? $_[0]->price_source->name . '/' . $_[0]->spec + : ''; +} + sub full_description { my ($self) = @_; diff --git a/SL/PriceSource/Pricegroup.pm b/SL/PriceSource/Pricegroup.pm index 4b73ac8ff..b1c75f879 100644 --- a/SL/PriceSource/Pricegroup.pm +++ b/SL/PriceSource/Pricegroup.pm @@ -5,6 +5,8 @@ use parent qw(SL::PriceSource::Base); use SL::PriceSource::Price; use SL::Locale::String; +use List::UtilsBy qw(min_by); +use List::Util qw(first); sub name { 'pricegroup' } @@ -36,12 +38,26 @@ sub price_from_source { return $self->make_price($price); } +sub best_price { + my ($self, %params) = @_; + + my @prices = $self->availabe_prices; + my $customer = $self->record->customer; + my $min_price = min_by { $_->price } @prices; + + return $min_price if !$customer || !$customer->cv_klass; + + my $best_price = first { $_->spec == $customer->cv_class } @prices; + + return $best_price || $min_price; +} + sub make_price { my ($self, $price_obj) = @_; SL::PriceSource::Price->new( price => $price_obj->price, - source => 'pricegroup/' . $price_obj->id, + spec => $price_obj->id, description => $price_obj->pricegroup->pricegroup, price_source => $self, ) diff --git a/bin/mozilla/io.pl b/bin/mozilla/io.pl index e9fbad82a..49e352894 100644 --- a/bin/mozilla/io.pl +++ b/bin/mozilla/io.pl @@ -218,6 +218,7 @@ sub display_row { my $totalweight = 0; + my $record = _make_record(); # rows my @ROWS; @@ -233,7 +234,7 @@ sub display_row { $form->{"sellprice_$i"} = $form->{"price_new_$i"}; } - my $record_item = _make_record_item($i); + my $record_item = $record->items->[$i-1]; # unit begin $form->{"unit_old_$i"} ||= $form->{"unit_$i"}; @@ -322,7 +323,7 @@ sub display_row { $column_data{weight} = $form->format_amount(\%myconfig, $form->{"qty_$i"} * $form->{"weight_$i"}, 3) . ' ' . $defaults->{weightunit} if $defaults->{show_weight}; if ($form->{"id_${i}"}) { - my $price_source = SL::PriceSource->new(record_item => $record_item); + my $price_source = SL::PriceSource->new(record_item => $record_item, record => $record); my $price = $price_source->price_from_source($::form->{"active_price_source_$i"}); $::form->{price_sources}[$i] = $price_source; $column_data{price_source} .= $cgi->button(-value => $price->full_description, -onClick => "toggle_price_source($i)"); @@ -1890,6 +1891,8 @@ sub _remove_billed_or_delivered_rows { $::form->{rowcount} -= $removed_rows; } +# TODO: both of these are makeshift so that price sources can operate on rdbo objects. if +# this ever gets rewritten in controller style, throw this out sub _make_record_item { my ($row) = @_; @@ -1918,6 +1921,8 @@ sub _make_record_item { next unless $obj->meta->column($method); if ($obj->meta->column($method)->isa('Rose::DB::Object::Metadata::Column::Date')) { $obj->${\"$method\_as_date"}($::form->{"$method\_$row"}); + } elsif ((ref $obj->meta->column($method)) =~ /^Rose::DB::Object::Metadata::Column::(?:Numeric|Float|DoublePrecsion)$/) { + $obj->${\"$method\_as\_number"}($::form->{$method}); } else { $obj->$method($::form->{"$method\_$row"}); } @@ -1929,3 +1934,50 @@ sub _make_record_item { return $obj; } + +sub _make_record { + my $class = { + sales_order => 'Order', + purchase_oder => 'Order', + sales_quotation => 'Order', + request_quotation => 'Order', + invoice => 'Invoice', + purchase_invoice => 'PurchaseInvoice', + purchase_delivery_order => 'DeliveryOrder', + sales_delivery_order => 'DeliveryOrder', + }->{$::form->{type}}; + + return unless $class; + + $class = 'SL::DB::' . $class; + + eval "require $class"; + + my $obj = $::form->{id} + ? $class->meta->convention_manager->auto_manager_class_name->find_by(id => $::form->{id}) + : $class->new; + + for my $method (keys %$::form) { + next unless $obj->can($method); + next unless $obj->meta->column($method); + + if ($obj->meta->column($method)->isa('Rose::DB::Object::Metadata::Column::Date')) { + $obj->${\"$method\_as_date"}($::form->{$method}); + } elsif ((ref $obj->meta->column($method)) =~ /^Rose::DB::Object::Metadata::Column::(?:Numeric|Float|DoublePrecsion)$/) { + $obj->${\"$method\_as\_number"}($::form->{$method}); + } else { + $obj->$method($::form->{$method}); + } + } + + my @items; + for my $i (1 .. $::form->{rowcount}) { + next unless $::form->{"id_$i"}; + push @items, _make_record_item($i) + } + + $obj->orderitems(@items); + + return $obj; +} + diff --git a/bin/mozilla/oe.pl b/bin/mozilla/oe.pl index b9abb0f16..8424f9c64 100644 --- a/bin/mozilla/oe.pl +++ b/bin/mozilla/oe.pl @@ -594,8 +594,6 @@ sub update { check_oe_access(); - my $order = _make_record(); - set_headings($form->{"id"} ? "edit" : "add"); $form->{update} = 1; @@ -2111,28 +2109,3 @@ sub dispatcher { $::form->error($::locale->text('No action defined.')); } -sub _make_record { - my $obj = SL::DB::Order->new; - - for my $method (keys %$::form) { - next unless $obj->can($method); - next unless $obj->meta->column($method); - - if ($obj->meta->column($method)->isa('Rose::DB::Object::Metadata::Column::Date')) { - $obj->${\"$method\_as_date"}($::form->{$method}); - } elsif ((ref $obj->meta->column($method)) =~ /^Rose::DB::Object::Metadata::Column::(?:Integer|Numeric|Float|DoublePrecsion)$/) { - $obj->$method($::form->{$method}); - } - } - - my @items; - for my $i (1 .. $::form->{rowcount}) { - next unless $::form->{"id_$i"}; - push @items, _make_record_item($i) - } - - $obj->orderitems(@items); - - return $obj; -} - -- 2.20.1