Esempio n. 1
0
def create_supplier(vendor_code, dict_obj):

    dict_obj.vendor_code = vendor_code
    ware_account = OutwareAccount.get_fengchao_account()
    try:
        ow_supplier = OutwareSupplier.objects.create(
            outware_account=ware_account,
            vendor_name=dict_obj.vendor_name,
            vendor_code=dict_obj.vendor_code,
            extras={'data': dict(dict_obj)},
            uni_key=OutwareSupplier.generate_unikey(ware_account.id, vendor_code),
        )
    except IntegrityError:
        ow_supplier = OutwareSupplier.objects.get(
            outware_account=ware_account,
            uni_key=OutwareSupplier.generate_unikey(ware_account.id, vendor_code),
        )

    if ow_supplier.is_pushed_ok:
        return {'success': True, 'object': ow_supplier, 'message': '供应商重复创建自动结束' }

    try:
        sdks.request_getway(dict(dict_obj), constants.ACTION_SUPPLIER_CREATE['code'], ware_account)
    except Exception, exc:
        logger.error(str(exc), exc_info=True)
        return {'success': False, 'object': ow_supplier, 'message': str(exc)}
Esempio n. 2
0
def create_inbound_order(inbound_code, vendor_code, dict_obj):
    """ 采购入仓单/销退单创建 """
    if not dict_obj.receiver_info or not dict_obj.order_items:
        return {'success': False, 'object': None, 'message': '缺少收货地址信息/或商品SKU信息'}

    ware_account = OutwareAccount.get_fengchao_account()

    order_type = dict_obj.order_type
    ow_supplier = OutwareSupplier.objects.get(outware_account=ware_account, vendor_code=vendor_code)
    with transaction.atomic():
        try:
            #  TODO@TIPS, use atomic inner for fix django model create bug
            # referl: http://stackoverflow.com/questions/21458387/transactionmanagementerror-you-cant-execute-queries-until-the-end-of-the-atom
            with transaction.atomic():
                ow_inbound = OutwareInboundOrder.objects.create(
                    outware_supplier=ow_supplier,
                    inbound_code=inbound_code,
                    store_code=dict_obj.store_code,
                    order_type=order_type,
                    extras={'data': dict(dict_obj)},
                    uni_key=OutwareInboundOrder.generate_unikey(inbound_code, order_type),
                )
        except IntegrityError:
            ow_inbound = OutwareInboundOrder.objects.get(
                inbound_code=inbound_code,
                order_type=order_type
            )
            if not ow_inbound.is_reproducible:
                return {'success': True, 'object': ow_inbound, 'message': '订单不可重复推送,如果需要修改请先取消'}

            ow_inbound.extras['data'] = dict(dict_obj)
            ow_inbound.save()

        for sku_item in dict_obj.order_items:
            sku_code = sku_item.sku_id
            batch_no = sku_item.batch_code
            ow_sku = OutwareSku.objects.get(outware_supplier=ow_supplier, sku_code=sku_code)
            if ow_sku.is_batch_mgt_on and not batch_no:
                raise Exception(u'SKU商品已启用批次管理,请设置入仓商品的批次号')

            try:
                with transaction.atomic():
                    OutwareInboundSku.objects.create(
                        outware_inboind=ow_inbound,
                        sku_code=sku_code,
                        batch_no=batch_no,
                        push_qty=sku_item.quantity,
                        uni_key=OutwareInboundSku.generate_unikey(ow_inbound.id, sku_code, batch_no),
                    )
            except IntegrityError:
                pass

    # TODO@MERON 入仓单取消后重新创建是否更换inbound_code?
    try:
        sdks.request_getway(dict(dict_obj), constants.ACTION_PO_CREATE['code'], ware_account)
    except Exception, exc:
        logger.error(str(exc), exc_info=True)
        return {'success': False, 'object': ow_inbound, 'message': str(exc)}
Esempio n. 3
0
def union_sku_and_supplier(ow_sku):
    # 创建sku与供应商关联
    ware_account = OutwareAccount.get_fengchao_account()
    ow_supplier  = ow_sku.outware_supplier
    try:
        sdks.request_getway(
            {
                'vendor_name': ow_supplier.vendor_name,
                'vendor_code': ow_supplier.vendor_code,
                'sku_code': ow_sku.sku_code,
                'vdr_provide_barcode': ow_sku.extras.get('vdr_provide_barcode','0')
            },
            constants.ACTION_UNION_SKU_AND_SUPPLIER['code'], ware_account)
    except Exception, exc:
        logger.error(str(exc), exc_info=True)
        return {'success': False, 'object': ow_sku, 'message': str(exc)}
Esempio n. 4
0
def cancel_inbound_order(inbound_code, vendor_code, order_type):
    """ 采购入仓单/销退单取消 """
    ware_account = OutwareAccount.get_fengchao_account()

    ow_supplier = OutwareSupplier.objects.get(outware_account=ware_account, vendor_code=vendor_code)
    ow_inbound = OutwareInboundOrder.objects.get(
        outware_supplier=ow_supplier,
        inbound_code=inbound_code,
        order_type=order_type,
    )

    if ow_inbound.is_action_success(constants.ACTION_PO_CREATE['code']):
        try:
            sdks.request_getway({'order_code': inbound_code}, constants.ACTION_PO_CANCEL['code'], ware_account)
        except Exception, exc:
            logger.error(str(exc), exc_info=True)
            return {'success': False, 'object': ow_inbound, 'message': str(exc)}
Esempio n. 5
0
def cancel_order(order_code):
    # TODO@MERON 2016.3.11 该版本目前只支持转发商城推送订单, 合单拆分后续改进
    ware_account = OutwareAccount.get_fengchao_account()

    ware_order_code = OutwareOrder.format_order_code(
        order_code, prefix=ware_account.order_prefix)
    ow_order = OutwareOrder.objects.get(union_order_code=ware_order_code)
    action_code = constants.ACTION_ORDER_CREATE['code']

    if ow_order.is_action_success(action_code):
        try:
            sdks.request_getway({
                'order_number': ware_order_code,
            }, constants.ACTION_ORDER_CANCEL['code'], ware_account)
        except Exception, exc:
            logger.error(str(exc), exc_info=True)
            return {'success': False, 'object': ow_order, 'message': str(exc)}
Esempio n. 6
0
def create_sku_and_supplier(sku_code, vendor_code, sku_type, declare_type, dict_obj):
    # 包含sku信息以及与供应商的关系

    ware_account = OutwareAccount.get_fengchao_account()
    ow_supplier = OutwareSupplier.objects.get(outware_account=ware_account, vendor_code=vendor_code)
    action_code = constants.ACTION_SKU_CREATE['code']
    is_batch_mgt  = dict_obj.get('is_batch_mgt', False)
    is_expire_mgt = dict_obj.get('is_xpire_mgt', False)
    is_vendor_mgt = dict_obj.get('vdr_flag', False)

    if not getattr(dict_obj, 'bar_code'):
        raise Exception(u"请传入商品条码(多条码请使用逗号分隔)")

    if is_expire_mgt and dict_obj.shelf_life <= 0:
        raise Exception(u"如启动有效期管理请输入正确的有效期天数")

    try:
        with transaction.atomic():
            # for solution of An error occurred in the current transaction.
            # You can't execute queries until the end of the 'atomic' block
            ow_sku = OutwareSku.objects.create(
                outware_supplier=ow_supplier,
                sku_code=sku_code,
                sku_type=sku_type,
                declare_type=declare_type,
                is_batch_mgt=is_batch_mgt,
                is_expire_mgt=is_expire_mgt,
                is_vendor_mgt=is_vendor_mgt,
                extras={'data': dict(dict_obj)},
                uni_key=OutwareSku.generate_unikey(ow_supplier.id, sku_code),
            )
            ow_skustock, state = OutwareSkuStock.objects.get_or_create(sku_code=sku_code)
    except IntegrityError:
        ow_sku = OutwareSku.objects.get(outware_supplier=ow_supplier, sku_code=sku_code)
        ow_skustock, state = OutwareSkuStock.objects.get_or_create(sku_code=sku_code)

        if ow_skustock.is_action_success(action_code):
            action_code = constants.ACTION_SKU_EDIT['code']

        ow_sku.sku_type = sku_type
        ow_sku.declare_type = declare_type
        # is_batch_mgt and is_expire_mgt, if start ,can't be canceled
        if not ow_sku.is_batch_mgt:
            ow_sku.is_batch_mgt =is_batch_mgt
        if not ow_sku.is_expire_mgt:
            ow_sku.is_expire_mgt = is_expire_mgt
        ow_sku.is_vendor_mgt = is_vendor_mgt
        ow_sku.extras['data'] = dict(dict_obj)
        ow_sku.save()

    # TODO创建sku, 是否推送成功消息保存到skustock记录上
    try:
        resp = sdks.request_getway(dict(dict_obj), action_code, ware_account)
    except Exception, exc:
        logger.error(str(exc), exc_info=True)
        return {'success': False, 'object': ow_skustock, 'message': str(exc), 'action_code': action_code}
Esempio n. 7
0
def create_return_order(return_code, whse_code, vdr_code, dict_obj):
    # 包含sku信息以及与供应商的关系
    """ 退仓单 """
    ware_account = OutwareAccount.get_fengchao_account()

    if not dict_obj.receiver_info or not dict_obj.order_items:
        raise Exception('缺少收货地址信息/商品SKU信息:order_no=%s' % return_code)

    action_code = constants.ACTION_PO_RETURN['code']
    order_type   = constants.SOURCE_TYPE_USUAL['code']
    # format order_code
    order_code = dict_obj.order_number = OutwareOrder.format_order_code(
        return_code, prefix=ware_account.order_prefix)
    with transaction.atomic():
        try:
            #  TODO@TIPS, use atomic inner for fix django model create bug
            with transaction.atomic():
                ow_order = OutwareOrder.objects.create(
                    outware_account=ware_account,
                    store_code=whse_code,
                    union_order_code=order_code,
                    order_type=order_type,
                    order_source=constants.ORDER_RETURN['code'],
                    extras={'data': dict(dict_obj)},
                    uni_key=OutwareOrder.generate_unikey(ware_account.id, order_code, order_type),
                )
        except IntegrityError:
            ow_order = OutwareOrder.objects.get(
                outware_account=ware_account,
                union_order_code=order_code,
                order_type=order_type
            )
            if not ow_order.is_reproducible:
                return {'success': True, 'object': ow_order, 'message': '订单不可重复推送'}

            ow_order.extras['data'] = dict(dict_obj)
            ow_order.save()

        for sku_item in dict_obj.order_items:
            sku_order_code = sku_item.sku_order_code
            ow_sku = OutwareSku.objects.filter(sku_code=sku_item.sku_id).first()
            if not ow_sku:
                raise Exception('供应商商品规格信息未录入:sku_code=%s' % sku_item.sku_id)
            try:
                with transaction.atomic():
                    OutwareOrderSku.objects.create(
                        outware_account=ware_account,
                        union_order_code=order_code,
                        origin_skuorder_no=sku_order_code,
                        sku_code=sku_item.sku_id,
                        sku_qty=sku_item.quantity,
                        uni_key=OutwareOrderSku.generate_unikey(ware_account.id, sku_order_code),
                    )
            except IntegrityError:
                ow_ordersku = OutwareOrderSku.objects.get(
                    outware_account=ware_account,
                    origin_skuorder_no=sku_order_code,
                    sku_code=sku_item.sku_id
                )
                if not ow_ordersku.is_reproducible:
                    raise Exception('该SKU订单已存在,请先取消后重新创建:sku_order_code=%s' % sku_order_code)

                ow_ordersku.is_valid = True
                ow_ordersku.union_order_code = order_code
                ow_ordersku.sku_qty = sku_item.quantity
                ow_ordersku.save()

    # create fengchao return order
    try:
        sdks.request_getway(dict(dict_obj), action_code, ware_account)
    except Exception, exc:
        logger.error(str(exc), exc_info=True)
        return {'success': False, 'object': ow_order, 'message': str(exc)}
Esempio n. 8
0
def create_order(order_code, store_code, order_type, dict_obj):
    # TODO@MERON 2016.3.11 该版本目前只支持转发商城推送订单, 合单拆分后续改进
    ware_account = OutwareAccount.get_fengchao_account()

    if not dict_obj.receiver_info or not dict_obj.order_items:
        raise Exception('缺少收货地址信息/商品SKU信息:order_no=%s' % order_code)

    action_code = constants.ACTION_ORDER_CREATE['code']
    if order_type == constants.SOURCE_TYPE_CROSSBOADER['code']:
        action_code = constants.ACTION_CROSSORDER_CREATE['code']
        if not all([
                dict_obj.declare_type, dict_obj.order_person_idname,
                dict_obj.order_person_idcard
        ]):
            raise Exception('跨境订单需要传入报关方式以及用户身份信息:order_no=%s' % order_code)
    else:
        dict_obj.order_type = order_type
    # format order_code
    order_code = dict_obj.order_number = OutwareOrder.format_order_code(
        order_code, prefix=ware_account.order_prefix)
    with transaction.atomic():
        try:
            # TODO@TIPS, use atomic inner for fix django model create bug
            with transaction.atomic():
                ow_order = OutwareOrder.objects.create(
                    outware_account=ware_account,
                    store_code=store_code,
                    union_order_code=order_code,
                    order_type=order_type,
                    order_source=constants.ORDER_SALE['code'],
                    extras={'data': dict(dict_obj)},
                    uni_key=OutwareOrder.generate_unikey(
                        ware_account.id, order_code, order_type),
                )
        except IntegrityError:
            ow_order = OutwareOrder.objects.get(outware_account=ware_account,
                                                union_order_code=order_code,
                                                order_type=order_type)
            if not ow_order.is_reproducible:
                return {
                    'success': True,
                    'object': ow_order,
                    'message': '订单不可重复推送'
                }

            ow_order.extras['data'] = dict(dict_obj)
            ow_order.save()

        for sku_item in dict_obj.order_items:
            sku_order_code = sku_item.sku_order_code
            ow_sku = OutwareSku.objects.filter(
                sku_code=sku_item.sku_id).first()
            if not ow_sku:
                raise Exception('供应商商品规格信息未录入:sku_code=%s' % sku_item.sku_id)
            try:
                with transaction.atomic():
                    OutwareOrderSku.objects.create(
                        outware_account=ware_account,
                        union_order_code=order_code,
                        origin_skuorder_no=sku_order_code,
                        sku_code=sku_item.sku_id,
                        sku_qty=sku_item.quantity,
                        uni_key=OutwareOrderSku.generate_unikey(
                            ware_account.id, sku_order_code),
                    )
            except IntegrityError:
                ow_ordersku = OutwareOrderSku.objects.get(
                    outware_account=ware_account,
                    origin_skuorder_no=sku_order_code,
                    sku_code=sku_item.sku_id)
                if not ow_ordersku.is_reproducible:
                    raise Exception('该SKU订单已存在,请先取消后重新创建:sku_order_code=%s' %
                                    sku_order_code)

                ow_ordersku.is_valid = True
                ow_ordersku.union_order_code = order_code
                ow_ordersku.sku_qty = sku_item.quantity
                ow_ordersku.save()

    # create fengchao order
    try:
        sdks.request_getway(dict(dict_obj), action_code, ware_account)
    except Exception, exc:
        logger.error(str(exc), exc_info=True)
        return {'success': False, 'object': ow_order, 'message': str(exc)}