1 package BSE::ProductImportXLS;
3 use Spreadsheet::ParseExcel;
4 use BSE::API qw(bse_make_product bse_make_catalog bse_add_image);
9 our $VERSION = "1.000";
12 my ($class, $cfg, $profile, %opts) = @_;
15 my $section = "xls import $profile";
16 my %ids = $cfg->entriesCS($section);
18 or die "No entries found for profile $profile\n";
20 my $sheet = $cfg->entry($section, "sheet", 1);
21 my $skiprows = $cfg->entry($section, 'skiprows', 1);
22 my $use_codes = $cfg->entry($section, 'codes', 0);
23 my $parent = $cfg->entry($section, 'parent', 3);
24 my $price_dollar = $cfg->entry($section, 'price_dollar', 0);
25 my $reset_images = $cfg->entry($section, 'reset_images', 0);
26 my $file_path = $cfg->entry($section, 'file_path');
27 defined $file_path or $file_path = '';
28 my @file_path = split /$Config{path_sep}/, $file_path;
29 if ($opts{file_path}) {
33 split /$Config{path_sep}/, $_
39 for my $map (grep /^map_\w+$/, keys %ids) {
40 (my $out = $map) =~ s/^map_//;
43 or die "Mapping for $out not numeric\n";
47 for my $set (grep /^set_\w+$/, keys %ids) {
48 (my $out = $set) =~ s/^set_//;
49 $set{$out} = $ids{$set};
52 for my $xform (grep /^xform_\w+$/, keys %ids) {
53 (my $out = $xform) =~ s/^xform_//;
55 or die "Xform for $out but no mapping\n";
56 my $code = "sub { (local \$_, my \$product) = \@_; \n".$ids{$xform}."\n; return \$_ }";
59 or die "Compilation error for $xform code: $@\n";
63 or die "No title mapping found\n";
64 defined $map{retailPrice}
65 or die "No retailPrice mapping found\n";
66 if ($use_codes && !defined $map{product_code}) {
67 die "No product_code mapping found with 'codes' enabled\n";
70 for my $cat (qw/cat1 cat2 cat3/) {
72 $col and push @cats, $col;
81 skiprows => $skiprows,
85 price_dollar => $price_dollar,
86 reset_images => $reset_images,
88 file_path => \@file_path,
89 product_template => scalar($cfg->entry($section, 'product_template')),
90 catalog_template => scalar($cfg->entry($section, 'catalog_template')),
95 my ($class, $cfg) = @_;
97 my %ids = $cfg->entries("xls product imports");
102 my ($self, $filename, $callback) = @_;
104 $self->{catseen} = {};
105 $self->{catalogs} = [];
106 $self->{products} = [];
107 my $parser = Spreadsheet::ParseExcel->new;
108 my $wb = $parser->Parse($filename)
109 or die "Could not parse $filename";
110 $self->{sheet} <= $wb->{SheetCount}
111 or die "No enough worksheets in input\n";
112 my $ws = ($wb->worksheets)[$self->{sheet}-1]
113 or die "No worksheet found at $self->{sheet}\n";
115 my ($minrow, $maxrow) = $ws->RowRange;
118 for my $rownum ($self->{skiprows} ... $maxrow) {
120 my %entry = %{$self->{set}};
122 $self->{product_template}
123 and $entry{template} = $self->{product_template};
127 for my $col (keys %{$self->{map}}) {
128 my $cell = $ws->get_cell($rownum, $self->{map}{$col}-1);
130 $entry{$col} = $cell->value;
135 $non_blank ||= $entry{$col} =~ /\S/;
139 for my $col (keys %{$self->{xform}}) {
140 $entry{$col} = $self->{xform}{$col}->($entry{$col}, \%entry);
142 $entry{title} =~ /\S/
143 or die "title blank\n";
144 if ($self->{codes}) {
145 $entry{product_code} =~ /\S/
146 or die "product_code blank with use_codes\n";
148 $entry{retailPrice} =~ s/\$//; # in case
150 if ($entry{retailPrice} =~ /\d/) {
151 $self->{price_dollar}
152 and $entry{retailPrice} *= 100;
156 and $callback->("Row $rownum: Warning: no price");
157 $entry{retailPrice} = 0;
159 $entry{title} =~ /\n/
160 and die "Title may not contain newlines";
162 or $entry{summary} = $entry{title};
164 or $entry{description} = $entry{title};
166 or $entry{body} = $entry{title};
169 for my $cat (@{$self->{cats}}) {
170 my $cell = $ws->get_cell($rownum, $cat-1);
173 $value = $cell->value;
174 defined $value && $value =~ /\S/
175 and push @cats, $value;
177 $entry{parentid} = $self->_find_cat(\%cat_cache, $callback, $self->{parent}, @cats);
179 if ($self->{codes}) {
180 $product = Products->getBy(product_code => $entry{product_code});
183 @{$product}{keys %entry} = values %entry;
186 and $callback->("Updated $product->{id}: $entry{title}");
187 if ($self->{reset_images}) {
188 $product->remove_images($self->{cfg});
190 and $callback->(" $product->{id}: Reset images");
195 $product = bse_make_product
201 and $callback->("Added $product->{id}: $entry{title}");
203 for my $image_index (1 .. 10) {
204 my $file = $entry{"image${image_index}_file"};
207 my $full_file = $self->_find_file($file);
209 or die "File '$file' not found for image$image_index\n";
211 my %opts = ( file => $full_file );
212 for my $key (qw/alt name url storage/) {
213 my $fkey = "image${image_index}_$key";
215 and $opts{$key} = $entry{$fkey};
219 my $im = bse_add_image($self->{cfg}, $product, %opts,
222 or die join(", ",map "$_: $errors{$_}", keys %errors), "\n";
224 and $callback->(" $product->{id}: Add image '$file'");
226 push @{$self->{products}}, $product;
229 my $error = "Row ".($rownum+1).": $@";
232 push @{$self->{errors}}, $error;
234 and $callback->("Error: $error");
240 my ($self, $cache, $callback, $parent, @cats) = @_;
244 unless ($cache->{$parent}) {
245 my @kids = grep $_->{generator} eq 'BSE::Generate::Catalog',
246 Articles->children($parent);
247 $cache->{$parent} = \@kids;
250 my $title = shift @cats;
251 my ($cat) = grep lc $_->{title} eq lc $title, @{$cache->{$parent}};
260 $self->{catalog_template}
261 and $opts{template} = $self->{catalog_template};
262 $cat = bse_make_catalog(%opts);
264 and $callback->("Add catalog $cat->{id}: $title");
265 push @{$cache->{$parent}}, $cat;
268 unless ($self->{catseen}{$cat->{id}}) {
269 $self->{catseen}{$cat->{id}} = 1;
270 push @{$self->{catalogs}}, $cat;
273 return $self->_find_cat($cache, $callback, $cat->{id}, @cats);
278 and return @{$_[0]{errors}};
285 and return @{$_[0]{products}};
291 $_[0]{catalogs} or return;
293 return @{$_[0]{catalogs}};
297 my ($self, $file) = @_;
299 for my $path (@{$self->{file_path}}) {
300 my $full = "$path/$file";
301 -f $full and return $full;