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 => {
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);
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
}
return SL::PriceSource::Price->new(
price => $part->sellprice,
- source => 'master_data/sellprice',
+ spec => 'sellprice',
description => t8('Sellprice'),
price_source => $self,
);
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) = @_;
use SL::PriceSource::Price;
use SL::Locale::String;
+use List::UtilsBy qw(min_by);
+use List::Util qw(first);
sub name { 'pricegroup' }
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,
)
my $totalweight = 0;
+ my $record = _make_record();
# rows
my @ROWS;
$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"};
$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)");
$::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) = @_;
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"});
}
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;
+}
+
check_oe_access();
- my $order = _make_record();
-
set_headings($form->{"id"} ? "edit" : "add");
$form->{update} = 1;
$::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;
-}
-