use strict;
-use List::MoreUtils qw(any);
+use List::MoreUtils qw(any none);
use SL::Helper::Csv;
use SL::Controller::CsvImport::Helper::Consistency;
use SL::DB::Project;
use SL::DB::Shipto;
use SL::DB::TaxZone;
-use SL::TransNumber;
+use SL::DB::Unit;
+use SL::PriceSource;
use parent qw(SL::Controller::CsvImport::BaseMulti);
use Rose::Object::MakeMethods::Generic
(
- 'scalar --get_set_init' => [ qw(settings languages_by parts_by contacts_by ct_shiptos_by price_factors_by pricegroups_by) ],
+ 'scalar --get_set_init' => [ qw(settings languages_by all_parts parts_by part_counts_by contacts_by ct_shiptos_by price_factors_by pricegroups_by units_by) ],
);
{ name => 'taxincluded', description => $::locale->text('Tax Included') },
{ name => 'customer', description => $::locale->text('Customer (name)') },
{ name => 'customernumber', description => $::locale->text('Customer Number') },
+ { name => 'customer_gln', description => $::locale->text('Customer GLN') },
{ name => 'customer_id', description => $::locale->text('Customer (database ID)') },
{ name => 'vendor', description => $::locale->text('Vendor (name)') },
{ name => 'vendornumber', description => $::locale->text('Vendor Number') },
+ { name => 'vendor_gln', description => $::locale->text('Vendor GLN') },
{ name => 'vendor_id', description => $::locale->text('Vendor (database ID)') },
{ name => 'language_id', description => $::locale->text('Language (database ID)') },
{ name => 'language', description => $::locale->text('Language (name)') },
{ name => 'cusordnumber', description => $::locale->text('Customer Order Number') },
{ name => 'description', description => $::locale->text('Description') },
{ name => 'discount', description => $::locale->text('Discount') },
+ { name => 'ean', description => $::locale->text('EAN') },
{ name => 'lastcost', description => $::locale->text('Lastcost') },
{ name => 'longdescription', description => $::locale->text('Long Description') },
{ name => 'marge_percent', description => $::locale->text('Margepercent') },
return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $self->all_languages } } ) } qw(id description article_code) };
}
+sub init_all_parts {
+ my ($self) = @_;
+
+ return SL::DB::Manager::Part->get_all;
+}
+
sub init_parts_by {
my ($self) = @_;
- my $all_parts = SL::DB::Manager::Part->get_all;
- return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $all_parts } } ) } qw(id partnumber ean description) };
+ return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $self->all_parts } } ) } qw(id partnumber ean description) };
+}
+
+sub init_part_counts_by {
+ my ($self) = @_;
+
+ my $part_counts_by;
+
+ $part_counts_by->{ean}-> {$_->ean}++ for @{ $self->all_parts };
+ $part_counts_by->{description}->{$_->description}++ for @{ $self->all_parts };
+
+ return $part_counts_by;
}
sub init_contacts_by {
return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $all_pricegroups } } ) } qw(id pricegroup) };
}
+sub init_units_by {
+ my ($self) = @_;
+
+ my $all_units = SL::DB::Manager::Unit->get_all;
+ return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $all_units } } ) } qw(name) };
+}
+
sub check_objects {
my ($self) = @_;
my $i = 0;
my $num_data = scalar @{ $self->controller->data };
+ my $order_entry;
foreach my $entry (@{ $self->controller->data }) {
$self->controller->track_progress(progress => $i/$num_data * 100) if $i % 100 == 0;
+ $entry->{info_data}->{datatype} = $entry->{raw_data}->{datatype};
+
if ($entry->{raw_data}->{datatype} eq $self->_order_column) {
$self->handle_order($entry);
+ $order_entry = $entry;
} elsif ($entry->{raw_data}->{datatype} eq $self->_item_column && $entry->{object}->can('part')) {
- $self->handle_item($entry);
+ $self->handle_item($entry, $order_entry);
+ } else {
+ $order_entry = undef;
}
+
$self->handle_cvars($entry, sub_module => 'orderitems');
} continue {
$i++;
}
+ $self->add_info_columns($self->_order_column,
+ { header => $::locale->text('Data type'), method => 'datatype' });
+ $self->add_info_columns($self->_item_column,
+ { header => $::locale->text('Data type'), method => 'datatype' });
+
$self->add_info_columns($self->_order_column,
{ header => $::locale->text('Customer/Vendor'), method => 'vc_name' });
# Todo: access via ->[0] ok? Better: search first order column and use this
$self->add_items_to_order();
$self->handle_prices_and_taxes();
-
-
- # If order has errors set error for orderitems as well
- my $order_entry;
- foreach my $entry (@{ $self->controller->data }) {
- # Search first order
- if ($entry->{raw_data}->{datatype} eq $self->_order_column) {
- $order_entry = $entry;
- } elsif ( defined $order_entry
- && $entry->{raw_data}->{datatype} eq $self->_item_column
- && scalar @{ $order_entry->{errors} } > 0 ) {
- push @{ $entry->{errors} }, $::locale->text('Error: Invalid order for this order item');
- }
- }
-
}
sub handle_order {
my $object = $entry->{object};
my $vc_obj;
- if (any { $entry->{raw_data}->{$_} } qw(customer customernumber customer_id)) {
+ if (any { $entry->{raw_data}->{$_} } qw(customer customernumber customer_gln customer_id)) {
$self->check_vc($entry, 'customer_id');
$vc_obj = SL::DB::Customer->new(id => $object->customer_id)->load if $object->customer_id;
- } elsif (any { $entry->{raw_data}->{$_} } qw(vendor vendornumber vendor_id)) {
+ } elsif (any { $entry->{raw_data}->{$_} } qw(vendor vendornumber vendor_gln vendor_id)) {
$self->check_vc($entry, 'vendor_id');
$vc_obj = SL::DB::Vendor->new(id => $object->vendor_id)->load if $object->vendor_id;
} else {
if ($vc_obj) {
# copy from customer if not given
- foreach (qw(payment_id language_id taxzone_id currency_id)) {
+ foreach (qw(payment_id delivery_term_id language_id taxzone_id currency_id)) {
$object->$_($vc_obj->$_) unless $object->$_;
}
+ $object->intnotes($vc_obj->notes) unless $object->intnotes;
}
$self->handle_salesman($entry);
}
sub handle_item {
- my ($self, $entry) = @_;
+ my ($self, $entry, $order_entry) = @_;
+
+ return unless $order_entry;
my $object = $entry->{object};
+
return unless $self->check_part($entry);
my $part_obj = SL::DB::Part->new(id => $object->parts_id)->load;
+ $self->handle_unit($entry);
+
# copy from part if not given
$object->description($part_obj->description) unless $object->description;
$object->longdescription($part_obj->notes) unless $object->longdescription;
- $object->unit($part_obj->unit) unless $object->unit;
- $object->sellprice($part_obj->sellprice) unless defined $object->sellprice;
$object->lastcost($part_obj->lastcost) unless defined $object->lastcost;
# set to 0 if not given
- $object->discount(0) unless $object->discount;
$object->ship(0) unless $object->ship;
$self->check_project($entry, global => 0);
$self->check_price_factor($entry);
$self->check_pricegroup($entry);
+
+ $self->handle_sellprice($entry, $order_entry);
+ $self->handle_discount($entry, $order_entry);
+}
+
+sub handle_unit {
+ my ($self, $entry) = @_;
+
+ my $object = $entry->{object};
+
+ # Set unit from part if not given.
+ if (!$object->unit) {
+ $object->unit($object->part->unit);
+ return 1;
+ }
+
+ # Check whether or not unit is valid.
+ if ($object->unit && !$self->units_by->{name}->{ $object->unit }) {
+ push @{ $entry->{errors} }, $::locale->text('Error: Invalid unit');
+ return 0;
+ }
+
+ # Check whether unit is convertible to parts unit
+ if (none { $object->unit eq $_ } map { $_->name } @{ $object->part->unit_obj->convertible_units }) {
+ push @{ $entry->{errors} }, $::locale->text('Error: Invalid unit');
+ return 0;
+ }
+
+ return 1;
+}
+
+sub handle_sellprice {
+ my ($self, $entry, $record_entry) = @_;
+
+ my $item = $entry->{object};
+ my $record = $record_entry->{object};
+
+ return if !$record->customervendor;
+
+ # If sellprice is given, set price source to pricegroup if given or to none.
+ if (exists $entry->{raw_data}->{sellprice}) {
+ my $price_source = SL::PriceSource->new(record_item => $item, record => $record);
+ my $price_source_spec = $item->pricegroup_id ? 'pricegroup' . '/' . $item->pricegroup_id : '';
+ my $price = $price_source->price_from_source($price_source_spec);
+ $item->active_price_source($price->source);
+
+ } else {
+ # Set sellprice the best price of price source
+ my $price_source = SL::PriceSource->new(record_item => $item, record => $record);
+ my $price = $price_source->best_price;
+ if ($price) {
+ $item->sellprice($price->price);
+ $item->active_price_source($price->source);
+ } else {
+ $item->sellprice(0);
+ $item->active_price_source($price_source->price_from_source('')->source);
+ }
+ }
+}
+
+sub handle_discount {
+ my ($self, $entry, $record_entry) = @_;
+
+ my $item = $entry->{object};
+ my $record = $record_entry->{object};
+
+ return if !$record->customervendor;
+
+ # If discount is given, set discount source to none.
+ if (exists $entry->{raw_data}->{discount}) {
+ $item->discount($item->discount/100.0) if $item->discount;
+ $item->discount(0) unless $item->discount;
+
+ my $price_source = SL::PriceSource->new(record_item => $item, record => $record);
+ my $discount = $price_source->price_from_source('');
+ $item->active_discount_source($discount->source);
+
+ } else {
+ # Set discount the best discount of price source
+ my $price_source = SL::PriceSource->new(record_item => $item, record => $record);
+ my $discount = $price_source->best_discount;
+ if ($discount) {
+ $item->discount($discount->discount);
+ $item->active_discount_source($discount->source);
+ } else {
+ $item->discount(0);
+ $item->active_discount_source($price_source->discount_from_source('')->source);
+ }
+ }
}
sub check_part {
my ($self, $entry) = @_;
my $object = $entry->{object};
+ my $is_ambiguous;
# Check whether or not part ID is valid.
if ($object->parts_id && !$self->parts_by->{id}->{ $object->parts_id }) {
return 0;
}
- $object->parts_id($part->id);
+ if ($self->part_counts_by->{description}->{ $entry->{raw_data}->{description} } > 1) {
+ $is_ambiguous = 1;
+ } else {
+ $object->parts_id($part->id);
+ }
+ }
+
+ # Map ean to ID if given.
+ if (!$object->parts_id && $entry->{raw_data}->{ean}) {
+ my $part = $self->parts_by->{ean}->{ $entry->{raw_data}->{ean} };
+ if (!$part) {
+ push @{ $entry->{errors} }, $::locale->text('Error: Invalid part');
+ return 0;
+ }
+
+ if ($self->part_counts_by->{ean}->{ $entry->{raw_data}->{ean} } > 1) {
+ $is_ambiguous = 1;
+ } else {
+ $object->parts_id($part->id);
+ }
}
if ($object->parts_id) {
$entry->{info_data}->{partnumber} = $self->parts_by->{id}->{ $object->parts_id }->partnumber;
} else {
- push @{ $entry->{errors} }, $::locale->text('Error: Part not found');
+ if ($is_ambiguous) {
+ push @{ $entry->{errors} }, $::locale->text('Error: Part is ambiguous');
+ } else {
+ push @{ $entry->{errors} }, $::locale->text('Error: Part not found');
+ }
+ return 0;
+ }
+
+ if ($self->parts_by->{id}->{ $object->parts_id }->obsolete) {
+ push @{ $entry->{errors} }, $::locale->text('Error: Part is obsolete');
return 0;
}
sub save_objects {
my ($self, %params) = @_;
- # set order number and collect to save
- my $objects_to_save;
+ # Collect orders without errors to save.
+ my $entries_to_save = [];
foreach my $entry (@{ $self->controller->data }) {
next if $entry->{raw_data}->{datatype} ne $self->_order_column;
next if @{ $entry->{errors} };
- if (!$entry->{object}->ordnumber) {
- my $number = SL::TransNumber->new(type => 'sales_order',
- save => 1);
- $entry->{object}->ordnumber($number->create_unique());
- }
-
- push @{ $objects_to_save }, $entry;
+ push @{ $entries_to_save }, $entry;
}
- $self->SUPER::save_objects(data => $objects_to_save);
+ $self->SUPER::save_objects(data => $entries_to_save);
}
sub _order_column {