X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FController%2FCsvImport%2FOrder.pm;h=c3c5c5c87d788246a93a724d839f15d4d95ea037;hb=320664922d387971c50ff1070abc2b73050a024f;hp=f8dbbb1cb141e52c6f54866602b24d18206c6a51;hpb=cd8b56f2ec945264132d443ad8f412699d1a8a1b;p=kivitendo-erp.git diff --git a/SL/Controller/CsvImport/Order.pm b/SL/Controller/CsvImport/Order.pm index f8dbbb1cb..c3c5c5c87 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,6 +18,7 @@ use SL::DB::Pricegroup; use SL::DB::Project; use SL::DB::Shipto; use SL::DB::TaxZone; +use SL::DB::Unit; use SL::TransNumber; use parent qw(SL::Controller::CsvImport::BaseMulti); @@ -25,7 +26,7 @@ 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 parts_by contacts_by ct_shiptos_by price_factors_by pricegroups_by units_by) ], ); @@ -211,20 +212,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 +238,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) = @_; @@ -261,6 +255,8 @@ sub check_objects { 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); } elsif ($entry->{raw_data}->{datatype} eq $self->_item_column && $entry->{object}->can('part')) { @@ -272,6 +268,11 @@ sub check_objects { $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 +292,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 { @@ -336,7 +322,7 @@ 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->$_; } } @@ -345,37 +331,6 @@ sub handle_order { $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) = @_; @@ -415,11 +370,12 @@ sub handle_item { my $part_obj = SL::DB::Part->new(id => $object->parts_id)->load; + $self->handle_unit($entry); + $self->handle_sellprice($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 @@ -431,12 +387,54 @@ sub handle_item { $self->check_pricegroup($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) = @_; + + my $object = $entry->{object}; + + # Set sellprice from part if not given. Convert with respect to unit. + if (!defined $object->sellprice) { + my $sellprice = $object->part->sellprice; + + if ($object->unit ne $object->part->unit) { + $sellprice = $object->unit_obj->convert_to($sellprice, $object->part->unit_obj); + } + $object->sellprice($sellprice); + } +} + sub check_part { my ($self, $entry) = @_; my $object = $entry->{object}; - # Check wether or not part ID is valid. + # Check whether or not part ID is valid. if ($object->parts_id && !$self->parts_by->{id}->{ $object->parts_id }) { push @{ $entry->{errors} }, $::locale->text('Error: Invalid part'); return 0; @@ -471,6 +469,11 @@ sub check_part { return 0; } + if ($self->parts_by->{id}->{ $object->parts_id }->obsolete) { + push @{ $entry->{errors} }, $::locale->text('Error: Part is obsolete'); + return 0; + } + return 1; } @@ -482,7 +485,7 @@ sub check_contact { my $cp_cv_id = $object->customer_id || $object->vendor_id; return 0 unless $cp_cv_id; - # Check wether or not contact ID is valid. + # Check whether or not contact ID is valid. if ($object->cp_id && !$self->contacts_by->{'cp_cv_id+cp_id'}->{ $cp_cv_id . '+' . $object->cp_id }) { push @{ $entry->{errors} }, $::locale->text('Error: Invalid contact'); return 0; @@ -506,71 +509,6 @@ sub check_contact { return 1; } -sub check_department { - my ($self, $entry) = @_; - - my $object = $entry->{object}; - - # Check wether 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 wether 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) = @_; @@ -579,7 +517,7 @@ sub check_ct_shipto { my $trans_id = $object->customer_id || $object->vendor_id; return 0 unless $trans_id; - # Check wether or not shipto ID is valid. + # Check whether or not shipto ID is valid. if ($object->shipto_id && !$self->ct_shiptos_by->{'trans_id+shipto_id'}->{ $trans_id . '+' . $object->shipto_id }) { push @{ $entry->{errors} }, $::locale->text('Error: Invalid shipto'); return 0; @@ -593,7 +531,7 @@ sub check_price_factor { my $object = $entry->{object}; - # Check wether or not price_factor ID is valid. + # Check whether or not price_factor ID is valid. if ($object->price_factor_id && !$self->price_factors_by->{id}->{ $object->price_factor_id }) { push @{ $entry->{errors} }, $::locale->text('Error: Invalid price factor'); return 0; @@ -618,7 +556,7 @@ sub check_pricegroup { my $object = $entry->{object}; - # Check wether or not pricegroup ID is valid. + # Check whether or not pricegroup ID is valid. if ($object->pricegroup_id && !$self->pricegroups_by->{id}->{ $object->pricegroup_id }) { push @{ $entry->{errors} }, $::locale->text('Error: Invalid price group'); return 0; @@ -725,22 +663,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 {