+sub check_ct_shipto {
+ my ($self, $entry) = @_;
+
+ 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->{'trans_id+shipto_id'}->{ $trans_id . '+' . $object->shipto_id }) {
+ push @{ $entry->{errors} }, $::locale->text('Error: Invalid shipto');
+ 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 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 check_currency {
+ my ($self, $entry) = @_;
+
+ my $object = $entry->{object};
+
+ # Check whether or not currency ID is valid.
+ if ($object->currency_id && !$self->currencies_by->{id}->{ $object->currency_id }) {
+ push @{ $entry->{errors} }, $::locale->text('Error: Invalid currency');
+ return 0;
+ }
+
+ # Map name to ID if given.
+ if (!$object->currency_id && $entry->{raw_data}->{currency}) {
+ my $currency = $self->currencies_by->{name}->{ $entry->{raw_data}->{currency} };
+ if (!$currency) {
+ push @{ $entry->{errors} }, $::locale->text('Error: Invalid currency');
+ return 0;
+ }
+
+ $object->currency_id($currency->id);
+ }
+
+ return 1;
+}
+
+sub add_items_to_order {
+ my ($self) = @_;
+
+ # add orderitems to order
+ my $order_entry;
+ my @orderitems;
+ foreach my $entry (@{ $self->controller->data }) {
+ # search first order
+ if ($entry->{raw_data}->{datatype} eq $self->_order_column) {
+
+ # new order entry: add collected orderitems to the previous one
+ if (defined $order_entry) {
+ $order_entry->{object}->orderitems(@orderitems);
+ @orderitems = ();
+ }
+
+ $order_entry = $entry;
+
+ } elsif ( defined $order_entry && $entry->{raw_data}->{datatype} eq $self->_item_column ) {
+ # collect orderitems to add to order (if they have no errors)
+ # ( add_orderitems does not work here if we want to call
+ # calculate_prices_and_taxes afterwards ...
+ # so collect orderitems and add them at once )
+ push @orderitems, $entry->{object} if (scalar @{ $entry->{errors} } == 0);
+ }
+ }
+ # add last collected orderitems to last order
+ $order_entry->{object}->orderitems(@orderitems) if $order_entry;
+}
+
+sub handle_prices_and_taxes() {
+ my ($self) = @_;
+
+ # calculate prices and taxes
+ foreach my $entry (@{ $self->controller->data }) {
+ next if @{ $entry->{errors} };
+
+ if ($entry->{raw_data}->{datatype} eq $self->_order_column) {
+
+ $entry->{object}->calculate_prices_and_taxes;
+
+ $entry->{info_data}->{calc_amount} = $entry->{object}->amount_as_number;
+ $entry->{info_data}->{calc_netamount} = $entry->{object}->netamount_as_number;
+ }
+ }
+
+ # 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 @to_verify = ( { column => 'amount',
+ raw_column => 'verify_amount',
+ info_header => 'Calc. Amount',
+ info_method => 'calc_amount',
+ err_msg => 'Amounts differ too much',
+ },
+ { column => 'netamount',
+ raw_column => 'verify_netamount',
+ info_header => 'Calc. Net amount',
+ info_method => 'calc_netamount',
+ err_msg => 'Net amounts differ too much',
+ } );
+
+ 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->_order_column, $tv->{raw_column});
+ $self->add_info_columns($self->_order_column,
+ { header => $::locale->text($tv->{info_header}), method => $tv->{info_method} });
+ }
+
+ # check differences
+ foreach my $entry (@{ $self->controller->data }) {
+ next if @{ $entry->{errors} };
+ if ($entry->{raw_data}->{datatype} eq $self->_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) > $self->settings->{'max_amount_diff'}) {
+ push @{ $entry->{errors} }, $::locale->text($tv->{err_msg});
+ }
+ }
+ }
+ }
+
+}
+