#!/usr/bin/perl
use strict;
use DBI;
use Encode;
use lib '/home/USER/perl-lib';
use Spreadsheet::WriteExcel;
# USER - замените
# в папку /home/USER/perl-lib покласть перл-модуль (в прикреплении)
# Connect to database
my $user = "DB-USER";
my $pass = "DB-PASS";
my $database = "DB-NAME";
my $hostname = "127.0.0.1";
my $source = "dbi:mysql:$database";
my ($dbh,$sth,$sql,$stherror);
$dbh = DBI -> connect( $source, $user, $pass, { PrintError => 0, RaiseError => 1 }) or die "connecting: $DBI::errstr";
$dbh -> do("set character set utf8");
# create image-path for prom.ua
# добавить колонку promua в таблицу ps_image
# или перенести код ниже
$sql="UPDATE ps_image SET promua = CONCAT('http://site-name.ua/img/p/',
MID(id_image,1,1),'/',MID(id_image,2,1),'/',MID(id_image,3,1),'/',MID(id_image,4,1),'/',MID(id_image,5,1),'/',MID(id_image,6,1),'/',MID(id_image,7,1),'/',
id_image,'-large_default.jpg') WHERE promua = '';";
$sth = $dbh->prepare( $sql ) or die "preparing: ", $dbh->errstr;
$sth->execute or die "executing: ", $sth->errstr;
# Create a new Excel file
my $FileName = "/home/USER/public_html/prom/prom-import.xls";
my $workbook = Spreadsheet::WriteExcel->new($FileName);
# Add a worksheet
my $worksheet1 = $workbook->add_worksheet('Export Products Sheet');
# Set a Unicode font.
my $uni_font = $workbook->add_format(font => 'Arial Unicode MS');
$worksheet1->write( 0, 0, Encode::decode('UTF-8', "Код_товара"), $uni_font);
$worksheet1->write( 0, 1, Encode::decode('UTF-8', "Название_позиции"), $uni_font);
$worksheet1->write( 0, 2, Encode::decode('UTF-8', "Ключевые_слова"), $uni_font);
$worksheet1->write( 0, 3, Encode::decode('UTF-8', "Описание"), $uni_font);
$worksheet1->write( 0, 4, Encode::decode('UTF-8', "Тип_товара"), $uni_font);
$worksheet1->write( 0, 5, Encode::decode('UTF-8', "Цена"), $uni_font);
$worksheet1->write( 0, 6, Encode::decode('UTF-8', "Валюта"), $uni_font);
$worksheet1->write( 0, 7, Encode::decode('UTF-8', "Единица_измерения"), $uni_font);
$worksheet1->write( 0, 8, Encode::decode('UTF-8', "Минимальный_объем_заказа"), $uni_font);
$worksheet1->write( 0, 9, Encode::decode('UTF-8', "Ссылка_изображения"), $uni_font);
$worksheet1->write( 0, 10, Encode::decode('UTF-8', "Наличие"), $uni_font);
$worksheet1->write( 0, 11, Encode::decode('UTF-8', "Идентификатор_товара"), $uni_font);
$worksheet1->write( 0, 12, Encode::decode('UTF-8', "Номер_группы"), $uni_font);
$worksheet1->write( 0, 13, Encode::decode('UTF-8', "Название_Характеристики"), $uni_font);
$worksheet1->write( 0, 14, Encode::decode('UTF-8', "Измерение_Характеристики"), $uni_font);
$worksheet1->write( 0, 15, Encode::decode('UTF-8', "Значение_Характеристики"), $uni_font);
$worksheet1->write( 0, 16, Encode::decode('UTF-8', "Название_Характеристики"), $uni_font);
$worksheet1->write( 0, 17, Encode::decode('UTF-8', "Измерение_Характеристики"), $uni_font);
$worksheet1->write( 0, 18, Encode::decode('UTF-8', "Значение_Характеристики"), $uni_font);
$worksheet1->write( 0, 19, Encode::decode('UTF-8', "Название_Характеристики"), $uni_font);
$worksheet1->write( 0, 20, Encode::decode('UTF-8', "Измерение_Характеристики"), $uni_font);
$worksheet1->write( 0, 21, Encode::decode('UTF-8', "Значение_Характеристики"), $uni_font);
$worksheet1->write( 0, 22, Encode::decode('UTF-8', "Название_Характеристики"), $uni_font);
$worksheet1->write( 0, 23, Encode::decode('UTF-8', "Измерение_Характеристики"), $uni_font);
$worksheet1->write( 0, 24, Encode::decode('UTF-8', "Значение_Характеристики"), $uni_font);
$worksheet1->write( 0, 25, Encode::decode('UTF-8', "Название_Характеристики"), $uni_font);
$worksheet1->write( 0, 26, Encode::decode('UTF-8', "Измерение_Характеристики"), $uni_font);
$worksheet1->write( 0, 27, Encode::decode('UTF-8', "Значение_Характеристики"), $uni_font);
$worksheet1->write( 0, 28, Encode::decode('UTF-8', "Название_Характеристики"), $uni_font);
$worksheet1->write( 0, 29, Encode::decode('UTF-8', "Измерение_Характеристики"), $uni_font);
$worksheet1->write( 0, 30, Encode::decode('UTF-8', "Значение_Характеристики"), $uni_font);
$worksheet1->write( 0, 31, Encode::decode('UTF-8', "Название_Характеристики"), $uni_font);
$worksheet1->write( 0, 32, Encode::decode('UTF-8', "Измерение_Характеристики"), $uni_font);
$worksheet1->write( 0, 33, Encode::decode('UTF-8', "Значение_Характеристики"), $uni_font);
$worksheet1->write( 0, 34, Encode::decode('UTF-8', "Название_Характеристики"), $uni_font);
$worksheet1->write( 0, 35, Encode::decode('UTF-8', "Измерение_Характеристики"), $uni_font);
$worksheet1->write( 0, 36, Encode::decode('UTF-8', "Значение_Характеристики"), $uni_font);
$worksheet1->write( 0, 37, Encode::decode('UTF-8', "Название_Характеристики"), $uni_font);
$worksheet1->write( 0, 38, Encode::decode('UTF-8', "Измерение_Характеристики"), $uni_font);
$worksheet1->write( 0, 39, Encode::decode('UTF-8', "Значение_Характеристики"), $uni_font);
$worksheet1->write( 0, 40, Encode::decode('UTF-8', "Название_Характеристики"), $uni_font);
$worksheet1->write( 0, 41, Encode::decode('UTF-8', "Измерение_Характеристики"), $uni_font);
$worksheet1->write( 0, 42, Encode::decode('UTF-8', "Значение_Характеристики"), $uni_font);
# IF (LENGTH(pl.meta_keywords) > 0, pl.meta_keywords, IF(LENGTH(GROUP_CONCAT(t.name)) > 0, GROUP_CONCAT(DISTINCT t.name), '')),
$sql = "SELECT p.reference, pl.name,
CONCAT( pl.meta_keywords, IF(LENGTH(pl.meta_keywords) > 0,',',''), GROUP_CONCAT(DISTINCT t.name)), pl.description,
IF( p.price = 0, '1', p.price), GROUP_CONCAT( DISTINCT i.promua ORDER BY i.position ASC SEPARATOR ', ' ),
IF( p.active = 1, IF(sav.quantity > 0, '+', '-'), '-'), c.id_promua, p.id_product
FROM ps_product AS p
LEFT JOIN ps_product_lang AS pl ON (pl.id_product = p.id_product )
LEFT JOIN ps_image AS i ON (p.id_product = i.id_product)
LEFT JOIN ps_stock_available AS sav ON (sav.id_product = p.id_product)
LEFT JOIN ps_category AS c ON (p.id_category_default = c.id_category)
LEFT JOIN ps_product_tag AS pt ON (pt.id_product = p.id_product AND pl.id_lang = pt.id_lang)
LEFT JOIN ps_tag AS t ON (t.id_tag = pt.id_tag)
WHERE pl.id_lang = 2
GROUP BY p.id_product";
$sth = $dbh->prepare( $sql ) or die "preparing: ", $dbh->errstr;
$sth->execute or die "executing: ", $sth->errstr;
my $i=1;
while(( my @row )= $sth->fetchrow_array ){
$worksheet1->write( $i, 0, Encode::decode('UTF-8', $row[0]), $uni_font); # Код_товара = p.reference
$worksheet1->write( $i, 1, Encode::decode('UTF-8', $row[1]), $uni_font); # Название_позиции = pl.name
$worksheet1->write( $i, 2, Encode::decode('UTF-8', $row[2]), $uni_font); # Ключевые_слова = pl.meta_keywords, t.name
$worksheet1->write( $i, 3, Encode::decode('UTF-8', $row[3]), $uni_font); # Описание = pl.description
$worksheet1->write( $i, 4, Encode::decode('UTF-8', "r"), $uni_font); # Тип_товара = "r"
$worksheet1->write( $i, 5, $row[4]); # Цена = p.price
$worksheet1->write( $i, 6, "UAH" ); # Валюта = "UAH"
$worksheet1->write( $i, 7, Encode::decode('UTF-8', "шт."), $uni_font); # Единица_измерения = "шт."
$worksheet1->write( $i, 8, "1" ); # Минимальный_объем_заказа = "1"
$worksheet1->write( $i, 9, $row[5]); # Ссылка_изображения = i.promua
$worksheet1->write( $i, 10, $row[6]); # Наличие = p.active
$worksheet1->write( $i, 11, $row[0]); # Идентификатор_товара = p.reference
$worksheet1->write( $i, 12, $row[7]); # Номер_группы = c.id_promua
my $sql2 = "SELECT agl.name, GROUP_CONCAT(DISTINCT al.name SEPARATOR '|')
FROM ps_product AS p
LEFT JOIN ps_product_attribute AS pa ON ( pa.id_product = p.id_product )
LEFT JOIN ps_product_attribute_combination AS pac ON (pac.id_product_attribute = pa.id_product_attribute)
LEFT JOIN ps_attribute AS a ON (a.id_attribute = pac.id_attribute)
LEFT JOIN ps_attribute_lang AS al ON (al.id_attribute = pac.id_attribute)
LEFT JOIN ps_attribute_group_lang AS agl ON (agl.id_attribute_group = a.id_attribute_group )
WHERE al.id_lang = 2 AND pa.id_product = '".$row[8]."'
GROUP BY a.id_attribute_group;
";
my $sth2 = $dbh->prepare( $sql2 ) or die "preparing: ", $dbh->errstr;
$sth2->execute or die "executing: ", $sth2->errstr;
my $y=12;
while(( my @row2 )= $sth2->fetchrow_array ){
$worksheet1->write( $i, $y+1, Encode::decode('UTF-8', $row2[0]), $uni_font); # Название_Характеристики
$worksheet1->write( $i, $y+2, Encode::decode('UTF-8', ""), $uni_font); # Измерение_Характеристики
$worksheet1->write( $i, $y+3, Encode::decode('UTF-8', $row2[1]), $uni_font); # Значение_Характеристики
$y=$y+3;
}
$i++;
}
$workbook->close();