001.
<?php
002.
session_start();
003.
header(
'Content-Type:text/html; charset=utf8'
);
004.
005.
require_once
(
'../../includes/config2.php'
);
006.
require_once
(
'../../includes/database.php'
);
007.
require_once
(
'../../includes/functions.php'
);
008.
require_once
(
'excel/oleread.php'
);
009.
require_once
(
'excel/reader.php'
);
010.
011.
$action
=
$_POST
[
'action'
];
012.
$act_del
=
$_GET
[
'act_del'
];
013.
014.
if
(
$action
==
'import'
) {
015.
$now
=
$_POST
[
'year'
];
016.
$old
= (
$now
- 1);
017.
018.
if
(
is_uploaded_file
(
$_FILES
[
"xls_file"
][
"tmp_name"
]))
019.
{
020.
$ext
=
strtolower
(
pathinfo
(
$_FILES
[
"xls_file"
][
"name"
], PATHINFO_EXTENSION));
021.
$allow
=
array
(
'xls'
);
022.
if
(in_array(
$ext
,
$allow
))
023.
{
024.
025.
$file_name
= md5(
$_FILES
[
"xls_file"
][
"name"
] .
date
(
'Y-m-d H:i:s'
));
026.
027.
$uploadPath
=
$config
[
'report_xls_up'
] .
$file_name
.
".xls"
;
028.
$tempPath
=
$_FILES
[
"xls_file"
][
"tmp_name"
];
029.
move_uploaded_file(
$tempPath
,
$uploadPath
);
030.
031.
$data
=
new
Spreadsheet_Excel_Reader();
032.
$data
->setOutputEncoding(
'utf-8'
);
033.
$data
->read(
$uploadPath
);
034.
error_reporting
(E_ALL ^ E_NOTICE);
035.
036.
$commands
=
array
();
037.
$abbrev
= NULL;
038.
$abbreviations
=
array
();
039.
$error
= FALSE;
040.
041.
for
(
$row
= 2;
$row
<=
$data
->sheets[0][
'numRows'
];
$row
++)
042.
{
043.
$avg_now
= (float)
str_replace
(
','
,
''
,
$data
->sheets[0][
'cells'
][
$row
][6]);
044.
$avg_old
= (float)
str_replace
(
','
,
''
,
$data
->sheets[0][
'cells'
][
$row
][7]);
045.
$avg_target
= (float)
str_replace
(
','
,
''
,
$data
->sheets[0][
'cells'
][
$row
][8]);
046.
$growthYear
= 0;
047.
$growthTarget
= 0;
048.
049.
if
(
$avg_now
!= 0 AND
$avg_old
!= 0 AND
$avg_target
!= 0)
050.
{
051.
$growthYear
= (float) (((
$avg_now
-
$avg_old
) /
$avg_old
) * 100);
052.
$growthTarget
= (float) (((
$avg_now
-
$avg_target
) /
$avg_target
) *100);
053.
}
054.
elseif
(
$avg_now
== 0 AND
$avg_old
!= 0 AND
$avg_target
!= 0)
055.
{
056.
$growthYear
= 0;
057.
$growthTarget
= 0;
058.
}
059.
060.
$SQL
=
"INSERT INTO shop_ranking (id, shop_name, shop_id, val_now_year, val_old_year, avg_now_year, avg_old_year, avg_target, growth_year, growth_target, now_year, old_year, abbreviation, post_by, import_date, hilight) VALUES('',"
;
061.
$SQL
.=
"'"
.
$data
->sheets[0]['cells
'][$row][2] . "'
,";
062.
$SQL
.=
"'"
.
str_replace
(',
','
',$data->sheets[0]['
cells
'][$row][3]) . "'
,";
063.
$SQL
.=
"'"
.
str_replace
(',
','
',$data->sheets[0]['
cells
'][$row][4]) . "'
,";
064.
$SQL
.=
"'"
.
str_replace
(',
','
',$data->sheets[0]['
cells
'][$row][5]) . "'
,";
065.
$SQL
.=
"'"
.
$avg_now
.
"',"
;
066.
$SQL
.=
"'"
.
$avg_old
.
"',"
;
067.
$SQL
.=
"'"
.
$avg_target
.
"',"
;
068.
$SQL
.=
"'"
.
$growthYear
.
"',"
;
069.
$SQL
.=
"'"
.
$growthTarget
.
"',"
;
070.
$SQL
.=
"'"
.
$now
.
"',"
;
071.
$SQL
.=
"'"
.
$old
.
"',"
;
072.
$SQL
.=
"'"
.
$data
->sheets[0]['cells
'][$row][1] . "'
,";
073.
$SQL
.=
"'"
.
$_SESSION
['useradmin
'] . "'
,";
074.
$SQL
.=
"'"
.
date
('Y-m-d H:i:s
') . "'
,";
075.
$SQL
.=
"'0'"
;
076.
$SQL
.=
");"
;
077.
$commands
[] =
$SQL
;
078.
079.
}
080.
@unlink(
$uploadPath
);
081.
082.
083.
mysql_query(
"TRUNCATE TABLE shop_ranking"
);
084.
085.
foreach
(
$commands
as
$command
) {
086.
if
( ! mysql_query(
$command
)) {
087.
$error
= TRUE;
088.
break
;
089.
}
090.
091.
}
092.
if
(
$error
){
093.
re_transection(
$abbreviations
,
$now
,
$old
);
094.
}
else
{
095.
echo
redirect(
'form_import.php?msg=import_success'
);
096.
}
097.
}
else
{
098.
echo
redirect(
'form_import.php?msg=extension_error'
);
099.
}
100.
}
else
{
101.
echo
redirect(
'form_import.php?msg=upload_error'
);
102.
}
103.
}
104.
105.
?>