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',
58 $self->_set_defaults(table => 'customer');
65 my ($self, %params) = @_;
67 while (my ($key, $value) = each %params) {
68 my $setting = $self->_get_setting($key);
71 $setting = SL::DB::CsvImportProfileSetting->new(key => $key);
72 $self->settings(@{ $self->settings || [] }, $setting);
75 $setting->value($value);
82 my ($self, $key, $default) = @_;
84 my $setting = $self->_get_setting($key);
85 return $setting ? $setting->value : $default;
89 my ($self, %params) = @_;
91 while (my ($key, $value) = each %params) {
92 $self->settings(@{ $self->settings || [] }, { key => $key, value => $value }) if !$self->_get_setting($key);
98 sub clone_and_reset_deep {
101 my $clone = $self->clone_and_reset;
102 $clone->settings(map { $_->clone_and_reset } $self->settings);
120 sub _before_save_unset_default_on_others {
123 if ($self->is_default) {
124 SL::DB::Manager::CsvImportProfile->update_all(set => { is_default => 0 },
125 where => [ type => $self->type,
126 '!id' => $self->id ]);
137 my ($self, $key) = @_;
138 return first { $_->key eq $key } @{ $self->settings || [] };