由于经常见有些朋友说eBay的帐单乱码问题、不方便计算一些交易数据等,所以写了一个PHP程序实现一些功能,现在是调试阶段,希望大家试用并反馈意见,提出更多的功能需求。
使用方法:
一次性安装设置工作:
1、下载PHP5安装。http://windows.php.net/download/ 选择一个VC6 x86 Thread Safe版本的就可以。在Web Server setup时选择”Do not setup a web server”。其余默认。
2、建一个工作目录,如 E:\ebayprofic
3、在工作目录下新建一个currency.txt 文本文件,内容为你Paypal帐单中可能出现的货币代码加空格加对人-民币的汇率,如下:
USD 6.8129
GBP 9.9861
EUR 8.6208
HKD 0.8758
AUD 0.6036
4、在工作目录新建一个 runit.bat 批处理文件,记事本打开,内容为:
php ebayprofit.php
pause
5、在工作目录新建一个PHP程序文件名为 ebayprofit.php ,用记事本打开并最大化,把下一个帖内的源代码拷贝进去保存,源代码即<?PHP 开头至?>结尾。
每次计算一个时间段的帐单时:
1、下载一段时间的eBay帐单(不包含简版summarized)保存到工作目录下并重命名为ebay.csv,不要用EXCEL之类的软件打开保存,会损坏其格式的。
2、下载一段时间的Paypal帐单保存到工作目录下并重命名为paypal.csv,不要用EXCEL之类的软件打开保存,会损坏其格式的。
3、点击运行runit.bat,会生成/更新两个文件:buyinprice.csv,为物品成本价,以RMB为单位,需要更新一下新物品价格。result.csv是一个结果表。
现阶段:
[2010-05-12]初始调试阶段,目标:正常运行并取出eBay帐单中成交费相关字段保存至result.csv表中。
历史阶段:
[2010-05-10]前进一步调试阶段,目标:正常运行,取出Paypal帐单中的记录,与eBay帐单匹配取出字段,保存至result.csv表中。
TODO:
解决多币种记录问题。
处理eBay合并帐单生成的单笔Paypal记录。
用户支持,其他暂未想到的功能。
Q&A:
1、看不懂这么长的程序代码。
A:不需要看懂程序代码,只需要使用即可。
2、安全问题,怕丢失eBay密码,泄露成本秘密。
A:PHP程序是免费通用软件,该PHP程序也不存在访问网络及泄露数据至网络上的问题。
<?php
#version 2010-05-12
$currencyHandler = fopen(“currency.txt”, “r”);
$currency = array();
$paymentGot = array();
$paymentFee = array();
while (!feof($currencyHandler)) {
$line = fgets($currencyHandler);
$explded = explode(” “, $line);
$currencyType = $explded[0];
$currency[$currencyType] = $explded[1];
$paymentGot[$currencyType] = 0;
$paymentFee[$currencyType] = 0;
}
fclose($currencyHandler); #close currency.txt file
if(file_exists(“buyinprice.csv”)){
$buyinpriceHandler = fopen(“buyinprice.csv”, “r+”);
}else{
$buyinpriceHandler = fopen(“buyinprice.csv”, “w+”);
}
$buyinprice = array();
while (!feof($buyinpriceHandler)) {
$line = fgets($buyinpriceHandler);
$explded = explode(“,”, $line);
if(isset($explded[0]) && $explded[0]){
$itemTitle = $explded[0];
$buyinprice[$itemTitle] = str_replace(“\n”,”",$explded[1]);
}
} #keep $buyinpriceHandler open
$WuPinKanDengFeiBig5 = “\xAA\xAB\xAB\x7E\xA5\x5A\xB5\x6E\xB6\x4F”;
$ChengJiaoFeiBig5 = “\xA6\xA8\xA5\xE6\xB6\x4F”;
$MaoHaoBig5 = “\xA1\x47″;
$MaiChuBig5 = “\xBD\xE6\xA5\x58″;
$ZuoKuaHaoBig5 = “\xA1\x5D”;
$YouKuaHaoBig5 = “\xA1\x5E”;
$XiaoShiBig5 = “\xA4\x70\xAE\xC9″;
$FengBig5 = “\xA4\xC0″;
$MiaoBig5 = “\xAC\xED”;
$selltypeBig5 = array(
“\xA9\xE7\xBD\xE6″ => “auction”,
“\xB0\xD3\xA9\xB1″ => “store”,
“\xA4\x40\xA4\x66\xBB\xF9″ => “fixprice”
);
$ebay2paypalCurrency = array(
“US $” => “USD”,
“GBP” => “GBP”,
“EUR” => “EUR”,
“AU $” => “AUD”,
“HK $” => “HKD”
);
$sold = array();
$fvFee = 0;
$insertFee = 0;
$insertCount = 0;
$ebayHandler = fopen(“ebay.csv”, “r”);
while (!feof($ebayHandler)) {
$line = fgets($ebayHandler);
$explded = explode(“,”, $line);
if(isset($explded[4])){
if(strpos($explded[3],$WuPinKanDengFeiBig5) !== false){
$fee = str_replace(“US \$”,”",$explded[4]);
$insertFee += $fee;
$insertCount++;
}
if((strpos($explded[3],$ChengJiaoFeiBig5) !== false) && (strpos($explded[4],”-”) === false)){
$timeH = str_replace($XiaoShiBig5,”:”,$explded[0]);
$timeM = str_replace($FengBig5,”:”,$timeH);
$timeString = str_replace($MiaoBig5,”",$timeM);
$fee = str_replace(“US \$”,”",$explded[4]);
$fvFee += $fee;
$selldetail = explode(“;”, $explded[1]);
$itemTitle = $selldetail[0];
if(!isset($buyinprice[$itemTitle])){ #unknown item buyinprice
$content = $itemTitle.”,\n”;
fwrite($buyinpriceHandler,$content);
$buyinprice[$itemTitle] = ”;
}
$sold[$itemTitle][$timeString]["detail"] = $selldetail[1];
$sold[$itemTitle][$timeString]["fvf"] = “-”.$fee;
$soldprice = explode($MaoHaoBig5,$selldetail[1]);
$dummy = preg_match(‘/\d+.\d+/’,$soldprice[1],$price);
$sold[$itemTitle][$timeString]["totalprice"] = $price[0];
$sold[$itemTitle][$timeString]["fvf_ratio"] = round(($fee*100/$price[0]),1).”%”;
if(strpos($selldetail[1],$MaiChuBig5) !== false){
$soldmulti = explode($MaiChuBig5, $selldetail[1]);
$dummy = preg_match(‘/ \d+ /’,$soldmulti[1],$sdigitals);
$quantity = intval($sdigitals[0]);
$sold[$itemTitle][$timeString]["quantity"] = $quantity;
$dummy = preg_match(‘/\d+.\d+/’,$soldmulti[1],$sprice);
$sold[$itemTitle][$timeString]["singleprice"] = $sprice[0];
}else{
$sold[$itemTitle][$timeString]["quantity"] = 1;
$sold[$itemTitle][$timeString]["singleprice"] = $price[0];
}
}
}
}
fclose($buyinpriceHandler);
printf (“insert Fee: %s USD\n”,$insertFee);
printf (“final Value Fee: %s USD\n”,$fvFee);
foreach ($sold as $title => $timeArray){
foreach ($timeArray as $time => $item){
$content = $time.”,”.$title.”,”.$item["quantity"].”,”.$item["singleprice"].”,”;
$content .= $item["totalprice"].”,”.$item["fvf"].”,”.$item["detail"].”\n”;
# fwrite($resultHandler,$content);
}
}
$paypalHandler = fopen(“paypal.csv”, “r”);
$dummy = fgets($paypalHandler);#ignore first Line
$resultHandler = fopen(“result.csv”, “w+”);
$content = “time,title,quantity,singleprice,totalprice,eBay_fvf,fvf_ratio,Ship,total_cost,”;
$content .= “Paypal_fee,Paypal_ratio,Net,totalratio,Net_RMB,buyinprice,shipRMB,buyerName,Country,buyerID\n”;
fwrite($resultHandler,$content);
while (!feof($paypalHandler)) {
$line = fgets($paypalHandler);
$explded = explode(“\”,\”", $line);
if(!isset($explded[20])){
continue; #ignore blank line
}
if( $explded[4] == “Currency Conversion”){
continue; #ignore payment type
}
if( $explded[4] == “eCheck Received”){
continue; #ignore payment type
}
if( $explded[4] == “Request Sent”){
continue; #ignore payment type
}
if( strpos($explded[4],”Withdraw”) !== false){
continue; #ignore payment type
}
$status = $explded[5];
$currencyType = $explded[6];
switch ($status){
case “Completed”:
$isPayment = 1;
break;
case “Cleared”:
$isPayment = 1;
break;
case “Partially Refunded”:
$isPayment = 1;
break;
case “Refunded”:
$isPayment = 1;
break;
default:
$isPayment = 0;
}
if($isPayment){
$got = str_replace(“,”,”",$explded[9]);
$fee = str_replace(“,”,”",$explded[8]);
if(isset($paymentGot[$currencyType])){
$paymentGot[$currencyType] += $got;
$paymentFee[$currencyType] += $fee;
}else{
printf (“Ignore unkown Payment Currency %s %s”,$currencyType,$got);
}
$title = $explded[15];
if(isset($sold[$title])){
$gross = str_replace(“,”,”",$explded[7]);
$shipping = str_replace(“,”,”",$explded[17]);
$paypalfee = str_replace(“,”,”",$explded[8]);
$paypalnet = str_replace(“,”,”",$explded[9]);
$paypalratio = round((abs($paypalfee)*100/$gross),1).”%”;
$totalratio = round((abs($paypalfee)*100/$gross),1).”%”;
$currencyType = $explded[6];
$buyerid = $explded[25];
$buyername = $explded[3];
$country = $explded[40];
#if( strpos($title,”;”) !== false){
if(count($sold[$title]) == 1 ){
foreach ( $sold[$title] as $time => $item){
#get time && item[]
}
}else{
$found = 0;
$bestTime = ”;
$timeOffset = 9999999999;
$total = strval($explded[7]-$explded[17]);
foreach ( $sold[$title] as $time => $item){
if($item["totalprice"] == $total){
if( strpos($item["detail"],$buyerid) !== false){
$found = 1;
break;
}
$pp_d = explode(“/”, substr($explded[0],1));
$pp_t = explode(“:”, $explded[1]);
$ppt = mktime($pp_t[0],$pp_t[1]+1,$pp_t[2]+1,$pp_d[0],$pp_d[1],$pp_d[2]);
if($explded[2] == “PDT”){
$ppt += 3600*16;
}else{
$ppt += 3600*16;
}
$sp = explode(” “, $time);
$eb_d = explode(“-”, str_replace(“-0″,”-”,$sp[0]));
$eb_t = explode(“:”, str_replace(“:0″,”:”,$sp[1]));
$ebt = mktime($eb_t[0],$eb_t[1],$eb_t[2],$eb_d[1],$eb_d[2],$eb_d[0]);
$offset = abs($ppt – $ebt);
if(($offset < $timeOffset)){
$timeOffset = $offset;
$bestTime = $time;
}
}
}
if(!$found){
$time = $bestTime;
$item[] = $sold[$title][$time];
}
}
if(isset($currency[$currencyType])){
$NetRMB = round($currency[$currencyType]*($paypalnet+$item["fvf"]));
}else{
$NetRMB = “N/A”;
}
$buyintotal = $buyinprice[$title]*$item["quantity"];
$totalratio = round((abs($item["fvf"]+$paypalfee)*100/$gross),1).”%”;
$content = $time.”,”.$title.”,”.$item["quantity"].”,”.$item["singleprice"].”,”.$item["totalprice"].”,”;
$content .= $item["fvf"].”,”.$item["fvf_ratio"].”,”.$shipping.”,”.$gross.”,”.$paypalfee.”,”;
$content .= $paypalratio.”,”.$paypalnet.”,”.$totalratio.”,”.$NetRMB.”,-”.$buyintotal.”,,”;
$content .= $buyername.”,”.$country.”,”.$buyerid.”\n”;
fwrite($resultHandler,$content);
}
}
}
print_r($paymentGot);
fclose($paypalHandler);
fclose($resultHandler);
?>