-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
138 lines (102 loc) · 3.99 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
// include FTP class
include "lib/FTP.Class.php";
require_once('lib/parsecsv.lib.php');
//configs
$remoteFolder = "out"; //without start or end slash
$remoteFile = "inventory.csv"; //file on remote
$timeFormat = "Y-m-d_H-i-s"; //date format and file name
$hours = 24; // how old files store in folders, in hours
$localFolder = "updated/"; //relative or abs path in which folder to store. with last slash !
$localFileSellerActive = $localFolder."SellerActive.csv"; // can be like $localFile
// set FTPC object
$ftp = new FTPC("ftp.server.com", "user", "pass");
///////////////
// functions //
///////////////
/**
* Delete files in $folder if creation time in hours is too old.
* @param $folder
* @param int $hours
*/
function deleteOld($folder, $hours = 24){
$folder = $folder."*";
$files = glob($folder);
foreach($files as $file){
$lastModifiedTime = filemtime($file);
$currentTime = time();
$timeDiff = abs($currentTime - $lastModifiedTime)/( 60 * 60);
if(is_file($file) && $timeDiff > $hours)
unlink($file);
}
}
/**
* Transform into SellerActive file, using custom formulas
* @param $data
*/
function processCSV($data){
$dataSellerActive = array();
foreach ($data as $value){
// "My custom formulas"
$itemCost = $value['price'];
$shippingCost = ($value['1stclass'] == 0) ? 7.95 : 2.61 ;
$sellerCost = $itemCost + $shippingCost;
$MAPPrice = $value['map'];
$MRPPrice = $value['mrp'];
$sellerCostPlusCommission = $sellerCost / 0.91;
$newCustomSellerCost = ($MAPPrice > $sellerCostPlusCommission) ? $MAPPrice : $sellerCostPlusCommission ;
// MAX(IF(ISBLANK(MAP Price),"0.00",MAP Price),(SellerCostplusCommission)) megaformula :)
$maxPrice = $newCustomSellerCost * 1.25;
// Import fields into SellerActive
$dataSellerActive[] = array(
'SellerSKU' => $value['sku'],
'Cost' => round($sellerCost, 2),
'Price (Preferred)' => round($maxPrice, 2),
'Price (minimum)' => round($value['list'], 2),
'Price (maximum)' => round($maxPrice, 2),
'MAP Price' => round($MAPPrice, 2),
'Price (retail)' => round($value['list'], 2)
);
}
return $dataSellerActive;
}
/////////
// run //
/////////
// start connect to FTP server
if($ftp->connect()) {
$ftp->cd($remoteFolder);
$modifiedTime = $ftp->modifiedTime($remoteFile, $timeFormat);
$localFile = $localFolder.$modifiedTime.".csv";//like 2017-02-08_13-56-00.csv
if(!file_exists($localFile)) {
//unlink all files in $localFolder
//array_map('unlink', glob($localFolder."*"));
// or delete only old files:
deleteOld($localFolder, $hours);
//download and write file
if ($ftp->get($remoteFile, $localFile)) {
//"File downloaded"
# create new parseCSV object.
$csv = new parseCSV();
# Parse '***.csv' using automatic delimiter detection...
$csv->auto($localFile);
print "<br />import remote file for processing";
# Output result.
#echo "<pre>";
#print_r($csv->data);
# Transform into SellerActive using custom formulas
$customFormula = processCSV($csv->data);
# write
$csvSellerActive = new parseCSV();
$csvSellerActive->save($localFileSellerActive, $customFormula);
print "<br />Write suceffily";
} else {
print "<br />Download failed: " . $ftp->error;
}
}else{
print "<br />All up to date and fresh. No needs to update.";
}
} else {
// connection failed, display last error
print "Connection failed: " . $ftp->error;
}