优易软件——同步订单-php开发ebay店铺管理系统

更新时间:2014-05-02 点击量:2142

 

新订单通知

新订单通知

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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?php
 
/**
* php开发ebay店铺管理系统 同步订单
* ----------------------------------------------------------------------------
* http://www.ecshop.com
* 阿牛ecshop的博客 致力于php技术
* ----------------------------------------------------------------------------
*/
 
define('IN_ECS', true);
 
require(dirname(__FILE__) . '/includes/init.php');
require_once(ROOT_PATH . ADMIN_PATH . '/includes/cls_sql_dump.php');
require(ROOT_PATH . ADMIN_PATH . '/includes/lib_ebay.php');
 
@ini_set('memory_limit', '64M');
 
/* 同步选项 */
if ($_REQUEST['act'] == 'do')
{
    admin_priv('sync_order'); //检查权限
 
    assign_query_info();
    $smarty->assign('action_link', array('text' => $_LANG['01_erp_order_list'], 'href'=>'erp_ebay_order.php?act=list'));
    $smarty->assign('ur_here',  $_LANG['04_erp_sync_order']);
    $smarty->display('erp_ebay_sync_order.htm');
}
 
if ($_REQUEST['act'] == 'start')
{
    $ebay_account = $db->GetOne('SELECT ebay_account FROM ' . $ecs->table('erp_admin_user') . " WHERE user_id = '" . $_SESSION['admin_id'] . "'");    
    $ebay_token = $db->GetOne('SELECT ebay_token FROM ' . $ecs->table('erp_admin_user') . " WHERE user_id = '" . $_SESSION['admin_id'] . "'");
 
    if(empty($ebay_token))
    {
        sys_msg($_LANG['token_empty'], 1);
    }
    else
    {
        @set_time_limit(0); //设置最长执行时间
 
        /* 获取时间选项 */
        $time = empty($_POST['time']) ? '' : trim($_POST['time']);
 
        switch ($time)
        {
            case '1d':
                $start_time = gmtime() - 86400;
                $end_time = gmtime();
                break;
 
            case '3d':
                $start_time = gmtime() - 86400 * 3;
                $end_time = gmtime();
                break;
 
            case '7d':
                $start_time = gmtime() - 86400 * 7;
                $end_time = gmtime();
                break;
 
            case '30d':
                $start_time = gmtime()- 86400 * 30;
                $end_time = gmtime();
                break;
 
            case 'cd':
                $start_time = local_strtotime($_REQUEST['start_time']);
                $end_time = local_strtotime($_REQUEST['end_time']);
 
                if(empty($_REQUEST['start_time']) || empty($_REQUEST['end_time']))
                {
                    sys_msg($_LANG['time_error'], 1);
                }
                break;
        }        
 
        //可读时间区间,用来记录日志。
        $log_txt = local_date($GLOBALS['_CFG']['time_format'], $start_time) . ' - ' . local_date($GLOBALS['_CFG']['time_format'], $end_time);
 
        $start_time = local_date($GLOBALS['_CFG']['date_format'], $start_time) . 'T' . local_date('H:i:s', $start_time) . '.000Z';
        $end_time = local_date($GLOBALS['_CFG']['date_format'], $end_time) . 'T' . local_date('H:i:s', $end_time) . '.000Z';
 
        /* 开始同步 */
        $start_pos = time(); //时间起点
        $page = 1;
        $order = getOrders($start_time, $end_time, $page, $ebay_token);
        $TotalNumberOfEntries = $order->PaginationResult->TotalNumberOfEntries;
        $total_page = $order->PaginationResult->TotalNumberOfPages;    
        $new_order_num = 0; //新增订单
 
        if (empty($order))
        {
            sys_msg($_LANG['sync_fail'], 1);
        }
        else
        {
            for ($page=1; $page<=$total_page; $page++)
            {    
                $order = getOrders($start_time, $end_time, $page, $ebay_token);
 
                $orderArray = $order->OrderArray->Order;
 
                foreach ($orderArray as $key => $value)
                {
                    $Transaction              = $value->TransactionArray->Transaction;    
 
                    $order_id                  = mysql_real_escape_string($value->OrderID);
                    $buyer_id                  = mysql_real_escape_string($value->BuyerUserID);
                    $buyer_name              = mysql_real_escape_string($value->ShippingAddress->Name);
                    is_object($Transaction)  ? $buyer_email = mysql_real_escape_string($Transaction->Buyer->Email) : $buyer_email = mysql_real_escape_string($Transaction[0]->Buyer->Email);
                    $buyer_phone              = mysql_real_escape_string($value->ShippingAddress->Phone);
                    $buyer_postal_code          = mysql_real_escape_string($value->ShippingAddress->PostalCode);
                    $created_time               = strtotime($value->CreatedTime);
                    $paid_time                  = strtotime($value->PaidTime);
                    $country                  = mysql_real_escape_string($value->ShippingAddress->CountryName);
                    $state                       = mysql_real_escape_string($value->ShippingAddress->State);
                    $country_code              = mysql_real_escape_string($value->ShippingAddress->Country);
                    $city                      = mysql_real_escape_string($value->ShippingAddress->CityName);
                    $street1                  = mysql_real_escape_string($value->ShippingAddress->Street1);
                    $street2                  = mysql_real_escape_string($value->ShippingAddress->Street2);
                    $shipping_service_cost      = mysql_real_escape_string($value->ShippingServiceSelected->ShippingServiceCost);
                    $subtotal                 = mysql_real_escape_string($value->Subtotal);
                    $note                      = isset($value->BuyerCheckoutMessage) ? mysql_real_escape_string($value->BuyerCheckoutMessage) : '';                
 
                    //总订购数量
                    $total_quantity = 0;
                    foreach ($Transaction as $key => $t_value)
                    {
                        $total_quantity += $t_value->QuantityPurchased;
                    }
                    reset($Transaction);
 
                    $order_status = 0;
 
                    if($value->OrderStatus == 'Completed' && $value->CheckoutStatus->Status == 'Complete')
                    {
                        $ebay_status = 'Complete';
                    }
                    elseif($value->OrderStatus == 'Active')
                    {
                        $ebay_status = 'Active';
                    }
                    else
                    {
                        $ebay_status = mysql_real_escape_string($value->OrderStatus);    
                    }
 
                    $order_exist_check = order_exist_check($order_id);
 
                    //检查订单是否存在
                    if($order_exist_check)
                    {
                        //更新订单表
                        update_order_status($order_id, $ebay_status);
                    }
                    else
                    {    
                        //写入订单表
                        insert_order($order_id, $buyer_id, $buyer_name, $buyer_email, $buyer_phone, $buyer_postal_code, $created_time, $paid_time, $ebay_status, $country, $state, $country_code, $city, $street1, $street2, $ebay_account, $shipping_service_cost, $subtotal, $note);
 
                        //写入订单商品表
                        foreach ($Transaction as $key => $item)
                        {
                            $item_id          = mysql_real_escape_string($item->Item->ItemID);
                            $transaction_id   = mysql_real_escape_string($item->TransactionID);
                            $item_title       = mysql_real_escape_string($item->Item->Title);
                            $sku               = mysql_real_escape_string($item->Item->SKU);
                            $item_price          = $item->TransactionPrice;
                            $quantity         = $item->QuantityPurchased;
                            $site             = mysql_real_escape_string($item->Item->Site);
 
                            insert_order_goods($item_id, $transaction_id, $order_id, $item_title, $sku, $item_price, $quantity, $site);
                        }
                        $new_order_num++;
                    }
                }
            }
 
            $end_pos = time(); //时间终点
            $process_time = $end_pos - $start_pos; //耗时
 
            /* 设置订单同步邮件模板所需要的内容信息 */
            $template = get_mail_template('sync_order');
 
            $sql = "SELECT user_name FROM " .$ecs->table('erp_admin_user') . " WHERE user_id = " . $_SESSION['admin_id'];
            $operator = $db->getOne($sql);
 
            $smarty->assign('operator', $operator);
            $smarty->assign('ebay_account', $ebay_account);
            $smarty->assign('time_area', $log_txt);
            $smarty->assign('total_order_num', $TotalNumberOfEntries);
            $smarty->assign('new_order_num', $new_order_num);
            $smarty->assign('process_time', $process_time);
            $smarty->assign('shop_name', $_CFG['shop_name']);
            $smarty->assign('send_time', local_date($_CFG['time_format']));
 
            $sql = "SELECT user_name, email FROM " .$ecs->table('erp_admin_user') . " WHERE status = 1";
            $res = $db->query($sql);
            while ($row = $db->fetchRow($res))
            {
                $smarty->assign('user_name',   $row['user_name']);
                $content = $smarty->fetch('str:' . $template['template_content']);
 
                /* 向所有管理员发送邮件 */
                send_mail($row['user_name'], $row['email'], $template['template_subject'], $content, $template['is_html'], 0);
            }
 
            /* 记录日志 */
            admin_log($log_txt, 'sync', 'order');
 
            $links[] = array('text' => sprintf($_LANG['sync_total'], $TotalNumberOfEntries, $new_order_num), 'href' => "erp_ebay_order.php?act=list");
            sys_msg($_LANG['sync_completed'], 0, $links);
        }
    }
}
?>

 

 
支付宝扫码打赏 微信打赏

如果文章对您有帮助,就打赏一个吧

在线客服

客户服务

热线电话:

0755-36656507 15889489919 服务时间:

周一到周六:9:00-18:00

在线QQ客服

在线微信客服

关于我们 常见问题

支付方式 加盟合作

提交需求
优惠红包 购物车0 反馈留言 返回顶部