Commit | Line | Data |
---|---|---|
3709451d TC |
1 | package BSE::ImportTargetProduct; |
2 | use strict; | |
3 | use base 'BSE::ImportTargetArticle'; | |
4 | use BSE::API qw(bse_make_product bse_make_catalog bse_add_image); | |
5 | use Articles; | |
6 | use Products; | |
0cca6ce6 TC |
7 | use BSE::TB::ProductOptions; |
8 | use BSE::TB::ProductOptionValues; | |
df2663f0 | 9 | use BSE::TB::PriceTiers; |
3709451d | 10 | |
df2663f0 | 11 | our $VERSION = "1.001"; |
cb7fd78d | 12 | |
3709451d TC |
13 | sub new { |
14 | my ($class, %opts) = @_; | |
15 | ||
16 | my $self = $class->SUPER::new(%opts); | |
17 | ||
18 | my $importer = delete $opts{importer}; | |
19 | ||
20 | $self->{price_dollar} = $importer->cfg_entry('price_dollar', 0); | |
21 | $self->{product_template} = $importer->cfg_entry('product_template'); | |
22 | $self->{catalog_template} = $importer->cfg_entry('catalog_template'); | |
0cca6ce6 TC |
23 | $self->{prodopt_value_sep} = $importer->cfg_entry("prodopt_separator", "|"); |
24 | $self->{reset_prodopts} = $importer->cfg_entry("reset_prodopts", 1); | |
3709451d | 25 | |
0cca6ce6 | 26 | my $map = $importer->maps; |
3709451d TC |
27 | defined $map->{retailPrice} |
28 | or die "No retailPrice mapping found\n"; | |
29 | ||
df2663f0 TC |
30 | $self->{price_tiers} = +{ map { $_->id => $_ } BSE::TB::PriceTiers->all }; |
31 | ||
3709451d TC |
32 | return $self; |
33 | } | |
34 | ||
35 | sub xform_entry { | |
36 | my ($self, $importer, $entry) = @_; | |
37 | ||
38 | $self->SUPER::xform_entry($importer, $entry); | |
39 | ||
40 | if ($self->{use_codes}) { | |
41 | $entry->{product_code} =~ /\S/ | |
42 | or die "product_code blank with use_codes\n"; | |
43 | } | |
44 | $entry->{retailPrice} =~ s/\$//; # in case | |
45 | ||
46 | if ($entry->{retailPrice} =~ /\d/) { | |
47 | $self->{price_dollar} | |
48 | and $entry->{retailPrice} *= 100; | |
49 | } | |
50 | else { | |
51 | $importer->warn("Warning: no price"); | |
52 | $entry->{retailPrice} = 0; | |
53 | } | |
54 | } | |
55 | ||
56 | sub children_of { | |
57 | my ($self, $parent) = @_; | |
58 | ||
59 | return grep $_->{generator} eq 'Generate::Catalog', | |
60 | Articles->children($parent); | |
61 | } | |
62 | ||
63 | sub make_parent { | |
64 | my ($self, $importer, %entry) = @_; | |
65 | ||
3709451d TC |
66 | return bse_make_catalog(%entry); |
67 | } | |
68 | ||
69 | sub find_leaf { | |
70 | my ($self, $leaf_id) = @_; | |
71 | ||
0cca6ce6 | 72 | my ($leaf) = Products->getBy($self->{code_field}, $leaf_id) |
3709451d TC |
73 | or return; |
74 | ||
0cca6ce6 TC |
75 | if ($self->{reset_prodopts}) { |
76 | my @options = $leaf->db_options; | |
77 | for my $option (@options) { | |
78 | $option->remove; | |
79 | } | |
80 | } | |
81 | ||
3709451d TC |
82 | return $leaf; |
83 | } | |
84 | ||
85 | sub make_leaf { | |
86 | my ($self, $importer, %entry) = @_; | |
87 | ||
3709451d TC |
88 | return bse_make_product(%entry); |
89 | } | |
90 | ||
0cca6ce6 TC |
91 | sub fill_leaf { |
92 | my ($self, $importer, $leaf, %entry) = @_; | |
3709451d | 93 | |
0cca6ce6 TC |
94 | my $ordering = time; |
95 | for my $opt_num (1 .. 5) { | |
96 | my $name = $entry{"prodopt${opt_num}_name"}; | |
97 | my $values = $entry{"prodopt${opt_num}_values"}; | |
3709451d | 98 | |
0cca6ce6 TC |
99 | defined $name && $name =~ /\S/ && $values =~ /\S/ |
100 | or next; | |
101 | my @values = split /\Q$self->{prodopt_value_sep}/, $values | |
3709451d | 102 | or next; |
3709451d | 103 | |
0cca6ce6 | 104 | my $option = BSE::TB::ProductOptions->make |
3709451d | 105 | ( |
0cca6ce6 TC |
106 | product_id => $leaf->id, |
107 | name => $name, | |
108 | display_order => $ordering++, | |
3709451d | 109 | ); |
3709451d | 110 | |
0cca6ce6 TC |
111 | for my $value (@values) { |
112 | my $entry = BSE::TB::ProductOptionValues->make | |
113 | ( | |
114 | product_option_id => $option->id, | |
115 | value => $value, | |
116 | display_order => $ordering++, | |
117 | ); | |
118 | } | |
3709451d | 119 | } |
df2663f0 TC |
120 | |
121 | my %prices; | |
122 | for my $tier_id (keys %{$self->{price_tiers}}) { | |
123 | my $price = $entry{"tier_price_$tier_id"}; | |
124 | if (defined $price && $price =~ /\d/) { | |
125 | $price =~ s/\$//; # in case | |
126 | $price *= 100 if $self->{price_dollar}; | |
127 | ||
128 | $prices{$tier_id} = $price; | |
129 | } | |
130 | } | |
131 | ||
132 | $leaf->set_prices(\%prices); | |
133 | ||
0cca6ce6 | 134 | return $self->SUPER::fill_leaf($importer, $leaf, %entry); |
3709451d TC |
135 | } |
136 | ||
137 | sub default_parent { 3 } | |
138 | ||
139 | sub default_code_field { "product_code" } | |
140 | ||
141 | 1; |