X-Git-Url: http://wagnertech.de/gitweb/gitweb.cgi/mfinanz.git/blobdiff_plain/69f73331f02259ae3c819a6726881b8409e1f1b9..1d78de409b3536e1dba613700eb8ec5ffa531567:/SL/Controller/CsvImport/Order.pm diff --git a/SL/Controller/CsvImport/Order.pm b/SL/Controller/CsvImport/Order.pm index 689a6bb77..2cd1b42af 100644 --- a/SL/Controller/CsvImport/Order.pm +++ b/SL/Controller/CsvImport/Order.pm @@ -11,6 +11,10 @@ use SL::DB::OrderItem; use SL::DB::Part; use SL::DB::PaymentTerm; use SL::DB::Contact; +use SL::DB::Department; +use SL::DB::Project; +use SL::DB::Shipto; +use SL::DB::TaxZone; use SL::TransNumber; use parent qw(SL::Controller::CsvImport::BaseMulti); @@ -18,7 +22,7 @@ use parent qw(SL::Controller::CsvImport::BaseMulti); use Rose::Object::MakeMethods::Generic ( - 'scalar --get_set_init' => [ qw(settings languages_by all_parts parts_by all_contacts contacts_by) ], + 'scalar --get_set_init' => [ qw(settings languages_by all_parts parts_by all_contacts contacts_by all_departments departments_by all_projects projects_by all_ct_shiptos ct_shiptos_by all_taxzones taxzones_by) ], ); @@ -76,14 +80,24 @@ sub setup_displayable_columns { { name => 'language', description => $::locale->text('Language (name)') }, { name => 'payment_id', description => $::locale->text('Payment terms (database ID)') }, { name => 'payment', description => $::locale->text('Payment terms (name)') }, - { name => 'taxzone_id', description => $::locale->text('Steuersatz') }, - { name => 'contact_id', description => $::locale->text('Contact Person (database ID)') }, + { name => 'taxzone_id', description => $::locale->text('Steuersatz (database ID') }, + { name => 'taxzone', description => $::locale->text('Steuersatz (description)') }, + { name => 'cp_id', description => $::locale->text('Contact Person (database ID)') }, { name => 'contact', description => $::locale->text('Contact Person (name)') }, + { name => 'department_id', description => $::locale->text('Department (database ID)') }, + { name => 'department', description => $::locale->text('Department (description)') }, + { name => 'globalproject_id', description => $::locale->text('Document Project (database ID)') }, + { name => 'globalprojectnumber', description => $::locale->text('Document Project (number)') }, + { name => 'globalproject', description => $::locale->text('Document Project (description)') }, + { name => 'shipto_id', description => $::locale->text('Ship to (database ID)') }, ); $self->add_displayable_columns('OrderItem', - { name => 'parts_id', description => $::locale->text('Part (database ID)') }, - { name => 'partnumber', description => $::locale->text('Part Number') }, + { name => 'parts_id', description => $::locale->text('Part (database ID)') }, + { name => 'partnumber', description => $::locale->text('Part Number') }, + { name => 'project_id', description => $::locale->text('Project (database ID)') }, + { name => 'projectnumber', description => $::locale->text('Project (number)') }, + { name => 'project', description => $::locale->text('Project (description)') }, ); } @@ -123,6 +137,59 @@ sub init_contacts_by { return $cby; } +sub init_all_departments { + my ($self) = @_; + + return SL::DB::Manager::Department->get_all; +} + +sub init_departments_by { + my ($self) = @_; + + return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $self->all_departments } } ) } qw(id description) }; +} + +sub init_all_projects { + my ($self) = @_; + + return SL::DB::Manager::Project->get_all; +} + +sub init_projects_by { + my ($self) = @_; + + return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $self->all_projects } } ) } qw(id projectnumber description) }; +} + +sub init_all_ct_shiptos { + my ($self) = @_; + + return SL::DB::Manager::Shipto->get_all(query => [module => 'CT']); +} + +sub init_ct_shiptos_by { + my ($self) = @_; + + my $sby = { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $self->all_ct_shiptos } } ) } qw(shipto_id) }; + + # by trans_id _and_ shipto_id + $sby->{'trans_id+shipto_id'} = { map { ( $_->trans_id . '+' . $_->shipto_id => $_ ) } @{ $self->all_ct_shiptos } }; + + return $sby; +} + +sub init_all_taxzones { + my ($self) = @_; + + return SL::DB::Manager::TaxZone->get_all; +} + +sub init_taxzones_by { + my ($self) = @_; + + return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $self->all_taxzones } } ) } qw(id description) }; +} + sub check_objects { my ($self) = @_; @@ -149,6 +216,10 @@ sub check_objects { $self->check_contact($entry); $self->check_language($entry); $self->check_payment($entry); + $self->check_department($entry); + $self->check_project($entry, global => 1); + $self->check_ct_shipto($entry); + $self->check_taxzone($entry); if ($vc_obj) { # copy from customer if not given @@ -177,9 +248,10 @@ sub check_objects { $self->add_info_columns($self->settings->{'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_columns($self->settings->{'order_column'}, - map { "${_}_id" } grep { exists $self->controller->data->[0]->{raw_data}->{$_} } qw(business payment)); - + map { "${_}_id" } grep { exists $self->controller->data->[0]->{raw_data}->{$_} } qw(payment language department globalproject taxzone)); + $self->add_columns($self->settings->{'order_column'}, 'globalproject_id') if exists $self->controller->data->[0]->{raw_data}->{globalprojectnumber}; foreach my $entry (@{ $self->controller->data }) { if ($entry->{raw_data}->{datatype} eq $self->settings->{'item_column'} && $entry->{object}->can('part')) { @@ -196,11 +268,17 @@ sub check_objects { # set to 0 if not given $entry->{object}->discount(0) unless $entry->{object}->discount; $entry->{object}->ship(0) unless $entry->{object}->ship; + + $self->check_project($entry, global => 0); } } $self->add_info_columns($self->settings->{'item_column'}, { header => $::locale->text('Part Number'), method => 'partnumber' }); + # Todo: access via ->[1] ok? Better: search first item column and use this + $self->add_columns($self->settings->{'item_column'}, + map { "${_}_id" } grep { exists $self->controller->data->[1]->{raw_data}->{$_} } qw(project)); + $self->add_columns($self->settings->{'item_column'}, 'project_id') if exists $self->controller->data->[1]->{raw_data}->{projectnumber}; # add orderitems to order my $order_entry; @@ -262,6 +340,7 @@ sub check_objects { } ); foreach my $tv (@to_verify) { + # Todo: access via ->[0] ok? Better: search first order column and use this if (exists $self->controller->data->[0]->{raw_data}->{ $tv->{raw_column} }) { $self->add_raw_data_columns($self->settings->{'order_column'}, $tv->{raw_column}); $self->add_info_columns($self->settings->{'order_column'}, @@ -365,13 +444,14 @@ sub check_contact { my $object = $entry->{object}; - # Check wether or non contact ID is valid. + # Check wether or not contact ID is valid. if ($object->cp_id && !$self->contacts_by->{cp_id}->{ $object->cp_id }) { push @{ $entry->{errors} }, $::locale->text('Error: Invalid contact'); return 0; } - # Map number to ID if given. + # Map name to ID if given. + # Todo: names have not to be unique ... search all and check for matching customer/vendor? if (!$object->cp_id && $entry->{raw_data}->{contact}) { my $cp = $self->contacts_by->{cp_name}->{ $entry->{raw_data}->{contact} }; if (!$cp) { @@ -383,8 +463,10 @@ sub check_contact { } # Check if the contact belongs to this customer/vendor. - if ($object->cp_id && $object->customer_id && !$self->contacts_by->{'cp_cv_id+cp_id'}) { - push @{ $entry->{errors} }, $::locale->text('Error: Contact not found for this customer/vendor'); + my $trans_id = $object->customer_id || $object->vendor_id; + if ($object->cp_id && $trans_id + && !$self->contacts_by->{'cp_cv_id+cp_id'}->{ $trans_id . '+' . $object->cp_id }) { + push @{ $entry->{errors} }, $::locale->text('Error: Invalid contact this customer/vendor'); return 0; } @@ -395,6 +477,119 @@ 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) = @_; + + my $object = $entry->{object}; + + # Check wether or not shipto ID is valid. + if ($object->shipto_id && !$self->ct_shiptos_by->{shipto_id}->{ $object->shipto_id }) { + push @{ $entry->{errors} }, $::locale->text('Error: Invalid shipto'); + return 0; + } + + # Check if the shipto belongs to this customer/vendor. + my $trans_id = $object->customer_id || $object->vendor_id; + if ($object->shipto_id && $trans_id + && !$self->ct_shiptos_by->{'trans_id+shipto_id'}->{ $trans_id . '+' . $object->shipto_id } ) { + push @{ $entry->{errors} }, $::locale->text('Error: Invalid shipto for this customer/vendor'); + return 0; + } + + return 1; +} + +sub check_taxzone { + my ($self, $entry) = @_; + + my $object = $entry->{object}; + + # Check wether or not taxzone ID is valid. + if ($object->taxzone_id && !$self->taxzones_by->{id}->{ $object->taxzone_id }) { + push @{ $entry->{errors} }, $::locale->text('Error: Invalid taxzone'); + return 0; + } + + # Map description to ID if given. + if (!$object->taxzone_id && $entry->{raw_data}->{taxzone}) { + my $taxzone = $self->taxzones_by->{description}->{ $entry->{raw_data}->{taxzone} }; + if (!$taxzone) { + push @{ $entry->{errors} }, $::locale->text('Error: Invalid taxzone'); + return 0; + } + + $object->taxzone_id($taxzone->id); + } + + return 1; +} + + sub save_objects { my ($self, %params) = @_;