begin work on importing products

This commit is contained in:
Colin Kuskie 2008-05-27 00:32:25 +00:00
parent 9f8498a359
commit b283ebaf81
2 changed files with 196 additions and 0 deletions

118
lib/WebGUI/Shop/Products.pm Normal file
View file

@ -0,0 +1,118 @@
package WebGUI::Shop::Products;
use strict;
use WebGUI::Text;
use WebGUI::Storage;
use WebGUI::Exception::Shop;
use WebGUI::Shop::Admin;
use WebGUI::Asset::Sku::Product;
=head1 NAME
Package WebGUI::Shop::Products
=head1 DESCRIPTION
This package handles importing and exporting products into the Shop system, mainly
to be compatible with third-party systems, such as inventory control. If you want to
export your Products into another WebGUI site, please use the Asset Export system
instead.
=head1 METHODS
These subroutines are available from this package:
=cut
#-------------------------------------------------------------------
=head2 importProducts ( $session, $filePath )
Import products into the WebGUI system. If the master sku of a product
exists in the system, it will be updated. If master skus do not exist,
they will be added.
The first line of the file should contain only the name of the columns,
in any order. It may not contain comments.
These are the column names, each is required:
=over 4
=item *
mastersku
=item *
sku
=item *
title
=item *
shortdescription
=item *
price
=item *
weight
=item *
quantity
=back
The following lines will contain product information. Blank
lines and anything following a '#' sign will be ignored from
the second line of the file, on to the end.
Returns 1 if the import has taken place. This is to help you know
if old data has been deleted and new has been inserted.
=cut
sub importProducts {
my $session = shift;
my $filePath = shift;
WebGUI::Error::InvalidParam->throw(error => q{Must provide the path to a file})
unless $filePath;
WebGUI::Error::InvalidFile->throw(error => qq{File could not be found}, brokenFile => $filePath)
unless -e $filePath;
WebGUI::Error::InvalidFile->throw(error => qq{File is not readable}, brokenFile => $filePath)
unless -r $filePath;
open my $table, '<', $filePath or
WebGUI::Error->throw(error => qq{Unable to open $filePath for reading: $!\n});
my $headers;
$headers = <$table>;
chomp $headers;
my @headers = WebGUI::Text::splitCSV($headers);
WebGUI::Error::InvalidFile->throw(error => qq{Bad header found in the CSV file}, brokenFile => $filePath)
unless (join(q{-}, sort @headers) eq 'city-code-country-state-taxRate')
and (scalar @headers == 5);
my @productData = ();
my $line = 1;
while (my $productRow = <$table>) {
chomp $productRow;
$productRow =~ s/\s*#.+$//;
next unless $productRow;
local $_;
my @productRow = map { tr/|/,/; $_; } WebGUI::Text::splitCSV($productRow);
WebGUI::Error::InvalidFile->throw(error => qq{Error found in the CSV file}, brokenFile => $filePath, brokenLine => $line)
unless scalar @productRow == 5;
push @productData, [ @productRow ];
}
##Okay, if we got this far, then the data looks fine.
return unless scalar @productData;
return 1;
}
1;