X-Git-Url: http://wagnertech.de/git?p=kivitendo-erp.git;a=blobdiff_plain;f=SL%2FController%2FCsvImport%2FOrder.pm;fp=SL%2FController%2FCsvImport%2FOrder.pm;h=503b4d18547cce5dd8d66ec616b3e01650a3e1ea;hp=e0935c0e6ca521e19d550116549f0b1ba33213c4;hb=53593baa211863fbf66540cf1bcc36c8fb37257f;hpb=deb4d2dbb676d7d6f69dfe7815d6e0cb09bd4a44 diff --git a/SL/Controller/CsvImport/Order.pm b/SL/Controller/CsvImport/Order.pm index e0935c0e6..503b4d185 100644 --- a/SL/Controller/CsvImport/Order.pm +++ b/SL/Controller/CsvImport/Order.pm @@ -3,7 +3,7 @@ package SL::Controller::CsvImport::Order; use strict; -use List::MoreUtils qw(any); +use List::MoreUtils qw(any none); use SL::Helper::Csv; use SL::Controller::CsvImport::Helper::Consistency; @@ -18,14 +18,15 @@ use SL::DB::Pricegroup; 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 departments_by projects_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) ], ); @@ -132,9 +133,11 @@ sub setup_displayable_columns { { 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)') }, @@ -159,6 +162,7 @@ sub setup_displayable_columns { { 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') }, @@ -190,11 +194,27 @@ sub init_languages_by { 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 { @@ -211,20 +231,6 @@ sub init_contacts_by { return $cby; } -sub init_departments_by { - my ($self) = @_; - - my $all_departments = SL::DB::Manager::Department->get_all; - return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $all_departments } } ) } qw(id description) }; -} - -sub init_projects_by { - my ($self) = @_; - - my $all_projects = SL::DB::Manager::Project->get_all; - return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $all_projects } } ) } qw(id projectnumber description) }; -} - sub init_ct_shiptos_by { my ($self) = @_; @@ -251,6 +257,13 @@ sub init_pricegroups_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) = @_; @@ -258,20 +271,32 @@ sub check_objects { 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 @@ -291,21 +316,6 @@ sub check_objects { $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 { @@ -314,10 +324,10 @@ 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 { @@ -336,46 +346,16 @@ sub handle_order { 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); $self->handle_employee($entry); } -# ToDo: salesman by name -sub handle_salesman { - my ($self, $entry) = @_; - - my $object = $entry->{object}; - my $vc_obj; - $vc_obj = SL::DB::Customer->new(id => $object->customer_id)->load if $object->customer_id; - $vc_obj = SL::DB::Vendor->new(id => $object->vendor_id)->load if (!$vc_obj && $object->vendor_id); - - # salesman from customer/vendor or login if not given - if (!$object->salesman) { - if ($vc_obj && $vc_obj->salesman_id) { - $object->salesman(SL::DB::Manager::Employee->find_by(id => $vc_obj->salesman_id)); - } else { - $object->salesman(SL::DB::Manager::Employee->find_by(login => $::myconfig{login})); - } - } -} - -# ToDo: employee by name -sub handle_employee { - my ($self, $entry) = @_; - - my $object = $entry->{object}; - - # employee from login if not given - if (!$object->employee_id) { - $object->employee_id(SL::DB::Manager::Employee->find_by(login => $::myconfig{login})->id); - } -} - sub check_language { my ($self, $entry) = @_; @@ -408,33 +388,125 @@ sub check_language { } 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 }) { @@ -461,13 +533,41 @@ sub check_part { 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; } @@ -506,71 +606,6 @@ sub check_contact { return 1; } -sub check_department { - my ($self, $entry) = @_; - - my $object = $entry->{object}; - - # Check whether or not department ID is valid. - if ($object->department_id && !$self->departments_by->{id}->{ $object->department_id }) { - push @{ $entry->{errors} }, $::locale->text('Error: Invalid department'); - return 0; - } - - # Map description to ID if given. - if (!$object->department_id && $entry->{raw_data}->{department}) { - my $dep = $self->departments_by->{description}->{ $entry->{raw_data}->{department} }; - if (!$dep) { - push @{ $entry->{errors} }, $::locale->text('Error: Invalid department'); - return 0; - } - - $object->department_id($dep->id); - } - - return 1; -} - -sub check_project { - my ($self, $entry, %params) = @_; - - my $id_column = ($params{global} ? 'global' : '') . 'project_id'; - my $number_column = ($params{global} ? 'global' : '') . 'projectnumber'; - my $description_column = ($params{global} ? 'global' : '') . 'project'; - - my $object = $entry->{object}; - - # Check whether or not projetc ID is valid. - if ($object->$id_column && !$self->projects_by->{id}->{ $object->$id_column }) { - push @{ $entry->{errors} }, $::locale->text('Error: Invalid project'); - return 0; - } - - # Map number to ID if given. - if (!$object->$id_column && $entry->{raw_data}->{$number_column}) { - my $proj = $self->projects_by->{projectnumber}->{ $entry->{raw_data}->{$number_column} }; - if (!$proj) { - push @{ $entry->{errors} }, $::locale->text('Error: Invalid project'); - return 0; - } - - $object->$id_column($proj->id); - } - - # Map description to ID if given. - if (!$object->$id_column && $entry->{raw_data}->{$description_column}) { - my $proj = $self->projects_by->{description}->{ $entry->{raw_data}->{$description_column} }; - if (!$proj) { - push @{ $entry->{errors} }, $::locale->text('Error: Invalid project'); - return 0; - } - - $object->$id_column($proj->id); - } - - return 1; -} - sub check_ct_shipto { my ($self, $entry) = @_; @@ -725,22 +760,16 @@ sub handle_prices_and_taxes() { 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 {