Esempio n. 1
0
 def search_product(self, cr, uid, ids, product_name = None, start_modified = None, end_modified = None, context=None):
     """
     1) 按商品名称,商品修改时间搜索店铺商品
     2) start_modified、end_modified 都是UTC时间,需要加上8小时传给电商平台
     """
     shop_id = self.browse(cr, uid, ids[0], context= context)
     setDefaultAppInfo(shop_id.appkey, shop_id.appsecret)
     req = ItemsOnsaleGetRequest(shop_id.apiurl, 80)
     req.fields="approve_status,num_iid,title,nick, outer_id, modified"
     if product_name:
         req.q = product_name
     if start_modified:
         start_modified = (datetime.strptime(str(start_modified),'%Y-%m-%d %H:%M:%S',) + timedelta(hours=8)).strftime('%Y-%m-%d %H:%M:%S')
         req.start_modified = start_modified
     if end_modified:
         end_modified = (datetime.strptime(str(end_modified),'%Y-%m-%d %H:%M:%S',) + timedelta(hours=8)).strftime('%Y-%m-%d %H:%M:%S')
         req.end_modified = end_modified
     
     req.page_no = 1
     req.page_size = 100
     total_get = 0
     total_results = 100
     res = []
     while total_get < total_results:
         resp= req.getResponse(shop_id.sessionkey)
         total_results = resp.get('items_onsale_get_response').get('total_results')
         if total_results > 0:
             res += resp.get('items_onsale_get_response').get('items').get('item')
         total_get += req.page_size
         req.page_no = req.page_no + 1
     #
     # 时间需要减去8小时
     for r in res:
         r['modified'] = (datetime.strptime(r['modified'],'%Y-%m-%d %H:%M:%S',) - timedelta(hours=8)).strftime('%Y-%m-%d %H:%M:%S')
     return res
Esempio n. 2
0
 def sync_stock_qty(self, cr, uid, ids, context=None):
     """
      同步本条记录的库存数量到 电商店铺
     """
     port = 80
     res = self.read_group(cr, uid, [('sync_check', '=', True ), ('id', 'in', ids )], ['shop_id',], ['shop_id' ], context = context)
     for r in res:
         shop = self.pool.get('ebiz.shop').browse(cr, uid, r['shop_id'][0], context=context)
         location_id = shop.warehouse_id.lot_stock_id.id
         setDefaultAppInfo(shop.appkey, shop.appsecret)
         
         line_ids = self.search(cr, uid, r['__domain'], context = context)
         prts = self.read(cr, uid, line_ids, ['product_id'], context = context)
         product_ids = [x['product_id'][0] for x in prts]
         context.update({'location': location_id})
         ss = self.pool.get('product.product')._product_available(cr, uid, product_ids, context=context)
         for product in self.pool.get('product.product').browse(cr, uid, product_ids, context=context):
             req = ItemQuantityUpdateRequest(shop.apiurl, port)
             req.num_iid= product.num_iid
             if product.default_code:
                 req.outer_id = product.default_code
             else:
                 req.sku_id = product.sku_id
             qty = product.virtual_available
             if qty < 0: qty = 0
             req.quantity = int(qty)
             req.type=1
             try: 
                resp = req.getResponse(shop.sessionkey)
                ids = self.search(cr, uid, [('shop_id', '=', shop_id), ('product_id', '=', product.id)], context = context )
                self.write(cr, uid, ids, {'sync_date': time.strftime('%Y-%m-%d %H:%M:%S'), 'sync_check': False, 'sync_qty': qty }, context=context)
             except Exception,e:
                #写入 同步异常日志
                 syncerr = u"店铺【%s】商品【[%s]%s】库存数量同步错误: %s" % (shop.name, product.default_code, product.name, e)
                 self.pool.get('ebiz.syncerr').create(cr, uid, {'name':syncerr, 'shop_id': shop.id, 'type': 'stock' }, context = context )
Esempio n. 3
0
 def pull_order(self, cr, uid, ids, tid, context=None):
     """
     1) 取得交易tid信息
     2) ERP中创建交易对应的SO订单
     3) 如果ERP无此买家、发货地址,自动创建对应的Partner对象及联系人
     4) 如果ERP中无此商品,则报同步异常,不同步此tid
     """
     port = 80
     shop = self.browse(cr,uid,ids[0], context = context)
     setDefaultAppInfo(shop.appkey, shop.appsecret)
     try:
         #req = TradeFullinfoGetRequest(shop.apiurl, port)
         req = TradeGetRequest(shop.apiurl, port)
         req.fields="seller_nick,buyer_nick,created,sid,tid,status,buyer_memo,seller_memo,payment,discount_fee,adjust_fee,post_fee,total_fee,pay_time,end_time,modified,received_payment,price,alipay_id,receiver_name,receiver_state,receiver_city,receiver_district,receiver_address,receiver_zip,receiver_mobile,receiver_phone,orders.price,orders.num,orders.iid,orders.num_iid,orders.sku_id,orders.refund_status,orders.status,orders.oid,orders.total_fee,orders.payment,orders.discount_fee,orders.adjust_fee,orders.sku_properties_name,orders.outer_iid,orders.outer_sku_id"
         
         req.tid = long(tid)
         resp = req.getResponse(shop.sessionkey)
         trade =  resp.get('trade_get_response') and resp.get('trade_get_response').get('trade')
         if not trade: return False
         
         trade['created'] = (datetime.strptime(trade['created'], '%Y-%m-%d %H:%M:%S',) - timedelta(hours=8)).strftime('%Y-%m-%d %H:%M:%S')
         trade['pay_time'] = (datetime.strptime(trade['pay_time'], '%Y-%m-%d %H:%M:%S',) - timedelta(hours=8)).strftime('%Y-%m-%d %H:%M:%S')
         #创建Partner
         partner_id, address_id = self.create_partner_address(cr, uid, shop.code, trade, context = context )
         #创建订单及明细行
         order_id = self.create_order(cr, uid, shop, partner_id, address_id, trade, context = context )
         return order_id
     except Exception,e:
        #写入 同步异常日志
         syncerr = u"店铺【%s】订单【%s】同步错误: %s" % (shop.name, tid, e)
         self.pool.get('ebiz.syncerr').create(cr, uid, {'name':syncerr, 'shop_id': shop.id, 'type': 'order' }, context = context )
         return False
Esempio n. 4
0
    def search_invoices(self, cr, uid, ids, date_start = None, date_end = None, context=None):
        """
        从电商店铺搜索一定时间区间创建的、指定交易状态的支付宝对账记录。
        支付类型:
            PAYMENT:在线支付,TRANSFER:转账,DEPOSIT:充值,WITHDRAW:提现,
            CHARGE:收费,PREAUTH:预授权,OTHER:其它。
        """
        port = 80
        shop = self.browse(cr, uid, ids[0], context = context)
        setDefaultAppInfo(shop.appkey, shop.appsecret)
        req = AlipayUserAccountreportGetRequest(shop.apiurl,port)
        req.fields=" balance,memo,alipay_order_no,opt_user_id,merchant_order_no,create_time,self_user_id,business_type,out_amount,type,in_amount"
        if date_start:
            date_start = (datetime.strptime(str(date_start),'%Y-%m-%d %H:%M:%S',) + timedelta(hours=8)).strftime('%Y-%m-%d %H:%M:%S')
            req.start_time = date_start
        if date_end:
            date_end = (datetime.strptime(str(date_end),'%Y-%m-%d %H:%M:%S',) + timedelta(hours=8)).strftime('%Y-%m-%d %H:%M:%S')
            req.end_time = date_end
        
        res = []
        req.page_no = 1
        req.page_size = 100

        total_get = 0
        total_results = 100
        while total_get < total_results:
            resp= req.getResponse(shop.sessionkey)
            trades = resp.get('alipay_user_accountreport_get_response').get('alipay_records', False)
            total_results = resp.get('alipay_user_accountreport_get_response').get('total_results')
            if total_results > 0:
                res += trades.get('alipay_record')
            total_get += req.page_size
            req.page_no = req.page_no + 1

        # 时间需要减去8小时
        for r in res:
            r['create_time'] = (datetime.strptime(r['create_time'],'%Y-%m-%d %H:%M:%S',) - timedelta(hours=8)).strftime('%Y-%m-%d %H:%M:%S')
        #测试数据
        # res = [
        #         {
        #             "balance":"100.00",
        #             "memo":"hello world",
        #             "alipay_order_no":"2014081021001001540010396144",
        #             "opt_user_id":"20880063000888880133",
        #             "merchant_order_no":"T200P765216671818695",
        #             "create_time":"2014-08-20 20:40:03",
        #             "self_user_id":"20880063000888880122",
        #             "business_type":"PAYMENT",
        #             "out_amount":"50.00",
        #             "type":"PAYMENT",
        #             "in_amount":"50.00"
        #         }
        #     ]

        return res
Esempio n. 5
0
 def _order_offline_send(self, cr, uid, shop, tid, logistics_company, logistics_id, context=None):
     setDefaultAppInfo(shop.appkey, shop.appsecret)
     req = LogisticsOfflineSendRequest(shop.apiurl, 80)
     req.tid = tid
     req.out_sid = logistics_id
     req.company_code = logistics_company
     try: 
        resp = req.getResponse(shop.sessionkey)
     except Exception,e:
        #写入 同步异常日志
         syncerr = u"店铺【%s】订单【%s】物流发货同步错误: %s" % (shop.name, tid, e)
         self.pool.get('ebiz.syncerr').create(cr, uid, {'name':syncerr, 'shop_id': shop.id, 'type': 'delivery' }, context = context )
         return False
Esempio n. 6
0
 def _order_signed(self, cr, uid, shop, order):
     #tid 格式为 店铺前缀_电商订单编号,如果是合并订单,则格式为 店铺前缀mg_流水号
     signed = True
     setDefaultAppInfo(shop.appkey, shop.appsecret)
     req = TradeGetRequest(shop.apiurl)
     req.fields="tid, modified, consign_time, status"
     
     i = order.name.find('_')
     if i <= 0:
         signed = False
     tid = order.name[i+1:]
     if order.name[:i].endswith('mg'):  #处理合并订单
         if not order.origin:
             syncerr = u"店铺【%s】订单【%s】买家签收同步错误: 合并订单的源单据中没有原始订单号!" % (shop.name, order.name)
             self.pool.get('ebiz.syncerr').create(cr, uid, {'name':syncerr, 'shop_id': shop.id, 'type': 'invoice' }, context = context )
             signed = False
         tids = order.origin.split(',')
         for t in tids:
             i = t.find('_')
             if i <= 0:
                 signed = False
                 continue
             tid = t[i+1:]
             req.tid = long(tid)
             resp = req.getResponse(shop.sessionkey)
             trade =  resp.get('trade_get_response') and resp.get('trade_get_response').get('trade')
             if not trade or trade['status'] != 'TRADE_FINISHED': 
                 signed = False
                 continue
     else:
         req.tid = long(tid)
         resp = req.getResponse(shop.sessionkey)
         trade =  resp.get('trade_get_response') and resp.get('trade_get_response').get('trade')
         if not trade or trade['status'] != 'TRADE_FINISHED': 
             signed = False
     
     return signed
Esempio n. 7
0
 def import_product(self, cr, uid, ids, product_ids, context=None):
     """
     1) 按商品数字编码,取得商品SKU编码、属性和属性值
     2) 如果该商品没有SKU,且ERP中没有该商品,ERP中直接创建product.product
     3) 如果该商品有SKU,则ERP中创建product.template,且在product.template 上添加 属性和属性值,并且创建该SKU
     4) 电商店铺商品/SKU和ERP产品的对应关系:依次用电商商品/SKU的商家外部编码、商品数字编码、sku_id 匹配ERP产品的default_code, num_iid, sku_id
     5) 返回匹配的产品ids
     """
     port = 80
     shop = self.browse(cr, uid, ids[0], context = context)
     setDefaultAppInfo(shop.appkey, shop.appsecret)
     req = ItemSkusGetRequest(shop.apiurl,port)
     req.fields="sku_id, num_iid, properties, price, status, memo, properties_name, outer_id"
     
     res = []
     for product in product_ids:
         try:
             req.num_iids = product.num_code
             resp= req.getResponse(shop.sessionkey)
             skus = resp.get('item_skus_get_response').get('skus', False)
             product_vals = {
                 'name': product.name,
                 'num_iid': long(product.num_code),
                 'type': 'product',
                 'categ_id': shop.categ_id.id,
                 'default_code': product.out_code or product.num_code,
             }
             if skus and skus.get('sku', False):
                 product_vals.update({'sku': skus.get('sku', False) })
             ids = self.create_product(cr, uid, product_vals, context = context)
             res += ids
         #一个商品的导入异常不中断其他商品的继续导入
         except Exception, e:
            #写入 同步异常日志
             syncerr = u"店铺【%s】商品【num_iid=%s】导入错误: %s" % (shop.name, product.num_code, e)
             self.pool.get('ebiz.syncerr').create(cr, uid, {'name':syncerr, 'shop_id': shop.id, 'type': 'product' }, context = context )
Esempio n. 8
0
    def search_import_orders(self, cr, uid, ids, status = 'WAIT_SELLER_SEND_GOODS', date_start = None, date_end = None, context=None):
        """
        搜索订单,批量导入
        """
        port = 80
        shop = self.browse(cr, uid, ids[0], context = context)
        setDefaultAppInfo(shop.appkey, shop.appsecret)
        req = TradesSoldIncrementGetRequest(shop.apiurl,port)
        req.fields="seller_nick,buyer_nick,created,sid,tid,status,buyer_memo,seller_memo,payment,discount_fee,adjust_fee,post_fee,total_fee,pay_time,end_time,modified,received_payment,price,alipay_id,receiver_name,receiver_state,receiver_city,receiver_district,receiver_address,receiver_zip,receiver_mobile,receiver_phone,orders.price,orders.num,orders.iid,orders.num_iid,orders.sku_id,orders.refund_status,orders.status,orders.oid,orders.total_fee,orders.payment,orders.discount_fee,orders.adjust_fee,orders.sku_properties_name,orders.outer_iid,orders.outer_sku_id"
        req.status = status
        if date_start:
            date_start = (datetime.strptime(str(date_start), '%Y-%m-%d %H:%M:%S',) + timedelta(hours=8)).strftime('%Y-%m-%d %H:%M:%S')
            req.start_modified = date_start
        if date_end:
            date_end = (datetime.strptime(str(date_end), '%Y-%m-%d %H:%M:%S',) + timedelta(hours=8)).strftime('%Y-%m-%d %H:%M:%S')
            req.end_modified = date_end
        
        res = []
        req.page_no = 1
        req.page_size = 100
        
        # 淘宝沙箱环境不支持use_has_next 参数
#        req.use_has_next = True
#        has_next = True
#        while has_next:
#            resp= req.getResponse(shop.sessionkey)
#            trades = resp.get('trades_sold_get_response').get('trades', False)
#            if trades:
#                res += trades.get('trade')
#            req.page_no += 1
#            has_next = resp.get('trades_sold_get_response').get('has_next', False)

        total_get = 0
        total_results = 100
        while total_get < total_results:
            resp= req.getResponse(shop.sessionkey)
            trades = resp.get('trades_sold_increment_get_response').get('trades', False)
            total_results = resp.get('trades_sold_increment_get_response').get('total_results')
            if total_results > 0:
                res += trades.get('trade')
            total_get += req.page_size
            req.page_no = req.page_no + 1

        # 时间需要减去8小时
        # 单号加上店铺前缀
        order_ids = []
        for trade in res:
            trade['created'] = (datetime.strptime(trade['created'], '%Y-%m-%d %H:%M:%S',) - timedelta(hours=8)).strftime('%Y-%m-%d %H:%M:%S')
            trade['pay_time'] = (datetime.strptime(trade['pay_time'], '%Y-%m-%d %H:%M:%S',) - timedelta(hours=8)).strftime('%Y-%m-%d %H:%M:%S')
            trade['sale_code'] = '%s_%s' % (shop.code, trade['tid'])
            try:
                #创建Partner
                partner_id, address_id = self.create_partner_address(cr, uid, shop.code, trade, context = context )
                #创建订单及明细行
                order_id = self.create_order(cr, uid, shop, partner_id, address_id, trade, context = context )
                order_ids.append(order_id)
            except Exception, e:
                #写入 同步异常日志
                syncerr = u"店铺【%s】订单【%s】同步错误: %s" % (shop.name, trade['tid'], e)
                self.pool.get('ebiz.syncerr').create(cr, uid, {'name':syncerr, 'shop_id': shop.id, 'type': 'order' }, context = context )
                continue
Esempio n. 9
0
    def search_orders(self, cr, uid, ids, status = 'WAIT_SELLER_SEND_GOODS', date_start = None, date_end = None, context=None):
        """
        从电商店铺搜索一定时间区间创建的、指定交易状态的订单
        本方法支持的交易状态有:
            WAIT_SELLER_SEND_GOODS (默认)
            WAIT_BUYER_CONFIRM_GOODS
            TRADE_FINISHED
            TRADE_CLOSED

淘宝订单交易状态
WAIT_BUYER_PAY:等待买家付款
WAIT_SELLER_SEND_GOODS:等待卖家发货
SELLER_CONSIGNED_PART:卖家部分发货
WAIT_BUYER_CONFIRM_GOODS:等待买家确认收货
TRADE_BUYER_SIGNED:买家已签收(货到付款专用)
TRADE_FINISHED:交易成功
TRADE_CLOSED:交易关闭
TRADE_CLOSED_BY_TAOBAO:交易被淘宝关闭
TRADE_NO_CREATE_PAY:没有创建外部交易(支付宝交易)
WAIT_PRE_AUTH_CONFIRM:余额宝0元购合约中
PAY_PENDING:外卡支付付款确认中
ALL_WAIT_PAY:所有买家未付款的交易(包含:WAIT_BUYER_PAY、TRADE_NO_CREATE_PAY)
ALL_CLOSED:所有关闭的交易(包含:TRADE_CLOSED、TRADE_CLOSED_BY_TAOBAO)
        """
        port = 80
        shop = self.browse(cr, uid, ids[0], context = context)
        setDefaultAppInfo(shop.appkey, shop.appsecret)
        req = TradesSoldIncrementGetRequest(shop.apiurl,port)
        req.fields="tid, buyer_nick, created, discount_fee, adjust_fee, post_fee, total_fee, pay_time, end_time, modified, consign_time, receiver_name"
        req.status = status
        if date_start:
            date_start = (datetime.strptime(str(date_start), '%Y-%m-%d %H:%M:%S',) + timedelta(hours=8)).strftime('%Y-%m-%d %H:%M:%S')
            req.start_modified = date_start
        if date_end:
            date_end = (datetime.strptime(str(date_end), '%Y-%m-%d %H:%M:%S',) + timedelta(hours=8)).strftime('%Y-%m-%d %H:%M:%S')
            req.end_modified = date_end
        
        res = []
        req.page_no = 1
        req.page_size = 100
        
        # 淘宝沙箱环境不支持use_has_next 参数
#        req.use_has_next = True
#        has_next = True
#        while has_next:
#            resp= req.getResponse(shop.sessionkey)
#            trades = resp.get('trades_sold_get_response').get('trades', False)
#            if trades:
#                res += trades.get('trade')
#            req.page_no += 1
#            has_next = resp.get('trades_sold_get_response').get('has_next', False)

        total_get = 0
        total_results = 100
        while total_get < total_results:
            resp= req.getResponse(shop.sessionkey)
            trades = resp.get('trades_sold_increment_get_response').get('trades', False)
            total_results = resp.get('trades_sold_increment_get_response').get('total_results')
            if total_results > 0:
                res += trades.get('trade')
            total_get += req.page_size
            req.page_no = req.page_no + 1

        # 时间需要减去8小时
        # 单号加上店铺前缀
        for r in res:
            r['created'] = (datetime.strptime(r['created'],'%Y-%m-%d %H:%M:%S',) - timedelta(hours=8)).strftime('%Y-%m-%d %H:%M:%S')
            r['modified'] = (datetime.strptime(r['modified'],'%Y-%m-%d %H:%M:%S',) - timedelta(hours=8)).strftime('%Y-%m-%d %H:%M:%S')
            r['sale_code'] = '%s_%s' % (shop.code, r['tid'])
        return res