Kosmetik: Leerzeichen am Zeilenende entfernt.
[kivitendo-erp.git] / SL / Controller / CsvImport / Order.pm
index c2c78c4..8eebb28 100644 (file)
@@ -12,8 +12,11 @@ use SL::DB::Part;
 use SL::DB::PaymentTerm;
 use SL::DB::Contact;
 use SL::DB::Department;
+use SL::DB::PriceFactor;
+use SL::DB::Pricegroup;
 use SL::DB::Project;
 use SL::DB::Shipto;
+use SL::DB::TaxZone;
 use SL::TransNumber;
 
 use parent qw(SL::Controller::CsvImport::BaseMulti);
@@ -21,7 +24,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 all_departments departments_by all_projects projects_by all_ct_shiptos ct_shiptos_by) ],
+ 'scalar --get_set_init' => [ qw(settings languages_by parts_by contacts_by departments_by projects_by ct_shiptos_by taxzones_by price_factors_by pricegroups_by) ],
 );
 
 
@@ -34,7 +37,7 @@ sub init_class {
 sub init_settings {
   my ($self) = @_;
 
-  return { map { ( $_ => $self->controller->profile->get($_) ) } qw(order_column item_column) };
+  return { map { ( $_ => $self->controller->profile->get($_) ) } qw(order_column item_column max_amount_diff) };
 }
 
 
@@ -43,14 +46,25 @@ sub init_profile {
 
   my $profile = $self->SUPER::init_profile;
 
+  # SUPER::init_profile sets row_ident to the class name
+  # overwrite it with the user specified settings
   foreach my $p (@{ $profile }) {
-    my $prof = $p->{profile};
     if ($p->{row_ident} eq 'Order') {
+      $p->{row_ident} = $self->settings->{'order_column'};
+    }
+    if ($p->{row_ident} eq 'OrderItem') {
+      $p->{row_ident} = $self->settings->{'item_column'};
+    }
+  }
+
+  foreach my $p (@{ $profile }) {
+    my $prof = $p->{profile};
+    if ($p->{row_ident} eq $self->settings->{'order_column'}) {
       # no need to handle
       delete @{$prof}{qw(delivery_customer_id delivery_vendor_id proforma quotation amount netamount)};
-      # handable, but not handled by now
     }
-    if ($p->{row_ident} eq 'OrderItem') {
+    if ($p->{row_ident} eq $self->settings->{'item_column'}) {
+      # no need to handle
       delete @{$prof}{qw(trans_id)};
     }
   }
@@ -64,8 +78,25 @@ sub setup_displayable_columns {
 
   $self->SUPER::setup_displayable_columns;
 
-  $self->add_displayable_columns('Order',
+  $self->add_displayable_columns($self->settings->{'order_column'},
                                  { name => 'datatype',         description => $::locale->text('Zeilenkennung')                  },
+                                 { name => 'closed',           description => $::locale->text('Closed')                         },
+                                 { name => 'curr',             description => $::locale->text('Currency')                       },
+                                 { name => 'cusordnumber',     description => $::locale->text('Customer Order Number')          },
+                                 { name => 'delivered',        description => $::locale->text('Delivered')                      },
+                                 { name => 'employee_id',      description => $::locale->text('Employee (database ID)')         },
+                                 { name => 'intnotes',         description => $::locale->text('Internal Notes')                 },
+                                 { name => 'marge_percent',    description => $::locale->text('Margepercent')                   },
+                                 { name => 'marge_total',      description => $::locale->text('Margetotal')                     },
+                                 { name => 'notes',            description => $::locale->text('Notes')                          },
+                                 { name => 'ordnumber',        description => $::locale->text('Order Number')                   },
+                                 { name => 'quonumber',        description => $::locale->text('Quotation Number')               },
+                                 { name => 'reqdate',          description => $::locale->text('Reqdate')                        },
+                                 { name => 'salesman_id',      description => $::locale->text('Salesman (database ID)')         },
+                                 { name => 'shippingpoint',    description => $::locale->text('Shipping Point')                 },
+                                 { name => 'shipvia',          description => $::locale->text('Ship via')                       },
+                                 { name => 'transaction_description', description => $::locale->text('Transaction description') },
+                                 { name => 'transdate',        description => $::locale->text('Order Date')                     },
                                  { name => 'verify_amount',    description => $::locale->text('Amount (for verification)')      },
                                  { name => 'verify_netamount', description => $::locale->text('Net amount (for verification)')  },
                                  { name => 'taxincluded',      description => $::locale->text('Tax Included')                   },
@@ -79,7 +110,8 @@ 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 => '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)')       },
@@ -90,9 +122,31 @@ sub setup_displayable_columns {
                                  { 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')                 },
+  $self->add_displayable_columns($self->settings->{'item_column'},
+                                 { name => 'datatype',        description => $::locale->text('Zeilenkennung')              },
+                                 { name => 'cusordnumber',    description => $::locale->text('Customer Order Number')      },
+                                 { name => 'description',     description => $::locale->text('Description')                },
+                                 { name => 'discount',        description => $::locale->text('Discount')                   },
+                                 { name => 'lastcost',        description => $::locale->text('Lastcost')                   },
+                                 { name => 'longdescription', description => $::locale->text('Long Description')           },
+                                 { name => 'marge_percent',   description => $::locale->text('Margepercent')               },
+                                 { name => 'marge_total',     description => $::locale->text('Margetotal')                 },
+                                 { name => 'ordnumber',       description => $::locale->text('Order 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)')      },
+                                 { name => 'price_factor_id', description => $::locale->text('Price factor (database ID)') },
+                                 { name => 'price_factor',    description => $::locale->text('Price factor (name)')        },
+                                 { name => 'pricegroup_id',   description => $::locale->text('Price group (database ID)')  },
+                                 { name => 'pricegroup',      description => $::locale->text('Price group (name)')         },
+                                 { name => 'qty',             description => $::locale->text('Quantity')                   },
+                                 { name => 'reqdate',         description => $::locale->text('Reqdate')                    },
+                                 { name => 'sellprice',       description => $::locale->text('Sellprice')                  },
+                                 { name => 'serialnumber',    description => $::locale->text('Serial No.')                 },
+                                 { name => 'subtotal',        description => $::locale->text('Subtotal')                   },
+                                 { name => 'unit',            description => $::locale->text('Unit')                       },
                                 );
 }
 
@@ -103,74 +157,72 @@ 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) = @_;
 
-  return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $self->all_parts } } ) } qw(id partnumber ean description) };
-}
-
-sub init_all_contacts {
-  my ($self) = @_;
-
-  return SL::DB::Manager::Contact->get_all;
+  my $all_parts = SL::DB::Manager::Part->get_all;
+  return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $all_parts } } ) } qw(id partnumber ean description) };
 }
 
 sub init_contacts_by {
   my ($self) = @_;
 
-  my $cby = { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $self->all_contacts } } ) } qw(cp_id cp_name) };
+  my $all_contacts = SL::DB::Manager::Contact->get_all;
 
+  my $cby;
   # by customer/vendor id  _and_  contact person id
-  $cby->{'cp_cv_id+cp_id'} = { map { ( $_->cp_cv_id . '+' . $_->cp_id => $_ ) } @{ $self->all_contacts } };
+  $cby->{'cp_cv_id+cp_id'}   = { map { ( $_->cp_cv_id . '+' . $_->cp_id   => $_ ) } @{ $all_contacts } };
+  # by customer/vendor id  _and_  contact person name
+  $cby->{'cp_cv_id+cp_name'} = { map { ( $_->cp_cv_id . '+' . $_->cp_name => $_ ) } @{ $all_contacts } };
 
   return $cby;
 }
 
-sub init_all_departments {
+sub init_departments_by {
   my ($self) = @_;
 
-  return SL::DB::Manager::Department->get_all;
+  my $all_departments = SL::DB::Manager::Department->get_all;
+  return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $all_departments } } ) } qw(id description) };
 }
 
-sub init_departments_by {
+sub init_projects_by {
   my ($self) = @_;
 
-  return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $self->all_departments } } ) } qw(id description) };
+  my $all_projects = SL::DB::Manager::Project->get_all;
+  return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $all_projects } } ) } qw(id projectnumber description) };
 }
 
-sub init_all_projects {
+sub init_ct_shiptos_by {
   my ($self) = @_;
 
-  return SL::DB::Manager::Project->get_all;
-}
+  my $all_ct_shiptos = SL::DB::Manager::Shipto->get_all(query => [module => 'CT']);
 
-sub init_projects_by {
-  my ($self) = @_;
+  my $sby;
+  # by trans_id  _and_  shipto_id
+  $sby->{'trans_id+shipto_id'} = { map { ( $_->trans_id . '+' . $_->shipto_id => $_ ) } @{ $all_ct_shiptos } };
 
-  return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $self->all_projects } } ) } qw(id projectnumber description) };
+  return $sby;
 }
 
-sub init_all_ct_shiptos {
+sub init_taxzones_by {
   my ($self) = @_;
 
-  return SL::DB::Manager::Shipto->get_all(query => [module => 'CT']);
+  my $all_taxzones = SL::DB::Manager::TaxZone->get_all;
+  return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $all_taxzones } } ) } qw(id description) };
 }
 
-sub init_ct_shiptos_by {
+sub init_price_factors_by {
   my ($self) = @_;
 
-  my $sby = { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $self->all_ct_shiptos } } ) } qw(shipto_id) };
+  my $all_price_factors = SL::DB::Manager::PriceFactor->get_all;
+  return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $all_price_factors } } ) } qw(id description) };
+}
 
-  # by trans_id  _and_  shipto_id
-  $sby->{'trans_id+shipto_id'} = { map { ( $_->trans_id . '+' . $_->shipto_id => $_ ) } @{ $self->all_ct_shiptos } };
+sub init_pricegroups_by {
+  my ($self) = @_;
 
-  return $sby;
+  my $all_pricegroups = SL::DB::Manager::Pricegroup->get_all;
+  return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $all_pricegroups } } ) } qw(id pricegroup) };
 }
 
 sub check_objects {
@@ -202,6 +254,7 @@ sub check_objects {
       $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
@@ -230,9 +283,11 @@ 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(payment language department globalproject));
+                     map { "${_}_id" } grep { exists $self->controller->data->[0]->{raw_data}->{$_} } qw(payment language department globalproject taxzone cp));
   $self->add_columns($self->settings->{'order_column'}, 'globalproject_id') if exists $self->controller->data->[0]->{raw_data}->{globalprojectnumber};
+  $self->add_columns($self->settings->{'order_column'}, 'cp_id')            if exists $self->controller->data->[0]->{raw_data}->{contact};
 
   foreach my $entry (@{ $self->controller->data }) {
     if ($entry->{raw_data}->{datatype} eq $self->settings->{'item_column'} && $entry->{object}->can('part')) {
@@ -249,11 +304,19 @@ 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->check_price_factor($entry);
+      $self->check_pricegroup($entry);
     }
   }
 
   $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 price_factor pricegroup));
+  $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;
@@ -300,7 +363,6 @@ sub check_objects {
 
   # If amounts are given, show calculated amounts as info and given amounts (verify_xxx).
   # And throw an error if the differences are too big.
-  my $max_diff = 0.02;
   my @to_verify = ( { column      => 'amount',
                       raw_column  => 'verify_amount',
                       info_header => 'Calc. Amount',
@@ -315,6 +377,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'},
@@ -327,7 +390,7 @@ sub check_objects {
       if ($entry->{raw_data}->{datatype} eq $self->settings->{'order_column'}) {
         next if !$entry->{raw_data}->{ $tv->{raw_column} };
         my $parsed_value = $::form->parse_amount(\%::myconfig, $entry->{raw_data}->{ $tv->{raw_column} });
-        if (abs($entry->{object}->${ \$tv->{column} } - $parsed_value) > $max_diff) {
+        if (abs($entry->{object}->${ \$tv->{column} } - $parsed_value) > $self->settings->{'max_amount_diff'}) {
           push @{ $entry->{errors} }, $::locale->text($tv->{err_msg});
         }
       }
@@ -418,16 +481,18 @@ sub check_contact {
 
   my $object = $entry->{object};
 
+  my $cp_cv_id = $object->customer_id || $object->vendor_id;
+  return 0 unless $cp_cv_id;
+
   # Check wether or not contact ID is valid.
-  if ($object->cp_id && !$self->contacts_by->{cp_id}->{ $object->cp_id }) {
+  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;
   }
 
   # 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} };
+    my $cp = $self->contacts_by->{'cp_cv_id+cp_name'}->{ $cp_cv_id . '+' . $entry->{raw_data}->{contact} };
     if (!$cp) {
       push @{ $entry->{errors} }, $::locale->text('Error: Invalid contact');
       return 0;
@@ -436,16 +501,8 @@ sub check_contact {
     $object->cp_id($cp->cp_id);
   }
 
-  # Check if the contact belongs to 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;
-  }
-
   if ($object->cp_id) {
-    $entry->{info_data}->{contact} = $self->contacts_by->{cp_id}->{ $object->cp_id }->cp_name;
+    $entry->{info_data}->{contact} = $self->contacts_by->{'cp_cv_id+cp_id'}->{ $cp_cv_id . '+' . $object->cp_id }->cp_name;
   }
 
   return 1;
@@ -521,23 +578,93 @@ sub check_ct_shipto {
 
   my $object = $entry->{object};
 
+  my $trans_id = $object->customer_id || $object->vendor_id;
+  return 0 unless $trans_id;
+
   # Check wether or not shipto ID is valid.
-  if ($object->shipto_id && !$self->ct_shiptos_by->{shipto_id}->{ $object->shipto_id }) {
+  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;
   }
 
-  # 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 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 check_price_factor {
+  my ($self, $entry) = @_;
+
+  my $object = $entry->{object};
+
+  # Check wether 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;
   }
 
+  # Map description to ID if given.
+  if (!$object->price_factor_id && $entry->{raw_data}->{price_factor}) {
+    my $price_factor = $self->price_factors_by->{description}->{ $entry->{raw_data}->{price_factor} };
+    if (!$price_factor) {
+      push @{ $entry->{errors} }, $::locale->text('Error: Invalid price factor');
+      return 0;
+    }
+
+    $object->price_factor_id($price_factor->id);
+  }
+
   return 1;
 }
 
+sub check_pricegroup {
+  my ($self, $entry) = @_;
+
+  my $object = $entry->{object};
+
+  # Check wether 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;
+  }
+
+  # Map pricegroup to ID if given.
+  if (!$object->pricegroup_id && $entry->{raw_data}->{pricegroup}) {
+    my $pricegroup = $self->pricegroups_by->{pricegroup}->{ $entry->{raw_data}->{pricegroup} };
+    if (!$pricegroup) {
+      push @{ $entry->{errors} }, $::locale->text('Error: Invalid price group');
+      return 0;
+    }
+
+    $object->pricegroup_id($pricegroup->id);
+  }
+
+  return 1;
+}
 
 
 sub save_objects {