1 package SL::DB::CsvImportProfile;
5 use List::Util qw(first);
7 require SL::DB::MetaSetup::CsvImportProfile;
8 use Rose::DB::Object::Helpers qw(clone_and_reset);
10 __PACKAGE__->meta->add_relationship(
12 type => 'one to many',
13 class => 'SL::DB::CsvImportProfileSetting',
14 column_map => { id => 'csv_import_profile_id' },
18 __PACKAGE__->meta->initialize;
20 __PACKAGE__->meta->make_manager_class;
22 __PACKAGE__->before_save('_before_save_unset_default_on_others');
28 sub new_with_default {
29 my ($class, $type) = @_;
31 return $class->new(type => $type)->set_defaults;
37 $self->_set_defaults(sep_char => ',',
41 numberformat => $::myconfig{numberformat},
42 duplicates => 'no_check',
45 if ($self->type eq 'parts') {
46 my $bugru = SL::DB::Manager::Buchungsgruppe->find_by(description => { like => 'Standard%19%' });
48 $self->_set_defaults(sellprice_places => 2,
49 sellprice_adjustment => 0,
50 sellprice_adjustment_type => 'percent',
51 article_number_policy => 'update_prices',
52 shoparticle_if_missing => '0',
54 default_buchungsgruppe => ($bugru ? $bugru->id : undef),
55 apply_buchungsgruppe => 'all',
57 } elsif ($self->type eq 'orders') {
58 $self->_set_defaults(order_column => $::locale->text('Order'),
59 item_column => $::locale->text('OrderItem'),
60 max_amount_diff => 0.02,
63 $self->_set_defaults(table => 'customer');
70 my ($self, %params) = @_;
72 while (my ($key, $value) = each %params) {
73 my $setting = $self->_get_setting($key);
76 $setting = SL::DB::CsvImportProfileSetting->new(key => $key);
77 $self->settings(@{ $self->settings || [] }, $setting);
80 $setting->value($value);
87 my ($self, $key, $default) = @_;
89 my $setting = $self->_get_setting($key);
90 return $setting ? $setting->value : $default;
94 my ($self, %params) = @_;
96 while (my ($key, $value) = each %params) {
97 $self->settings(@{ $self->settings || [] }, { key => $key, value => $value }) if !$self->_get_setting($key);
103 sub clone_and_reset_deep {
106 my $clone = $self->clone_and_reset;
107 $clone->settings(map { $_->clone_and_reset } $self->settings);
125 sub _before_save_unset_default_on_others {
128 if ($self->is_default) {
129 SL::DB::Manager::CsvImportProfile->update_all(set => { is_default => 0 },
130 where => [ type => $self->type,
131 '!id' => $self->id ]);
142 my ($self, $key) = @_;
143 return first { $_->key eq $key } @{ $self->settings || [] };