use List::MoreUtils qw(pairwise);
use SL::Helper::Csv;
+use SL::DB::Language;
+use SL::DB::PaymentTerm;
use parent qw(Rose::Object);
use Rose::Object::MakeMethods::Generic
(
scalar => [ qw(controller file csv) ],
- 'scalar --get_set_init' => [ qw(profile existing_objects class manager_class cvar_columns all_cvar_configs) ],
+ 'scalar --get_set_init' => [ qw(profile existing_objects class manager_class cvar_columns all_cvar_configs all_languages payment_terms_by) ],
);
sub run {
return [ map { "cvar_" . $_->name } (@{ $self->all_cvar_configs }) ];
}
+sub init_all_languages {
+ my ($self) = @_;
+
+ return SL::DB::Manager::Language->get_all;
+}
+
+sub init_payment_terms_by {
+ my ($self) = @_;
+
+ my $all_payment_terms = SL::DB::Manager::PaymentTerm->get_all;
+ return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $all_payment_terms } } ) } qw(id description) };
+}
+
sub handle_cvars {
my ($self, $entry) = @_;
sub check_duplicates {
}
+sub check_payment {
+ my ($self, $entry) = @_;
+
+ my $object = $entry->{object};
+
+ # Check whether or not payment ID is valid.
+ if ($object->payment_id && !$self->payment_terms_by->{id}->{ $object->payment_id }) {
+ push @{ $entry->{errors} }, $::locale->text('Error: Invalid payment terms');
+ return 0;
+ }
+
+ # Map name to ID if given.
+ if (!$object->payment_id && $entry->{raw_data}->{payment}) {
+ my $terms = $self->payment_terms_by->{description}->{ $entry->{raw_data}->{payment} };
+
+ if (!$terms) {
+ push @{ $entry->{errors} }, $::locale->text('Error: Invalid payment terms');
+ return 0;
+ }
+
+ $object->payment_id($terms->id);
+ }
+
+ return 1;
+}
+
sub save_objects {
my ($self, %params) = @_;
use strict;
use SL::Helper::Csv;
+use SL::DB::Business;
use SL::DB::CustomVariable;
use SL::DB::CustomVariableConfig;
+use SL::DB::PaymentTerm;
use parent qw(SL::Controller::CsvImport::Base);
use Rose::Object::MakeMethods::Generic
(
- 'scalar --get_set_init' => [ qw(table) ],
+ 'scalar --get_set_init' => [ qw(table languages_by businesses_by) ],
);
sub init_table {
return SL::DB::Manager::CustomVariableConfig->get_all(where => [ module => 'CT' ]);
}
+sub init_businesses_by {
+ my ($self) = @_;
+
+ return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ SL::DB::Manager::Business->get_all } } ) } qw(id description) };
+}
+
+sub init_languages_by {
+ my ($self) = @_;
+
+ return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $self->all_languages } } ) } qw(id description article_code) };
+}
+
sub check_objects {
my ($self) = @_;
foreach my $entry (@{ $self->controller->data }) {
my $object = $entry->{object};
- my $name = $object->name;
- $name =~ s/^\s+//;
- $name =~ s/\s+$//;
- if (!$name) {
- push @{ $entry->{errors} }, $::locale->text('Error: Name missing');
- next;
- }
+ next unless $self->check_name($entry);
+ next unless $self->check_language($entry);
+ next unless $self->check_business($entry);
+ next unless $self->check_payment($entry);
+ $self->handle_cvars($entry);
if ($vcs_by_number{ $object->$numbercolumn }) {
$entry->{object}->$numbercolumn('####');
} else {
$vcs_by_number{ $object->$numbercolumn } = $object;
}
-
- $self->handle_cvars($entry);
}
+ $self->add_columns(map { "${_}_id" } grep { exists $self->controller->data->[0]->{raw_data}->{$_} } qw(language business payment));
$self->add_cvar_raw_data_columns;
}
}
}
+sub check_name {
+ my ($self, $entry) = @_;
+
+ my $name = $entry->{object}->name;
+ $name =~ s/^\s+//;
+ $name =~ s/\s+$//;
+
+ return 1 if $name;
+
+ push @{ $entry->{errors} }, $::locale->text('Error: Name missing');
+ return 0;
+}
+
+sub check_language {
+ my ($self, $entry) = @_;
+
+ my $object = $entry->{object};
+
+ # Check whether or not language ID is valid.
+ if ($object->language_id && !$self->languages_by->{id}->{ $object->language_id }) {
+ push @{ $entry->{errors} }, $::locale->text('Error: Invalid language');
+ return 0;
+ }
+
+ # Map name to ID if given.
+ if (!$object->language_id && $entry->{raw_data}->{language}) {
+ my $language = $self->languages_by->{description}->{ $entry->{raw_data}->{language} }
+ || $self->languages_by->{article_code}->{ $entry->{raw_data}->{language} };
+
+ if (!$language) {
+ push @{ $entry->{errors} }, $::locale->text('Error: Invalid language');
+ return 0;
+ }
+
+ $object->language_id($language->id);
+ }
+
+ return 1;
+}
+
+sub check_business {
+ my ($self, $entry) = @_;
+
+ my $object = $entry->{object};
+
+ # Check whether or not business ID is valid.
+ if ($object->business_id && !$self->businesss_by->{id}->{ $object->business_id }) {
+ push @{ $entry->{errors} }, $::locale->text('Error: Invalid business');
+ return 0;
+ }
+
+ # Map name to ID if given.
+ if (!$object->business_id && $entry->{raw_data}->{business}) {
+ my $business = $self->businesses_by->{description}->{ $entry->{raw_data}->{business} };
+
+ if (!$business) {
+ push @{ $entry->{errors} }, $::locale->text('Error: Invalid business');
+ return 0;
+ }
+
+ $object->business_id($business->id);
+ }
+
+ return 1;
+}
+
sub save_objects {
my ($self, %params) = @_;
);
}
+sub init_profile {
+ my ($self) = @_;
+
+ my $profile = $self->SUPER::init_profile;
+ delete @{$profile}{qw(language business salesman payment)};
+
+ return $profile;
+}
+
# TODO:
-# Kundentyp
-# salesman_id
-# Sprache
-# Zahlungsbedingungen
+# salesman_id -- Kunden mit Typ 'Verkäufer', falls $::vertreter an ist, ansonsten Employees
1;
use SL::DB::Buchungsgruppe;
use SL::DB::CustomVariable;
use SL::DB::CustomVariableConfig;
-use SL::DB::Language;
use SL::DB::PartsGroup;
use SL::DB::PaymentTerm;
use SL::DB::PriceFactor;
use Rose::Object::MakeMethods::Generic
(
scalar => [ qw(table) ],
- 'scalar --get_set_init' => [ qw(bg_by settings parts_by price_factors_by units_by payment_terms_by packing_types_by partsgroups_by
- all_languages translation_columns) ],
+ 'scalar --get_set_init' => [ qw(bg_by settings parts_by price_factors_by units_by packing_types_by partsgroups_by
+ translation_columns) ],
);
sub init_class {
return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $all_price_factors } } ) } qw(id description) };
}
-sub init_payment_terms_by {
- my ($self) = @_;
-
- my $all_payment_terms = SL::DB::Manager::PaymentTerm->get_all;
- return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $all_payment_terms } } ) } qw(id description) };
-}
-
sub init_packing_types_by {
my ($self) = @_;
shoparticle_if_missing parts_type) };
}
-sub init_all_languages {
- my ($self) = @_;
-
- return SL::DB::Manager::Language->get_all;
-}
-
sub init_all_cvar_configs {
my ($self) = @_;
return 1;
}
-sub check_payment {
- my ($self, $entry) = @_;
-
- my $object = $entry->{object};
-
- # Check whether or not payment ID is valid.
- if ($object->payment_id && !$self->payment_terms_by->{id}->{ $object->payment_id }) {
- push @{ $entry->{errors} }, $::locale->text('Error: Invalid payment terms');
- return 0;
- }
-
- # Map name to ID if given.
- if (!$object->payment_id && $entry->{raw_data}->{payment}) {
- my $terms = $self->payment_terms_by->{description}->{ $entry->{raw_data}->{payment} };
-
- if (!$terms) {
- push @{ $entry->{errors} }, $::locale->text('Error: Invalid payment terms');
- return 0;
- }
-
- $object->payment_id($terms->id);
- }
-
- return 1;
-}
-
sub check_packing_type {
my ($self, $entry) = @_;