Ejemplo n.º 1
0
def set_deal_number(user: Operator, deal_id):
    deal = BW.get_deal(deal_id)

    if not deal:
        return BW.BW_NO_SUCH_DEAL

    user.deal_data.deal_id = deal_id
    user.deal_data.stage = deal.get(DEAL_STAGE_ALIAS)

    if user.deal_data.stage not in (BFM.DEAL_PROCESSED_WAITING_FOR_SUPPLY_STATUS_ID, BFM.DEAL_PRINTED_STATUS_ID,
                                    BFM.DEAL_PROCESSED_ON_HOLD_STATUS_ID, BFM.DEAL_PAID_PREPAID_STATUS_ID):
        return BW.BW_WRONG_STAGE

    user.deal_data.florist_id = deal.get(DEAL_FLORIST_NEW_ALIAS)

    user.deal_data.order = Utils.prepare_external_field(deal, DEAL_ORDER_ALIAS)
    contact_id = deal.get(DEAL_CONTACT_ALIAS)

    contact_data = BW.get_contact_data(contact_id)
    contact_name = contact_data.get(CONTACT_USER_NAME_ALIAS)
    contact_phone = contact_data.get(CONTACT_PHONE_ALIAS)

    user.deal_data.contact = contact_name + ' ' + contact_phone
    user.deal_data.florist = Utils.prepare_external_field(BW.FLORISTS, user.deal_data.florist_id, BW.FLORISTS_LOCK)

    order_received_by_id = deal.get(DEAL_ORDER_RECEIVED_BY_ALIAS)
    user.deal_data.order_received_by = Utils.prepare_external_field(BW.BITRIX_IDS_USERS, order_received_by_id,
                                                                    BW.BITRIX_USERS_LOCK)

    user.deal_data.total_sum = Utils.prepare_external_field(deal, DEAL_TOTAL_SUM_ALIAS)

    payment_type_id = Utils.prepare_external_field(deal, DEAL_PAYMENT_TYPE_ALIAS)
    user.deal_data.payment_type = Utils.prepare_external_field(BW.PAYMENT_TYPES, payment_type_id, BW.PAYMENT_TYPES_LOCK)

    payment_method_id = Utils.prepare_external_field(deal, DEAL_PAYMENT_METHOD_ALIAS)
    user.deal_data.payment_method = Utils.prepare_external_field(BW.PAYMENT_METHODS, payment_method_id,
                                                                 BW.PAYMENT_METHODS_LOCK)

    user.deal_data.payment_status = Utils.prepare_external_field(deal, DEAL_PAYMENT_STATUS_ALIAS)

    user.deal_data.prepaid = Utils.prepare_external_field(deal, DEAL_PREPAID_ALIAS)
    user.deal_data.to_pay = Utils.prepare_external_field(deal, DEAL_TO_PAY_ALIAS)

    courier_id = Utils.prepare_external_field(deal, DEAL_COURIER_NEW_ALIAS)
    user.deal_data.courier = Utils.prepare_external_field(BW.COURIERS, courier_id, BW.COURIERS_LOCK)

    order_type_id = Utils.prepare_external_field(deal, DEAL_ORDER_TYPE_ALIAS)
    user.deal_data.order_type = Utils.prepare_external_field(BW.ORDERS_TYPES, order_type_id,
                                                             BW.ORDERS_TYPES_LOCK)

    user.deal_data.order_comment = Utils.prepare_external_field(deal, DEAL_ORDER_COMMENT_ALIAS)
    user.deal_data.delivery_comment = Utils.prepare_external_field(deal, DEAL_DELIVERY_COMMENT_ALIAS)
    user.deal_data.incognito = Utils.prepare_deal_incognito_bot_view(deal, DEAL_INCOGNITO_ALIAS)

    if user.deal_data.florist_id:
        return BH_ALREADY_HAS_FLORIST

    return BW.BW_OK
def festive_reapprove(update: Update, context, user: BaseUser):
    deal_id = context.match.group(1)
    deal = BW.get_deal(deal_id)

    stage_id = deal.get(BFA.DEAL_STAGE_ALIAS)
    deal_stage = Utils.prepare_external_field(BW.STAGES, stage_id, BW.STAGES_LOCK)

    deal_order = Utils.prepare_external_field(deal, BFA.DEAL_ORDER_ALIAS)

    deal_date = Utils.prepare_deal_date(deal, BFA.DEAL_DATE_ALIAS)
    deal_time = Utils.prepare_deal_time(deal, BFA.DEAL_TIME_ALIAS)
    deal_sum = Utils.prepare_external_field(deal, BFA.DEAL_TOTAL_SUM_ALIAS)

    order_received_by_id = deal.get(BFA.DEAL_ORDER_RECEIVED_BY_ALIAS)
    deal_accepted = Utils.prepare_external_field(BW.BITRIX_IDS_USERS, order_received_by_id,
                                                 BW.BITRIX_USERS_LOCK)

    source_id = Utils.prepare_external_field(deal, BFA.DEAL_SOURCE_ID_ALIAS)
    deal_source = Utils.prepare_external_field(BW.SOURCES, source_id, BW.SOURCES_LOCK)

    contact_id = Utils.prepare_external_field(deal, BFA.DEAL_CONTACT_ALIAS)

    contact_data = BW.get_contact_data(contact_id)
    contact_name = contact_data.get(BFA.CONTACT_USER_NAME_ALIAS)
    contact_phone = contact_data.get(BFA.CONTACT_PHONE_ALIAS)
    deal_contact = contact_name + ' ' + contact_phone

    subdivision_id = Utils.prepare_external_field(deal, BFA.DEAL_SUBDIVISION_ALIAS)
    deal_subdivision = Utils.prepare_external_field(BW.SUBDIVISIONS, subdivision_id, BW.SUBDIVISIONS_LOCK)

    deal_has_reserve = Utils.prepare_external_field(deal, BFA.DEAL_ORDER_HAS_RESERVE_ALIAS)
    deal_reserve_desc = Utils.prepare_external_field(deal, BFA.DEAL_ORDER_RESERVE_DESC_ALIAS)

    deal_delivery_type = Utils.prepare_deal_supply_method(deal, BFA.DEAL_SUPPLY_METHOD_ALIAS)

    district_id = Utils.prepare_external_field(deal, BFA.DEAL_DISTRICT_ALIAS)
    deal_district = Utils.prepare_external_field(BW.DISTRICTS, district_id, BW.DISTRICTS_LOCK)

    address, location = Utils.prepare_deal_address(deal, BFA.DEAL_ADDRESS_ALIAS)
    deal_address = address

    deal_delivery_comment = Utils.prepare_external_field(deal, BFA.DEAL_DELIVERY_COMMENT_ALIAS)

    payment_type_id = Utils.prepare_external_field(deal, BFA.DEAL_PAYMENT_TYPE_ALIAS)
    deal_pay_type = Utils.prepare_external_field(BW.PAYMENT_TYPES, payment_type_id, BW.PAYMENT_TYPES_LOCK)

    payment_method_id = Utils.prepare_external_field(deal, BFA.DEAL_PAYMENT_METHOD_ALIAS)
    deal_pay_method = Utils.prepare_external_field(BW.PAYMENT_METHODS, payment_method_id,
                                                   BW.PAYMENT_METHODS_LOCK)

    deal_prepaid = Utils.prepare_external_field(deal, BFA.DEAL_PREPAID_ALIAS)

    deal_terminal = Utils.prepare_external_field(BW.DEAL_TERMINAL_CHANGE_MAPPING,
                                                 Utils.prepare_external_field(deal, BFA.DEAL_TERMINAL_CHANGE_ALIAS))
    deal_change = Utils.prepare_external_field(deal, BFA.DEAL_CHANGE_SUM_ALIAS)

    deal_to_pay = Utils.prepare_external_field(deal, BFA.DEAL_TO_PAY_ALIAS)

    deal_pay_status = Utils.prepare_external_field(deal, BFA.DEAL_PAYMENT_STATUS_ALIAS)

    if deal_has_reserve == BFM.DEAL_HAS_RESERVE_YES:
        with BW.BITRIX_USERS_LOCK:
            access_token = context.bot_data[cfg.BOT_ACCESS_TOKEN_PERSISTENT_KEY]
            photo_urls = BW.process_deal_photo_dl_urls(deal, access_token, (BFA.DEAL_ORDER_RESERVE_ALIAS,))
    else:
        photo_urls = None

    BH.reapprove_deal(deal_id)
    HttpJobs.send_festive_deal_message(context.bot, deal_id, deal_stage, deal_order,
                                       deal_date, deal_time, deal_sum, deal_accepted,
                                       deal_source, deal_contact, deal_subdivision, deal_reserve_desc,
                                       deal_delivery_type, deal_district, deal_address, deal_delivery_comment,
                                       deal_pay_method, deal_pay_type, deal_prepaid, deal_terminal, deal_change,
                                       deal_to_pay, deal_pay_status,
                                       photo_urls)

    edited_msg = re.sub(re.escape(Txt.DECLINED_HEADER.format(deal_id)), Txt.REAPPROVED_HEADER.format(deal_id),
                        update.effective_message.text_markdown_v2)

    TgCommons.edit_mdv2(update.effective_message, msg_text=edited_msg,
                        need_cancel=False)
Ejemplo n.º 3
0
def get_info(context, deal_id, user: BaseUser):
    deal = BW.get_deal(deal_id)

    user.festive_data.deal_link = Utils.prepare_external_field(
        deal, BFA.DEAL_LINK_ALIAS)
    user.festive_data.deal_order = Utils.prepare_external_field(
        deal, BFA.DEAL_ORDER_ALIAS)

    user.festive_data.deal_user_declined = Utils.prepare_external_field(
        BW.BITRIX_IDS_USERS, user.bitrix_user_id, BW.BITRIX_USERS_LOCK)

    subdiv_id = deal.get(BFA.DEAL_SUBDIVISION_ALIAS)
    subdiv_name = Utils.prepare_external_field(BW.SUBDIVISIONS,
                                               subdiv_id,
                                               BW.SUBDIVISIONS_LOCK,
                                               escape_md=False)
    user.festive_data.subdiv_chat_id = creds.FESTIVE_UNAPPROVED_SUBDIVS.get(
        subdiv_name)

    user.festive_data.deal_date = Utils.prepare_deal_date(
        deal, BFA.DEAL_DATE_ALIAS)
    user.festive_data.deal_time = Utils.prepare_deal_time(
        deal, BFA.DEAL_TIME_ALIAS)
    user.festive_data.deal_sum = Utils.prepare_external_field(
        deal, BFA.DEAL_TOTAL_SUM_ALIAS)

    order_received_by_id = deal.get(BFA.DEAL_ORDER_RECEIVED_BY_ALIAS)
    user.festive_data.deal_accepted = Utils.prepare_external_field(
        BW.BITRIX_IDS_USERS, order_received_by_id, BW.BITRIX_USERS_LOCK)

    source_id = Utils.prepare_external_field(deal, BFA.DEAL_SOURCE_ID_ALIAS)
    user.festive_data.deal_source = Utils.prepare_external_field(
        BW.SOURCES, source_id, BW.SOURCES_LOCK)

    contact_id = Utils.prepare_external_field(deal, BFA.DEAL_CONTACT_ALIAS)

    contact_data = BW.get_contact_data(contact_id)
    contact_name = contact_data.get(BFA.CONTACT_USER_NAME_ALIAS)
    contact_phone = contact_data.get(BFA.CONTACT_PHONE_ALIAS)
    user.festive_data.deal_contact = contact_name + ' ' + contact_phone

    subdivision_id = Utils.prepare_external_field(deal,
                                                  BFA.DEAL_SUBDIVISION_ALIAS)
    user.festive_data.deal_subdivision = Utils.prepare_external_field(
        BW.SUBDIVISIONS, subdivision_id, BW.SUBDIVISIONS_LOCK)

    user.festive_data.deal_has_reserve = Utils.prepare_external_field(
        deal, BFA.DEAL_ORDER_HAS_RESERVE_ALIAS)
    user.festive_data.deal_reserve_desc = Utils.prepare_external_field(
        deal, BFA.DEAL_ORDER_RESERVE_DESC_ALIAS)

    user.festive_data.deal_delivery_type = Utils.prepare_deal_supply_method(
        deal, BFA.DEAL_SUPPLY_METHOD_ALIAS)

    district_id = Utils.prepare_external_field(deal, BFA.DEAL_DISTRICT_ALIAS)
    user.festive_data.deal_district = Utils.prepare_external_field(
        BW.DISTRICTS, district_id, BW.DISTRICTS_LOCK)

    address, location = Utils.prepare_deal_address(deal,
                                                   BFA.DEAL_ADDRESS_ALIAS)
    user.festive_data.deal_address = address

    user.festive_data.deal_delivery_comment = Utils.prepare_external_field(
        deal, BFA.DEAL_DELIVERY_COMMENT_ALIAS)

    payment_type_id = Utils.prepare_external_field(deal,
                                                   BFA.DEAL_PAYMENT_TYPE_ALIAS)
    user.festive_data.deal_pay_type = Utils.prepare_external_field(
        BW.PAYMENT_TYPES, payment_type_id, BW.PAYMENT_TYPES_LOCK)

    payment_method_id = Utils.prepare_external_field(
        deal, BFA.DEAL_PAYMENT_METHOD_ALIAS)
    user.festive_data.deal_pay_method = Utils.prepare_external_field(
        BW.PAYMENT_METHODS, payment_method_id, BW.PAYMENT_METHODS_LOCK)

    user.festive_data.deal_prepaid = Utils.prepare_external_field(
        deal, BFA.DEAL_PREPAID_ALIAS)

    user.festive_data.deal_terminal = Utils.prepare_external_field(
        BW.DEAL_TERMINAL_CHANGE_MAPPING,
        Utils.prepare_external_field(deal, BFA.DEAL_TERMINAL_CHANGE_ALIAS))
    user.festive_data.deal_change = Utils.prepare_external_field(
        deal, BFA.DEAL_CHANGE_SUM_ALIAS)

    user.festive_data.deal_to_pay = Utils.prepare_external_field(
        deal, BFA.DEAL_TO_PAY_ALIAS)

    user.festive_data.deal_pay_status = Utils.prepare_external_field(
        deal, BFA.DEAL_PAYMENT_STATUS_ALIAS)

    if user.festive_data.deal_has_reserve == BFM.DEAL_HAS_RESERVE_YES:
        with BW.BITRIX_USERS_LOCK:
            access_token = context.bot_data[
                cfg.BOT_ACCESS_TOKEN_PERSISTENT_KEY]
            user.festive_data.photo_urls = BW.process_deal_photo_dl_urls(
                deal, access_token, (BFA.DEAL_ORDER_RESERVE_ALIAS, ))
    else:
        user.festive_data.photo_urls = None
Ejemplo n.º 4
0
def process_deals(user):
    user.data.clear_deals()

    deals_type = user.data.deals_type
    stages = None
    dt = None
    is_late = None

    if deals_type == DealsType.DELIVERS_TODAY:
        stages = [BFM.DEAL_IS_IN_DELIVERY_STATUS_ID]
        dt = datetime.now(tz=cfg.TIMEZONE).strftime('%Y-%m-%d')
    elif deals_type == DealsType.DELIVERS_TOMORROW:
        stages = [BFM.DEAL_IS_IN_DELIVERY_STATUS_ID]
        dt = (datetime.now(tz=cfg.TIMEZONE) +
              timedelta(days=1)).strftime('%Y-%m-%d')
    elif deals_type == DealsType.IN_ADVANCE:
        stages = [
            BFM.DEAL_NEW_STATUS_ID, BFM.DEAL_IN_PROCESS_STATUS_ID,
            BFM.DEAL_PAID_PREPAID_STATUS_ID,
            BFM.DEAL_PROCESSED_WAITING_FOR_SUPPLY_STATUS_ID,
            BFM.DEAL_PROCESSED_ON_HOLD_STATUS_ID, BFM.DEAL_PRINTED_STATUS_ID,
            BFM.DEAL_FLORIST_STATUS_ID, BFM.DEAL_PROCESSED_1C_STATUS_ID,
            BFM.DEAL_IS_EQUIPPED_STATUS_ID, BFM.DEAL_UNAPPROVED_STATUS_ID,
            BFM.DEAL_APPROVED_STATUS_ID
        ]
    elif deals_type == DealsType.FINISHED_IN_TIME:
        stages = [BFM.DEAL_SUCCESSFUL_STATUS_ID, BFM.DEAL_LOSE_STATUS_ID]
        dt = user.data.deals_date.isoformat()
        is_late = False
    elif deals_type == DealsType.FINISHED_LATE:
        stages = [BFM.DEAL_SUCCESSFUL_STATUS_ID, BFM.DEAL_LOSE_STATUS_ID]
        dt = user.data.deals_date.isoformat()
        is_late = True

    flt = {
        DEAL_COURIER_NEW_ALIAS: user.bitrix_user_id,
        DEAL_STAGE_ALIAS: stages
    }

    if dt:
        flt[DEAL_DATE_ALIAS] = dt

    if is_late is not None:
        flt[DEAL_IS_LATE_ALIAS] = BFM.DEAL_IS_LATE_YES if is_late else BFM.DEAL_IS_LATE_NO

    params = {
        'filter':
        flt,
        'select': [
            DEAL_ID_ALIAS, DEAL_DATE_ALIAS, DEAL_TIME_ALIAS,
            DEAL_ADDRESS_ALIAS, DEAL_FLAT_ALIAS, DEAL_RECIPIENT_NAME_ALIAS,
            DEAL_RECIPIENT_PHONE_ALIAS, DEAL_DISTRICT_ALIAS,
            DEAL_DELIVERY_COMMENT_ALIAS, DEAL_INCOGNITO_ALIAS,
            DEAL_TERMINAL_CHANGE_ALIAS, DEAL_CHANGE_SUM_ALIAS,
            DEAL_TO_PAY_ALIAS, DEAL_BIG_PHOTO_ALIAS, DEAL_SUBDIVISION_ALIAS,
            DEAL_SENDER_ID_ALIAS, DEAL_SOURCE_ID_ALIAS, DEAL_ORDER_ALIAS,
            DEAL_CONTACT_ALIAS
        ],
        'order': {
            DEAL_TIME_ALIAS: 'ASC'
        }
    }

    deals = BW.send_request('crm.deal.list', params, handle_next=True)

    for d in deals:
        deal = DealData()
        deal.deal_id = Utils.prepare_external_field(d, DEAL_ID_ALIAS)
        deal.date = Utils.prepare_deal_date(d, DEAL_DATE_ALIAS)
        deal.time = Utils.prepare_deal_time(d, DEAL_TIME_ALIAS)

        address, location = Utils.prepare_deal_address(d, DEAL_ADDRESS_ALIAS)
        deal.address = address
        deal.flat = Utils.prepare_external_field(d, DEAL_FLAT_ALIAS)

        deal.recipient_name = Utils.prepare_external_field(
            d, DEAL_RECIPIENT_NAME_ALIAS)
        deal.recipient_phone = Utils.prepare_external_field(
            d, DEAL_RECIPIENT_PHONE_ALIAS)

        district_id = Utils.prepare_external_field(d, DEAL_DISTRICT_ALIAS)
        deal.district = Utils.prepare_external_field(BW.DISTRICTS, district_id,
                                                     BW.DISTRICTS_LOCK)

        deal.delivery_comment = Utils.prepare_external_field(
            d, DEAL_DELIVERY_COMMENT_ALIAS)
        deal.incognito = Utils.prepare_deal_incognito_bot_view(
            d, DEAL_INCOGNITO_ALIAS)

        terminal_change = Utils.prepare_external_field(
            d, DEAL_TERMINAL_CHANGE_ALIAS)

        if terminal_change == BFM.DEAL_NEED_TERMINAL:
            deal.terminal_needed = True
        elif terminal_change == BFM.DEAL_NEED_CHANGE:
            deal.change_sum = Utils.prepare_external_field(
                d, DEAL_CHANGE_SUM_ALIAS)

        to_pay = Utils.prepare_external_field(d, DEAL_TO_PAY_ALIAS)

        if to_pay != '0':
            deal.to_pay = to_pay

        # only URLs for now
        deal.order_big_photos = [
            el['downloadUrl'] for el in d.get(DEAL_BIG_PHOTO_ALIAS)
        ]

        subdivision_id = Utils.prepare_external_field(d,
                                                      DEAL_SUBDIVISION_ALIAS)
        deal.subdivision = Utils.prepare_external_field(
            BW.SUBDIVISIONS, subdivision_id, BW.SUBDIVISIONS_LOCK)

        sender_id = Utils.prepare_external_field(d, DEAL_SENDER_ID_ALIAS)
        deal.sender = Utils.prepare_str(BW.get_user_name(sender_id))

        source_id = Utils.prepare_external_field(d, DEAL_SOURCE_ID_ALIAS)
        deal.source = Utils.prepare_external_field(BW.SOURCES, source_id,
                                                   BW.SOURCES_LOCK)

        deal.order = Utils.prepare_external_field(d, DEAL_ORDER_ALIAS)

        contact_id = Utils.prepare_external_field(d, DEAL_CONTACT_ALIAS)
        contact_data = BW.get_contact_data(contact_id)
        deal.contact_phone = contact_data.get(CONTACT_PHONE_ALIAS)

        user.data.add_deal(deal)