def decline_deal(deal_id, comment, user_id): update_obj = { BFA.FESTIVE_APPROVEMENT_ALIAS: BFM.FESTIVE_APPROVEMENT_NO, BFA.FESTIVE_DECLINE_COMMENT_ALIAS: comment, BFA.FESTIVE_DECLINE_USER_ALIAS: user_id } BW.update_deal(deal_id, update_obj)
def update_deal_image(user: Operator): photos_list = user.equip.encode_deal_photos() deal_data = user.deal_data # switch to previous stage first in case of repeat equip - to trigger robots properly if user.equip.repeating: BW.update_deal(deal_data.deal_id, {DEAL_STAGE_ALIAS: DEAL_PROCESSED_1C_STATUS_ID}) update_obj = {DEAL_SMALL_PHOTO_ALIAS: [], DEAL_BIG_PHOTO_ALIAS: [], DEAL_STAGE_ALIAS: DEAL_IS_EQUIPPED_STATUS_ID, DEAL_CLIENT_URL_ALIAS: user.equip.digest, DEAL_EQUIPER_ID_ALIAS: user.bitrix_user_id, DEAL_CHECKLIST_ALIAS: {'fileData': [user.deal_data.photo_name, user.deal_data.photo_data]} } for photo in photos_list: update_obj[DEAL_SMALL_PHOTO_ALIAS].append({'fileData': [photo.name_small, photo.data_small]}) update_obj[DEAL_BIG_PHOTO_ALIAS].append({'fileData': [photo.name_big, photo.data_big]}) postcards_list = user.equip.encode_deal_postcards() if postcards_list: update_obj[DEAL_POSTCARD_PHOTO_ALIAS] = [] for photo in postcards_list: update_obj[DEAL_POSTCARD_PHOTO_ALIAS].append({'fileData': [photo.name_big, photo.data_big]}) BW.update_deal(deal_data.deal_id, update_obj)
def reapprove_deal(deal_id): update_obj = { BFA.FESTIVE_APPROVEMENT_ALIAS: BFM.FESTIVE_APPROVEMENT_NOT_SELECTED, BFA.FESTIVE_DECLINE_COMMENT_ALIAS: None, BFA.FESTIVE_DECLINE_USER_ALIAS: None } BW.update_deal(deal_id, update_obj)
def send_deal(user: Operator): update_obj = { DEAL_STAGE_ALIAS: BFM.DEAL_IS_IN_DELIVERY_STATUS_ID, DEAL_COURIER_NEW_ALIAS: user.deal_data.courier_id, DEAL_SENDER_ID_ALIAS: user.bitrix_user_id } BW.update_deal(user.deal_data.deal_id, update_obj)
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 update_deal_waiting_for_supply(user: Operator): deal_id = user.deal_data.deal_id update_obj = { DEAL_ORDER_RESERVE_ALIAS: [], DEAL_STAGE_ALIAS: DEAL_PROCESSED_WAITING_FOR_SUPPLY_STATUS_ID, DEAL_ORDER_RESERVE_DESC_ALIAS: None, DEAL_ORDER_HAS_RESERVE_ALIAS: DEAL_HAS_RESERVE_NO, DEAL_SUPPLY_DATETIME_ALIAS: user.deal_data.supply_datetime, DEAL_RESERVE_HANDLER_ID_ALIAS: user.bitrix_user_id } BW.update_deal(deal_id, update_obj)
def load_bitrix_dicts(): BW.load_dicts() # separate thread connection if not hasattr(BITRIX_DICTS_DB, 'conn'): BITRIX_DICTS_DB.conn = sqlite3.connect( os.path.join(cfg.DATA_DIR_NAME, cfg.BITRIX_DICTS_DATABASE)) cursor = BITRIX_DICTS_DB.conn.cursor() # some dicts need to be saved to use in Client backend process cursor.execute('delete from deal_times') cursor.executemany('insert into deal_times values (?,?)', BW.DEAL_TIMES.items()) BITRIX_DICTS_DB.conn.commit()
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) user.deal_data.courier_id = deal.get(DEAL_COURIER_NEW_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) if user.deal_data.stage != BFM.DEAL_APPROVED_STATUS_ID: return BW.BW_WRONG_STAGE terminal_change = Utils.prepare_external_field(deal, DEAL_TERMINAL_CHANGE_ALIAS) if terminal_change == BFM.DEAL_NEED_TERMINAL: user.deal_data.terminal_needed = True elif terminal_change == BFM.DEAL_NEED_CHANGE: user.deal_data.change_sum = Utils.prepare_external_field( deal, DEAL_CHANGE_SUM_ALIAS) user.deal_data.to_pay = Utils.prepare_external_field( deal, DEAL_TO_PAY_ALIAS) if user.deal_data.courier_id: return BH_ALREADY_HAS_COURIER return BW.BW_OK
def process_deals(user: Operator): params = { 'filter': {DEAL_FLORIST_NEW_ALIAS: user.bitrix_user_id, DEAL_STAGE_ALIAS: DEAL_FLORIST_STATUS_ID}, 'select': [DEAL_ID_ALIAS, DEAL_SUPPLY_METHOD_ALIAS, DEAL_ORDER_ALIAS, DEAL_ORDER_COMMENT_ALIAS, DEAL_POSTCARD_TEXT_ALIAS, DEAL_TOTAL_SUM_ALIAS, DEAL_DATE_ALIAS, DEAL_TIME_ALIAS, DEAL_ORDER_RESERVE_ALIAS, DEAL_ORDER_RESERVE_DESC_ALIAS, DEAL_HAS_POSTCARD_ALIAS] } 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.supply_type = Utils.prepare_deal_supply_method(d, DEAL_SUPPLY_METHOD_ALIAS) deal.order = Utils.prepare_external_field(d, DEAL_ORDER_ALIAS) deal.order_comment = Utils.prepare_external_field(d, DEAL_ORDER_COMMENT_ALIAS) has_postcard = Utils.prepare_external_field(d, DEAL_HAS_POSTCARD_ALIAS) if has_postcard == DEAL_HAS_POSTCARD_YES: deal.has_postcard = True deal.postcard_text = Utils.prepare_external_field(d, DEAL_POSTCARD_TEXT_ALIAS) deal.sum = Utils.prepare_external_field(d, DEAL_TOTAL_SUM_ALIAS) deal.date = Utils.prepare_deal_date(d, DEAL_DATE_ALIAS) deal.time = Utils.prepare_deal_time(d, DEAL_TIME_ALIAS) deal.order_reserve = [el['downloadUrl'] for el in d.get(DEAL_ORDER_RESERVE_ALIAS)] # only links list for now deal.reserve_desc = Utils.prepare_external_field(d, DEAL_ORDER_RESERVE_DESC_ALIAS) user.florist_order.add_deal(deal)
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) user.deal_data.supply_type = deal.get(DEAL_SUPPLY_METHOD_ALIAS) if user.deal_data.stage not in (DEAL_PROCESSED_1C_STATUS_ID, DEAL_UNAPPROVED_STATUS_ID, DEAL_IS_EQUIPPED_STATUS_ID, DEAL_FLORIST_STATUS_ID): return BW.BW_WRONG_STAGE has_postcard = Utils.prepare_external_field(deal, DEAL_HAS_POSTCARD_ALIAS) if has_postcard == DEAL_HAS_POSTCARD_YES: user.deal_data.has_postcard = True user.deal_data.postcard_text = Utils.prepare_external_field(deal, DEAL_POSTCARD_TEXT_ALIAS) user.deal_data.courier_id = deal.get(DEAL_COURIER_NEW_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) terminal_change = Utils.prepare_external_field(deal, DEAL_TERMINAL_CHANGE_ALIAS) if terminal_change == DEAL_NEED_TERMINAL: user.deal_data.terminal_needed = True elif terminal_change == DEAL_NEED_CHANGE: user.deal_data.change_sum = Utils.prepare_external_field(deal, DEAL_CHANGE_SUM_ALIAS) user.deal_data.to_pay = Utils.prepare_external_field(deal, DEAL_TO_PAY_ALIAS) return BW.BW_OK
def handle_login(update: Update, context, user): auth_button = KeyboardButton(text=GlobalTxt.SEND_CONTACT_BUTTON_TEXT, request_contact=True) contact = update.message.contact # check that sent contact is user's own contact if not contact or contact.user_id != update.effective_user.id: TgCommons.send_mdv2_reply_keyboard(update.effective_user, GlobalTxt.AUTHORIZATION_FAILED, [[auth_button]], True) return user.phone_number = Utils.prepare_phone_number(contact.phone_number) is_authorized = StorageWorker.check_authorization(user) if contact.user_id == update.effective_user.id and is_authorized: position = BW.get_user_position(user.bitrix_user_id) if position == BFM.COURIER_POSITION_ID: authorized_user = User.Courier.from_base(user) else: authorized_user = User.Operator.from_base(user) TgCommons.send_reply_keyboard_remove( update.effective_user, GlobalTxt.AUTHORIZATION_SUCCESSFUL) context.user_data[cfg.USER_PERSISTENT_KEY] = authorized_user return authorized_user.restart(update, context) else: TgCommons.send_mdv2_reply_keyboard(update.effective_user, GlobalTxt.REQUEST_LOGIN_MESSAGE, [[auth_button]], True) return State.LOGIN_REQUESTED
def get_deal_info_for_client(deal_id): try: deal = BW.get_deal(deal_id) if not deal: logger.error('Cant get deal info for deal %s', deal_id) return None stage = deal[DEAL_STAGE_ALIAS] deal_desc = ClientDealDesc() deal_desc.agreed = (stage != DEAL_IS_EQUIPPED_STATUS_ID) address, location = Utils.prepare_deal_address(deal, DEAL_ADDRESS_ALIAS, escape_md=False) deal_desc.address = address deal_desc.date = Utils.prepare_deal_date(deal, DEAL_DATE_ALIAS, escape_md=False) deal_desc.time = Utils.prepare_deal_time(deal, DEAL_TIME_ALIAS, escape_md=False) deal_desc.sum = Utils.prepare_external_field(deal, DEAL_TOTAL_SUM_ALIAS, escape_md=False) deal_desc.to_pay = Utils.prepare_external_field(deal, DEAL_TO_PAY_ALIAS, escape_md=False) deal_desc.flat = Utils.prepare_external_field(deal, DEAL_FLAT_ALIAS, escape_md=False) deal_desc.incognito = Utils.prepare_deal_incognito_client_view(deal, DEAL_INCOGNITO_ALIAS) except Exception as e: logger.error('Error getting client deal info: %s', e) return None return deal_desc
def return_to_warehouse(user): deal_id = user.deal_data.deal_id # get stage, check it's actual actual_stage = BW.get_deal_stage(deal_id) if actual_stage != BFM.DEAL_IS_IN_DELIVERY_STATUS_ID: return BW.BW_WRONG_STAGE fields = { DEAL_STAGE_ALIAS: BFM.DEAL_APPROVED_STATUS_ID, DEAL_WAREHOUSE_RETURNED: BFM.DEAL_IS_RETURNED_TO_WAREHOUSE_YES, DEAL_WAREHOUSE_RETURN_REASON: user.data.warehouse_return_reason } BW.update_deal(deal_id, fields) return BW.BW_OK
def bitrix_oauth_update_job(context: CallbackContext): with BW.OAUTH_LOCK: refresh_token = context.bot_data[cfg.BOT_REFRESH_TOKEN_PERSISTENT_KEY] a_token, r_token = BW.refresh_oauth(refresh_token) if a_token: context.bot_data[cfg.BOT_ACCESS_TOKEN_PERSISTENT_KEY] = a_token context.bot_data[cfg.BOT_REFRESH_TOKEN_PERSISTENT_KEY] = r_token
def update_deal_by_client(deal_id, data): try: comment = data.get(REQUEST_COMMENT_ALIAS) approved = data.get(REQUEST_APPROVED_ALIAS) call_me_back = data.get(REQUEST_CALLMEBACK_ALIAS) fields = { DEAL_CLIENT_COMMENT_ALIAS: comment, DEAL_STAGE_ALIAS: DEAL_APPROVED_STATUS_ID if approved else DEAL_UNAPPROVED_STATUS_ID, DEAL_CLIENT_CALLMEBACK_ALIAS: call_me_back, DEAL_COMMENT_APPROVED_ALIAS: DEAL_COMMENT_APPROVED_STUB if approved else None } BW.update_deal(deal_id, fields) return True except Exception as e: logger.error('Error updating client deal info: %s', e) return False
def update_deal_no_reserve(user: Operator): deal_id = user.deal_data.deal_id photos_list = user.reserve.encode_deal_photos() update_obj = { DEAL_ORDER_RESERVE_ALIAS: [{ 'fileData': [photos_list[0].name_big, photos_list[0].data_big] }], DEAL_STAGE_ALIAS: DEAL_PROCESSED_ON_HOLD_STATUS_ID, DEAL_ORDER_RESERVE_DESC_ALIAS: Txt.NO_RESERVE_NEEDED_STUB, DEAL_ORDER_HAS_RESERVE_ALIAS: DEAL_HAS_RESERVE_NO, DEAL_RESERVE_HANDLER_ID_ALIAS: user.bitrix_user_id, DEAL_ORDER_RESERVE_NOT_NEEDED_APPROVE: DEAL_ORDER_RESERVE_NOT_NEEDED_APPROVE } BW.update_deal(deal_id, update_obj)
def maintain_storage(): init_bitrix_dicts_db() Photos1.init_db() BW.load_active_users() load_bitrix_dicts() schedule.every().day.at(Photos1.ORDERS_CLEANUP_TIME).do( Photos1.orders_cleanup_job) schedule.every(15).minutes.do(BW.load_active_users) schedule.every().hour.do(load_bitrix_dicts) def thread_fun(): while True: try: schedule.run_pending() time.sleep(SCHEDULING_SLEEP_INTERVAL) except Exception as e: logger.error("Scheduler thread error: %s", e) thread = Thread(target=thread_fun, daemon=True) thread.start()
def finish_deal(user): deal_id = user.deal_data.deal_id # get stage, check it's actual actual_stage = BW.get_deal_stage(deal_id) if actual_stage != BFM.DEAL_IS_IN_DELIVERY_STATUS_ID: return BW.BW_WRONG_STAGE fields = { DEAL_STAGE_ALIAS: BFM.DEAL_SUCCESSFUL_STATUS_ID, DEAL_WAREHOUSE_RETURNED: BFM.DEAL_IS_RETURNED_TO_WAREHOUSE_NO } if user.data.late_reason is None: fields[DEAL_IS_LATE_ALIAS] = BFM.DEAL_IS_LATE_NO else: fields[DEAL_IS_LATE_ALIAS] = BFM.DEAL_IS_LATE_YES fields[DEAL_IS_LATE_REASON_ALIAS] = user.data.late_reason BW.update_deal(deal_id, fields) return BW.BW_OK
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 != DEAL_PAID_PREPAID_STATUS_ID: return BW.BW_WRONG_STAGE return BW.BW_OK
def update_deal_reserve(user: Operator): deal_id = user.deal_data.deal_id photos_list = user.reserve.encode_deal_photos() update_obj = { DEAL_ORDER_RESERVE_ALIAS: [], DEAL_STAGE_ALIAS: DEAL_PROCESSED_ON_HOLD_STATUS_ID, DEAL_ORDER_RESERVE_DESC_ALIAS: user.deal_data.reserve_desc, DEAL_ORDER_HAS_RESERVE_ALIAS: DEAL_HAS_RESERVE_YES, DEAL_RESERVE_HANDLER_ID_ALIAS: user.bitrix_user_id, DEAL_ORDER_RESERVE_NOT_NEEDED_APPROVE: DEAL_ORDER_RESERVE_NOT_NEEDED_APPROVE } for photo in photos_list: update_obj[DEAL_ORDER_RESERVE_ALIAS].append( {'fileData': [photo.name_big, photo.data_big]}) BW.update_deal(deal_id, update_obj)
def stat_unprocessed(): # TODO: move to batch queries to prevent overload sleep_interval = 1 with BW.FESTIVE_DATES_LOCK: params = { 'select': ['ID'], 'filter': { BFA.FESTIVE_APPROVEMENT_ALIAS: BFM.FESTIVE_APPROVEMENT_NOT_SELECTED, BFA.DEAL_DATE_ALIAS: BW.FESTIVE_DATES } } unprocessed = BW.send_request('crm.deal.list', params, obtain_total=True) subdivs = {} with BW.SUBDIVISIONS_LOCK, BW.FESTIVE_DATES_LOCK: for s_id, s_name in BW.SUBDIVISIONS.items(): params = { 'select': ['ID'], 'filter': { BFA.FESTIVE_APPROVEMENT_ALIAS: BFM.FESTIVE_APPROVEMENT_NO, BFA.DEAL_DATE_ALIAS: BW.FESTIVE_DATES, BFA.DEAL_SUBDIVISION_ALIAS: s_id } } declined = BW.send_request('crm.deal.list', params, obtain_total=True) if declined: subdivs[s_name] = declined time.sleep(sleep_interval) return unprocessed, subdivs
def check_deal_stage_before_update(deal_id): try: deal = BW.get_deal(deal_id) if not deal: logger.error('Cant get deal info for deal %s', deal_id) return None stage = deal[DEAL_STAGE_ALIAS] return stage == DEAL_IS_EQUIPPED_STATUS_ID except Exception as e: logging.error("Exception getting contact data, %s", e) return None
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
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)
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)
def approve_deal(deal_id): update_obj = { BFA.FESTIVE_APPROVEMENT_ALIAS: BFM.FESTIVE_APPROVEMENT_YES, BFA.FESTIVE_DECLINE_USER_ALIAS: '' } BW.update_deal(deal_id, update_obj)
def festive_deal_processed(context: CallbackContext): query_components = context.job.context bot = context.bot with BW.OAUTH_LOCK: access_token = context.bot_data[cfg.BOT_ACCESS_TOKEN_PERSISTENT_KEY] deal_id = Utils.prepare_external_field(query_components, BFA.WEBHOOK_DEAL_ID_ALIAS) deal_stage = Utils.prepare_external_field(query_components, BFA.WEBHOOK_STAGE_ALIAS) deal_order = Utils.prepare_external_field(query_components, BFA.WEBHOOK_DEAL_ORDER_ALIAS) deal_date = Utils.prepare_external_field(query_components, BFA.WEBHOOK_DATE_ALIAS) deal_time = Utils.prepare_external_field(query_components, BFA.WEBHOOK_TIME_ALIAS) deal_sum = Utils.prepare_external_field(query_components, BFA.WEBHOOK_SUM_ALIAS) deal_accepted = Utils.prepare_external_field( query_components, BFA.WEBHOOK_ACCEPTED_ALIAS) deal_source = Utils.prepare_external_field(query_components, BFA.WEBHOOK_SOURCE_ALIAS) deal_contact = Utils.prepare_external_field(query_components, BFA.WEBHOOK_CONTACT_ALIAS) deal_subdivision = Utils.prepare_external_field( query_components, BFA.WEBHOOK_SUBDIVISION_ALIAS) deal_has_reserve = Utils.prepare_external_field( query_components, BFA.WEBHOOK_HAS_RESERVE_ALIAS) deal_reserve_desc = Utils.prepare_external_field( query_components, BFA.WEBHOOK_RESERVE_DESC_ALIAS) deal_delivery_type = Utils.prepare_external_field( query_components, BFA.WEBHOOK_DELIVERY_TYPE_ALIAS) deal_district = Utils.prepare_external_field( query_components, BFA.WEBHOOK_DISTRICT_ALIAS) deal_address = Utils.prepare_external_field(query_components, BFA.WEBHOOK_ADDRESS_ALIAS) deal_delivery_comment = Utils.prepare_external_field( query_components, BFA.WEBHOOK_DELIVERY_COMMENT_ALIAS) deal_pay_method = Utils.prepare_external_field( query_components, BFA.WEBHOOK_PAY_METHOD_ALIAS) deal_pay_type = Utils.prepare_external_field( query_components, BFA.WEBHOOK_PAY_TYPE_ALIAS) deal_prepaid = Utils.prepare_external_field(query_components, BFA.WEBHOOK_PREPAID_ALIAS) deal_terminal = Utils.prepare_external_field( query_components, BFA.WEBHOOK_TERMINAL_ALIAS) deal_change = Utils.prepare_external_field(query_components, BFA.WEBHOOK_CHANGE_ALIAS) deal_to_pay = Utils.prepare_external_field(query_components, BFA.WEBHOOK_TO_PAY_ALIAS) deal_pay_status = Utils.prepare_external_field( query_components, BFA.WEBHOOK_PAY_STATUS_ALIAS) if deal_id != GlobalTxt.FIELD_IS_EMPTY_PLACEHOLDER: if deal_has_reserve == BFM.DEAL_HAS_RESERVE_YES_FRIENDLY: photo_urls = BW.get_deal_photo_dl_urls( deal_id, access_token, (BFA.DEAL_ORDER_RESERVE_ALIAS, )) else: photo_urls = None send_festive_deal_message( 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)
def update_deal_florist(user: Operator): update_obj = {DEAL_STAGE_ALIAS: BFM.DEAL_FLORIST_STATUS_ID, DEAL_FLORIST_NEW_ALIAS: user.deal_data.florist_id, DEAL_FLORIST_SETTER_ID_ALIAS: user.bitrix_user_id} BW.update_deal(user.deal_data.deal_id, update_obj)
def update_deal_courier(user: Operator): update_obj = {DEAL_COURIER_NEW_ALIAS: user.deal_data.courier_id} BW.update_deal(user.deal_data.deal_id, update_obj)