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 units_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 => '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 {
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);
$self->handle_unit($entry);
$self->handle_sellprice($entry);
+ $self->handle_discount($entry);
# copy from part if not given
$object->description($part_obj->description) unless $object->description;
$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);
}
}
+sub handle_discount {
+ my ($self, $entry) = @_;
+
+ my $object = $entry->{object};
+
+ $object->discount($object->discount/100.0) if $object->discount;
+ $object->discount(0) unless $object->discount;
+}
+
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;
}