1 package SL::DB::CsvImportProfile;
5 use List::Util qw(first);
7 use SL::DB::MetaSetup::CsvImportProfile;
9 __PACKAGE__->meta->add_relationship(
11 type => 'one to many',
12 class => 'SL::DB::CsvImportProfileSetting',
13 column_map => { id => 'csv_import_profile_id' },
17 __PACKAGE__->meta->initialize;
19 __PACKAGE__->meta->make_manager_class;
21 __PACKAGE__->before_save('_before_save_unset_default_on_others');
27 sub new_with_default {
28 my ($class, $type) = @_;
30 return $class->new(type => $type)->set_defaults;
36 $self->_set_defaults(sep_char => ',',
40 numberformat => $::myconfig{numberformat},
41 duplicates => 'no_check',
44 if ($self->type eq 'parts') {
45 my $bugru = SL::DB::Manager::Buchungsgruppe->find_by(description => { like => 'Standard%19%' });
47 $self->_set_defaults(sellprice_places => 2,
48 sellprice_adjustment => 0,
49 sellprice_adjustment_type => 'percent',
50 article_number_policy => 'update_prices',
51 shoparticle_if_missing => '0',
53 default_buchungsgruppe => ($bugru ? $bugru->id : undef),
54 apply_buchungsgruppe => 'all',
57 $self->_set_defaults(table => 'customer');
64 my ($self, %params) = @_;
66 while (my ($key, $value) = each %params) {
67 my $setting = $self->_get_setting($key);
70 $setting = SL::DB::CsvImportProfileSetting->new(key => $key);
71 $self->settings(@{ $self->settings || [] }, $setting);
74 $setting->value($value);
81 my ($self, $key, $default) = @_;
83 my $setting = $self->_get_setting($key);
84 return $setting ? $setting->value : $default;
88 my ($self, %params) = @_;
90 while (my ($key, $value) = each %params) {
91 $self->settings(@{ $self->settings || [] }, { key => $key, value => $value }) if !$self->_get_setting($key);
101 sub _before_save_unset_default_on_others {
104 if ($self->is_default) {
105 SL::DB::Manager::CsvImportProfile->update_all(set => { is_default => 0 },
106 where => [ type => $self->type,
107 '!id' => $self->id ]);
118 my ($self, $key) = @_;
119 return first { $_->key eq $key } @{ $self->settings || [] };