Beispiel #1
0
 def refund_coupon_template(self):
     # type: () -> Optional[CouponTemplate]
     refund_coupon = self.amount_flow.get('refund_coupon')
     if refund_coupon:
         template_id = refund_coupon.get('template_id')
         from flashsale.coupon.apis.v1.coupontemplate import get_coupon_template_by_id
         return get_coupon_template_by_id(template_id)
     return None
Beispiel #2
0
def task_ordercarry_update_carryrecord(carry):
    if carry.mama_id <= 0:
        return

    from flashsale.xiaolumm.models import OrderCarry
    if carry.status == OrderCarry.STAGING:
        carry_record_status = CarryRecord.PENDING
    elif carry.status == OrderCarry.ESTIMATE:
        carry_record_status = CarryRecord.PENDING
    elif carry.status == OrderCarry.CONFIRM:
        carry_record_status = CarryRecord.CONFIRMED
    elif carry.status == OrderCarry.CANCEL:
        carry_record_status = CarryRecord.CANCEL

    record = CarryRecord.objects.filter(uni_key=carry.uni_key).first()
    if record:
        from flashsale.pay.models.trade import SaleOrder
        from flashsale.pay.models.product import ModelProduct
        from flashsale.xiaolumm.models import XiaoluMama
        sale_order = SaleOrder.objects.filter(oid=carry.order_id).first()
        from shopback.items.models import Product
        products = Product.objects.filter(id=sale_order.item_id)
        product = products[0]
        model_product = product.get_product_model()
        from flashsale.pay.apis.v1.product import get_virtual_modelproduct_from_boutique_modelproduct
        coupon_mp = get_virtual_modelproduct_from_boutique_modelproduct(product.model_id)

        if carry_record_status == CarryRecord.CONFIRMED:
            if record.status != carry_record_status:
                record.confirm()
            # give elite score
            if model_product and coupon_mp and coupon_mp.products[0].elite_score > 0 and sale_order.payment > 0 and (model_product.is_boutique_product or model_product.product_type == ModelProduct.USUAL_TYPE):
                    from flashsale.coupon.apis.v1.transfer import create_present_elite_score
                    from flashsale.coupon.apis.v1.coupontemplate import get_coupon_template_by_id
                    upper_mama = XiaoluMama.objects.filter(id=carry.mama_id,
                                                           status=XiaoluMama.EFFECT,
                                                           charge_status=XiaoluMama.CHARGED).first()
                    template = get_coupon_template_by_id(id=374)
                    customer = upper_mama.get_mama_customer()
                    if upper_mama.elite_score < 2000:
                        transfer_in = create_present_elite_score(customer, int(round(coupon_mp.products[0].elite_score * (sale_order.payment / sale_order.price))), template, None, carry.order_id)

        if carry_record_status == CarryRecord.CANCEL:
            if record.status != carry_record_status:
                record.cancel()
            # cancel elite score
            if model_product and coupon_mp and coupon_mp.products[0].elite_score > 0 and sale_order.payment > 0 and (model_product.is_boutique_product or model_product.product_type == ModelProduct.USUAL_TYPE):
                from flashsale.coupon.models.transfer_coupon import CouponTransferRecord
                upper_mama = XiaoluMama.objects.filter(id=carry.mama_id,
                                                       status=XiaoluMama.EFFECT,
                                                       charge_status=XiaoluMama.CHARGED).first()
                if not upper_mama:
                    return 
                customer = upper_mama.get_mama_customer()
                uni_key_in = "elite_in-%s-%s" % (customer.id, carry.order_id)
                cts = CouponTransferRecord.objects.filter(uni_key=uni_key_in).first()
                if cts:
                    cts.transfer_status = CouponTransferRecord.CANCELED
                    cts.save()
    else:
        if carry.carry_type != OrderCarry.ADVANCED_MAMA_REFERAL_ORDER:
            try:
                CarryRecord.create(carry.mama_id, carry.carry_num, CarryRecord.CR_ORDER, carry.carry_description,
                           uni_key=carry.uni_key,status=carry_record_status)
            except IntegrityError:
                pass
Beispiel #3
0
def double_mama_score():
    from flashsale.coupon.models.transfer_coupon import CouponTransferRecord
    from_time = datetime.datetime(2017, 2, 8, 23, 0, 0, 0)
    to_time = datetime.datetime(2017, 2, 8, 23, 30, 0, 0)
    has_add = CouponTransferRecord.objects.filter(
        transfer_status=CouponTransferRecord.DELIVERED,
        transfer_type__in=[CouponTransferRecord.IN_GIFT_COUPON],
        created__range=(from_time, to_time)
    )

    has_add_mms = [p['coupon_to_mama_id'] for p in has_add.values('coupon_to_mama_id')]

    from flashsale.xiaolumm.models import XiaoluMama
    mamas = XiaoluMama.objects.filter(referal_from__in=[XiaoluMama.DIRECT, XiaoluMama.INDIRECT], status=XiaoluMama.EFFECT,
                                      charge_status=XiaoluMama.CHARGED, elite_score__gt=0)
    for mama in mamas.iterator():
        origin_score = mama.elite_score
        score = mama.elite_score
        if mama.id in has_add_mms:
            gift_ct = CouponTransferRecord.objects.filter(
                coupon_to_mama_id=mama.id,
                transfer_status=CouponTransferRecord.DELIVERED,
                transfer_type__in=[CouponTransferRecord.IN_GIFT_COUPON],
                created__range=(from_time, to_time)
            ).first()
            if mama.elite_score >= 600 and mama.elite_score < 2000:
                score = 600 - gift_ct.elite_score + 300
            elif mama.elite_score >= 2000 and mama.elite_score < 6000:
                score = 2000 - gift_ct.elite_score + 1000
            elif mama.elite_score >= 6000 and mama.elite_score < 20000:
                score = 6000 - gift_ct.elite_score + 3000
            elif mama.elite_score >= 20000:
                score = 20000 - gift_ct.elite_score + 10000
        else:
            if score >= 600 and score < 1000:
                score += 300
            elif score >= 2000 and score < 3000:
                score += 1000
            elif score >= 6000 and score < 10000:
                score += 3000

        if mama.elite_score != score:
            mama.elite_score = score
            mama.save()
            from core.options import log_action, CHANGE, ADDITION, get_systemoa_user
            sys_oa = get_systemoa_user()
            log_action(sys_oa, mama, CHANGE, u'0208升级分数翻倍修改用户积分从%s到%s' % (origin_score, score))

        try:
            from flashsale.coupon.apis.v1.transfer import create_present_elite_score
            from flashsale.coupon.apis.v1.coupontemplate import get_coupon_template_by_id
            from flashsale.pay.apis.v1.customer import get_customer_by_id

            if score > origin_score:
                template = get_coupon_template_by_id(id=374)
                customer = get_customer_by_id(mama.customer_id)
                transfer_in = create_present_elite_score(customer, int(score - origin_score), template, 1)
                log_action(sys_oa, transfer_in, ADDITION, '0208升级分数翻倍赠送积分: 赠送')
                # log_action(sys_oa, transfer_out, ADDITION, '0208升级分数翻倍赠送积分: 消耗')
        except Exception as e:
            pass
def check_xlmm_carry_record(recent_days):
    results = []
    err_num = 0
    import datetime
    from flashsale.xiaolumm.models import OrderCarry, CarryRecord
    from flashsale.coupon.models import CouponTransferRecord
    tt = datetime.datetime.now()
    tf = tt - datetime.timedelta(days=recent_days)
    queryset = OrderCarry.objects.filter(created__gte=tf)
    for carry in queryset.iterator():
        if carry.carry_type == OrderCarry.ADVANCED_MAMA_REFERAL_ORDER:
            continue
        if carry.status == OrderCarry.STAGING:
            carry_record_status = CarryRecord.PENDING
        elif carry.status == OrderCarry.ESTIMATE:
            carry_record_status = CarryRecord.PENDING
        elif carry.status == OrderCarry.CONFIRM:
            carry_record_status = CarryRecord.CONFIRMED
        elif carry.status == OrderCarry.CANCEL:
            carry_record_status = CarryRecord.CANCEL
        record = CarryRecord.objects.filter(uni_key=carry.uni_key).first()
        if record:
            if record.status != carry_record_status:
                if carry_record_status == CarryRecord.CONFIRMED:
                    record.confirm()
                if carry_record_status == CarryRecord.CANCEL:
                    record.cancel()
            if record.carry_num != carry.carry_num:
                err_num += 1
                results.append(record.id)
            from flashsale.pay.models.trade import SaleOrder
            from flashsale.pay.models.product import ModelProduct
            from flashsale.xiaolumm.models import XiaoluMama
            sale_order = SaleOrder.objects.filter(oid=carry.order_id).first()
            from shopback.items.models import Product
            products = Product.objects.filter(id=sale_order.item_id)
            product = products[0]
            model_product = product.get_product_model()
            from flashsale.pay.apis.v1.product import get_virtual_modelproduct_from_boutique_modelproduct
            coupon_mp = get_virtual_modelproduct_from_boutique_modelproduct(
                product.model_id)
            if carry_record_status == CarryRecord.CONFIRMED:
                if model_product and coupon_mp and coupon_mp.products[
                        0].elite_score > 0 and sale_order.payment > 0 and (
                            model_product.is_boutique_product
                            or model_product.product_type
                            == ModelProduct.USUAL_TYPE):
                    from flashsale.coupon.apis.v1.transfer import create_present_elite_score
                    from flashsale.coupon.apis.v1.coupontemplate import get_coupon_template_by_id
                    upper_mama = XiaoluMama.objects.filter(
                        id=carry.mama_id,
                        status=XiaoluMama.EFFECT,
                        charge_status=XiaoluMama.CHARGED).first()
                    if not upper_mama:
                        continue
                    template = get_coupon_template_by_id(id=374)
                    customer = upper_mama.get_mama_customer()
                    uni_key_in = "elite_in-%s-%s" % (customer.id,
                                                     carry.order_id)
                    cts = CouponTransferRecord.objects.filter(
                        uni_key=uni_key_in).first()
                    if not cts:
                        transfer_in = create_present_elite_score(
                            customer,
                            int(
                                round(
                                    coupon_mp.products[0].elite_score *
                                    (sale_order.payment / sale_order.price))),
                            template, None, carry.order_id)
                if record.carry_num > 0:
                    referal_id = 'carryrecord-%s' % record.id
                    from flashsale.pay.models import BudgetLog
                    bg = BudgetLog.objects.filter(
                        referal_id=referal_id).first()
                    if bg:
                        if bg.flow_amount != carry.carry_num:
                            err_num += 1
                            results.append(record.id)
                    else:
                        err_num += 1
                        results.append(record.id)
            if carry_record_status == CarryRecord.CANCEL:
                if model_product and product.elite_score > 0 and carry.carry_num > 0 and (
                        model_product.is_boutique_product or
                        model_product.product_type == ModelProduct.USUAL_TYPE):
                    from flashsale.coupon.models.transfer_coupon import CouponTransferRecord
                    upper_mama = XiaoluMama.objects.filter(
                        id=carry.mama_id,
                        status=XiaoluMama.EFFECT,
                        charge_status=XiaoluMama.CHARGED).first()
                    customer = upper_mama.get_mama_customer()
                    uni_key_in = "elite_in-%s-%s" % (customer.id,
                                                     carry.order_id)
                    cts = CouponTransferRecord.objects.filter(
                        uni_key=uni_key_in).first()
                    if cts and cts.transfer_status != CouponTransferRecord.CANCELED:
                        cts.transfer_status = CouponTransferRecord.CANCELED
                        cts.save()
        else:
            carry.save()
    return results
def task_auto_exchg_xlmm_order():
    import datetime
    tt = datetime.datetime.now()
    tf = tt - datetime.timedelta(days=2)
    from flashsale.pay.models.trade import SaleOrder, SaleTrade, Customer
    from flashsale.xiaolumm.models import OrderCarry, XiaoluMama, ExchangeSaleOrder
    from flashsale.pay.models import ModelProduct
    exchg_orders = OrderCarry.objects.filter(
        carry_type=OrderCarry.REFERAL_ORDER,
        status__in=[OrderCarry.CONFIRM],
        created__range=(datetime.date(2017, 2, 1), tf))
    unexchg_coupon_num = 0
    autoexchg_coupon_num = 0
    if exchg_orders.exists():
        for entry in exchg_orders.iterator():
            sale_order = SaleOrder.objects.filter(oid=entry.order_id).first()
            exchg_sale_order = ExchangeSaleOrder.objects.filter(
                order_oid=entry.order_id).first()
            if not sale_order:
                continue
            if sale_order.extras.has_key('exchange') or (
                    exchg_sale_order and exchg_sale_order.has_exchanged):
                continue
            model_product = ModelProduct.objects.filter(
                id=sale_order.item_product.model_id,
                is_boutique=True,
                product_type=ModelProduct.VIRTUAL_TYPE).first()
            if model_product:
                from flashsale.pay.apis.v1.order import get_pay_type_from_trade
                if round(sale_order.payment / sale_order.price
                         ) > 0 and model_product.extras.has_key('template_id'):
                    unexchg_coupon_num += round(sale_order.payment /
                                                sale_order.price)
                    from flashsale.xiaolumm.apis.v1.xiaolumama import get_mama_by_id
                    level1_mama = get_mama_by_id(entry.contributor_id)
                    level2_mama = get_mama_by_id(entry.mama_id)
                    if level2_mama:
                        level3_mama = level2_mama.get_referal_from_mama()
                    if level1_mama and level2_mama \
                            and level1_mama.get_level_lowest_elite() >= level2_mama.get_level_lowest_elite():
                        from flashsale.coupon.apis.v1.transfer import create_present_elite_score, get_elite_score_by_templateid
                        from flashsale.coupon.apis.v1.coupontemplate import get_coupon_template_by_id
                        autoexchg_coupon_num += round(sale_order.payment /
                                                      sale_order.price)
                        customer = level2_mama.get_customer()
                        template = get_coupon_template_by_id(
                            model_product.extras['template_id'])
                        product_id, elite_score, agent_price = get_elite_score_by_templateid(
                            template.id, level2_mama)
                        elite_score *= round(sale_order.payment /
                                             sale_order.price)
                        uni_key_prefix = "autoexchg-%s" % (sale_order.id)
                        try:
                            create_present_elite_score(customer, elite_score,
                                                       template, '',
                                                       uni_key_prefix)
                        except IntegrityError:
                            logger.info({
                                'message':
                                u'task_auto_exchg_xlmm_order integrity err | oid=%s'
                                % (sale_order.oid),
                            })
                        if level2_mama.referal_from == XiaoluMama.INDIRECT and level3_mama and level3_mama.is_elite_mama:
                            entry.mama_id = level3_mama.id
                            entry.save(update_fields=['mama_id'])
                            from core.options import log_action, CHANGE, ADDITION, get_systemoa_user
                            sys_oa = get_systemoa_user()
                            log_action(
                                sys_oa, entry, CHANGE,
                                u'auto exchange ordercarry=%s,so=%s,level1 %s >= level2 %s,level2 %s to level3 %s'
                                % (entry.id, sale_order.oid,
                                   level1_mama.get_level_lowest_elite(),
                                   level2_mama.get_level_lowest_elite(),
                                   level2_mama.id, level3_mama.id))
                            # print 'add level3', sale_order.oid, level1_mama.id, level2_mama.id, level3_mama.id
                        else:
                            exchg_sale_order = ExchangeSaleOrder.objects.filter(
                                order_oid=entry.order_id).first()
                            if not exchg_sale_order:
                                exchg_record = ExchangeSaleOrder(
                                    order_oid=entry.order_id,
                                    has_exchanged=True,
                                    exchg_type=1,
                                    uni_key=entry.order_id)
                                exchg_record.save()
                            else:
                                exchg_sale_order.has_exchanged = True
                                exchg_sale_order.exchg_type = 1
                                exchg_sale_order.save()
                                from core.options import log_action, CHANGE, ADDITION, get_systemoa_user
                                sys_oa = get_systemoa_user()
                                log_action(
                                    sys_oa, exchg_sale_order, CHANGE,
                                    u'auto exchange ordercarry=%s,level1 %s >= level2 %s,level2 %s, level3 none or not elite, so %s exchg finish'
                                    % (entry.id,
                                       level1_mama.get_level_lowest_elite(),
                                       level2_mama.get_level_lowest_elite(),
                                       level2_mama.id, sale_order.oid))
                            # print 'chg so', sale_order.oid, level1_mama.id, level2_mama.id
        logger.info({
            'message':
            u'task_auto_exchg_xlmm_order | mama unexchg_coupon_num=%s autoexchg_coupon_num=%s'
            % (unexchg_coupon_num, autoexchg_coupon_num),
        })