Exemplo n.º 1
0
def notify_today_campaigns():
    """
    Could be scheduled every morning 8:30 ~ 9:00 AM.
    Check and send notification about campaigns
    """
    store_id = request.args.get('store_id', None)
    campaigns_notify = {}

    if store_id:
        campaigns = Campaign.find_all_to_be_notified_today_by_store(store_id)
    else:
        campaigns = Campaign.find_all_to_be_notified_today()

    if campaigns:
        for campaign in campaigns:
            notify_all_sales_of_store(campaign.store_id,
                                      push_msg_for_campaign_creation(campaign),
                                      type="campaign",
                                      campaign_id=campaign.id)
            campaign.notify_sent = True

        campaigns_notify['notify_count'] = len(campaigns)

        return campaigns_notify

    return 0
Exemplo n.º 2
0
def campaigns():
    current_user = g.user
    store_id = get_or_set_store_id()
    from application.forms.campaign import CampaignSearchForm
    form = CampaignSearchForm(request.args)

    type = form.type.data
    keywords = form.keywords.data
    days = None
    active = False
    if type == 'active-campaigns':
        days = 15
        active = True

    query = {
        'days': days,
        'active': active,
        'keywords': keywords,
    }

    sort_params = SortMixin.get_order_query(form)
    if sort_params:
        query.update(sort_params)
    query.update(get_page_info(request))

    campaigns = Campaign.find_all_by_store_in_recent_days_and_keywords(
        store_id, **query)

    return render_template('campaigns/campaigns.html',
                           selected_menu=CAMPAIGN_MGMT,
                           form=form,
                           campaigns=campaigns,
                           back_endpoint=request.args.get(
                               'back_endpoint', None))
Exemplo n.º 3
0
Arquivo: user.py Projeto: sungitly/isr
def morning_call():
    current_user = g.user
    store_id = get_or_set_store_id()

    today = datetime.date.today()
    yesterday = today - datetime.timedelta(days=1)
    orders_stats = dict()
    orders_stats['yesterday_orders_count'] = Order.count_orders_by_date_and_store(yesterday, store_id)
    orders_stats['current_month_orders_count'] = Order.count_orders_from_date_and_store(
        datetime.date.today().replace(day=1),
        store_id)
    current_month_ta = TaSetting.find_active_monthly_ta(today.year, today.month, store_id)
    orders_stats['current_month_target'] = current_month_ta.value if current_month_ta else 'N/A'

    sales_stats = User.get_all_sales_by_store_from_cache(long(store_id))
    yesterday_receptions = Reception.find_all_by_date_in_store(yesterday, store_id)
    today_appointments = Appointment.find_all_by_date_in_store(datetime.date.today(), store_id)

    rx_stats = dict()
    rx_stats['yesterday_incomplete_count'] = len(
        [x for x in yesterday_receptions if x._last_status_changer == 'system'])

    total_counts, sales_counts = calc_reception_counts(yesterday_receptions, sales_stats)

    for sale in sales_stats:
        sale.rx_count = sales_counts.get(sale.id, {'total': 0, 'new': 0, 'appt_new': 0, 'appt': 0, 'other': 0})

        populate_appt_count_agg_by_sale_and_type(today_appointments, sale)

    sales_stats.sort(key=lambda sale: sale.rx_count, reverse=True)

    appts_count = generate_appt_count_agg_by_type(today_appointments)
    recent_campaigns = Campaign.find_all_by_store_in_recent_days(store_id, 15)

    current_endpoint = 'user.morning_call'
    now = datetime.datetime.now()
    yesterday_datetime = now - datetime.timedelta(days=1)
    rx_base_url, rx_url_params = get_filter_link('receptions.receptions', yesterday_datetime, yesterday_datetime,
                                                 current_endpoint)
    rx_incomplete_params = rx_url_params.copy()
    rx_incomplete_params['incomplete'] = 'y'

    appt_base_url, appt_url_params = get_filter_link('appointments.appts', now, now, current_endpoint)

    order_base_url, order_url_params = get_filter_link('orders.orders', yesterday_datetime, yesterday_datetime,
                                                       current_endpoint)
    monthly_order_url_params = order_url_params.copy()
    monthly_order_url_params['start_date'] = datetime.date.today().replace(day=1).strftime(DATE_FORMAT)
    monthly_order_url_params['end_date'] = now.strftime(DATE_FORMAT)

    return render_template('user/salesmanager/morningcall.html', selected_menu=USER_MORNING_CALL,
                           orders_stats=orders_stats, sales_stats=sales_stats, appts_count=appts_count,
                           total_rx_count=total_counts, recent_campaigns=recent_campaigns,
                           recent_campaigns_count=len(recent_campaigns), current_month_ta=current_month_ta,
                           rx_base_url=rx_base_url, rx_url_params=rx_url_params, appt_base_url=appt_base_url,
                           appt_url_params=appt_url_params, rx_incomplete_params=rx_incomplete_params,
                           order_base_url=order_base_url, order_url_params=order_url_params,
                           monthly_order_url_params=monthly_order_url_params, rx_stats=rx_stats,
                           back_endpoint=request.args.get('back_endpoint', None))
Exemplo n.º 4
0
def get_campaign(uid):
    result = Campaign.find(uid)

    if result is None:
        abort(404,
              description=gettext(u'campaign with id %(id)s is not found',
                                  id=uid))
    return result
Exemplo n.º 5
0
def create_campaign():
    data = request.json
    if data is None:
        abort(400, description=gettext('invalid json request'))

    campaign = Campaign(**data)

    campaign.save_and_flush()

    if campaign.notify_date == date.today():
        notify_all_sales_of_store(campaign.store_id,
                                  push_msg_for_campaign_creation(campaign),
                                  type="campaign",
                                  campaign_id=campaign.id)
        campaign.notify_sent = True

    return campaign, 201, add_location_header(
        dict(), url_for('api.get_campaign', uid=campaign.id))
Exemplo n.º 6
0
def sync_campaigns():
    # if not hasattr(request, 'source'):
    #     abort(401)

    try:
        data = request.json
        if not data:
            abort(400, description=gettext('invalid json request'))
    except:
        abort(400, description=gettext('invalid json request'))

    campaign = Campaign(**data)
    # campaign.source = request.source
    # default notify_date to tomorrow
    campaign.notify_date = date.today() + timedelta(days=1)

    campaign.save_and_flush()

    return campaign, 201, add_location_header(
        dict(), url_for('api.get_campaign', uid=campaign.id))
Exemplo n.º 7
0
def edit_campaign(campaign_id):
    current_user = g.user
    store_id = get_or_set_store_id()

    campaign = Campaign.find_by_id_and_store(campaign_id, store_id)

    if not campaign:
        abort(404)

    return edit(
        CAMPAIGN_MGMT, campaign=campaign
    )  # render_template('campaigns/edit.html', selected_menu=CAMPAIGN_MGMT)
Exemplo n.º 8
0
def edit(selected_menu, campaign=None):
    current_user = g.user
    store_id = get_or_set_store_id()

    from application.forms.campaign import CampaignForm

    form = None
    campaign_cars = []
    if campaign:
        campaign_cars = campaign.related_cars.split(',')
        form = CampaignForm(obj=campaign)
    else:
        form = CampaignForm()

    lookup = Lookup.find_by_name_and_store(store_id, 'intent-car')

    if lookup:
        cars = LookupValue.find_all_by_lookup_id(lookup.id)
        form.related_cars.choices = [(car.code, car.value) for car in cars]

    if form.related_cars.choices is None:
        form.related_cars.choices = []

    if request.method == 'GET':
        form.related_cars.data = campaign_cars

    if request.method == 'POST' and form.validate_on_submit():
        if campaign is None:
            campaign = Campaign()
            campaign.store_id = store_id
        form.populate_obj(campaign)
        campaign.related_cars = u','.join(form.related_cars.data)
        campaign.save_and_flush()
        flash_success(gettext(u'campaign saved'))

        return redirect(url_for('campaigns.campaigns'))
        # form.
        # campaign = form.po
        # if form.validate_on_submit():
        # json_r = uc_login(form.username.data, form.password.data)
        #
        # if json_r and json_r['status'] == 0 and get_user_type(json_r) == 6:
        #     user = save_user_from_uc(json_r)
        #     add_user(user)
        #     return redirect(referer or url_for('user.dashboard'))
        #
        # flash_error(gettext(u'login failed'))

    return render_template('campaigns/edit.html',
                           selected_menu=selected_menu,
                           campaign=campaign,
                           form=form,
                           back_url=back_url(url_for('campaigns.campaigns')))
Exemplo n.º 9
0
def merge_stores(from_store_id, to_store_id):
    passcode = request.args.get('passcode', None)
    if not passcode or not valid_passcode(passcode):
        abort(401)

    result = dict()

    result['receptions'] = Reception.migrate_store(from_store_id, to_store_id)
    result['customers'] = Customer.migrate_store(from_store_id, to_store_id)
    result['appointments'] = Appointment.migrate_store(from_store_id, to_store_id)
    from application.models.order import Order
    result['orders'] = Order.migrate_store(from_store_id, to_store_id)
    from application.models.calllog import Calllog
    result['calllogs'] = Calllog.migrate_store(from_store_id, to_store_id)
    from application.models.campaign import Campaign
    result['campaigns'] = Campaign.migrate_store(from_store_id, to_store_id)
    from application.models.driverecord import DriveRecord
    result['driverecords'] = DriveRecord.migrate_store(from_store_id, to_store_id)

    return result
Exemplo n.º 10
0
Arquivo: user.py Projeto: sungitly/isr
def evening_call():
    current_user = g.user
    store_id = get_or_set_store_id()

    today = datetime.date.today()
    orders_stats = dict()
    orders_stats['today_orders_count'] = Order.count_orders_by_date_and_store(datetime.date.today(), store_id)
    orders_stats['current_month_orders_count'] = Order.count_orders_from_date_and_store(
        datetime.date.today().replace(day=1),
        store_id)
    current_month_ta = TaSetting.find_active_monthly_ta(today.year, today.month, store_id)
    orders_stats['current_month_target'] = current_month_ta.value if current_month_ta else 'N/A'

    sales_stats = User.get_all_sales_by_store_from_cache(long(store_id))
    today_receptions = Reception.find_all_of_today_in_store(store_id)
    today_appointments = Appointment.find_all_by_date_in_store(datetime.date.today(), store_id)
    tomorrow_appointments = Appointment.find_all_by_date_in_store(datetime.date.today() + datetime.timedelta(days=1),
                                                                  store_id)
    total_counts, sales_counts = calc_reception_counts(today_receptions, sales_stats)

    for sale in sales_stats:
        populate_appt_count_agg_by_sale_and_type(today_appointments, sale, include_closed=True)
        populate_appt_count_agg_by_sale_and_type(tomorrow_appointments, sale, attr_prefix='tomorrow_')
        sale.rx_count = sales_counts.get(sale.id, {'total': 0, 'new': 0, 'appt_new': 0, 'appt': 0, 'other': 0})

    appts_count = generate_appt_count_agg_by_type(today_appointments, include_closed=True)
    tomorrow_appts_count = generate_appt_count_agg_by_type(tomorrow_appointments)

    sales_stats.sort(key=lambda sale: sale.rx_count, reverse=True)

    rx_stats = {'today_incomplete_count': len([x for x in today_receptions if x.status != 'completed'])}

    appt_stats = dict()
    appt_stats['instore'] = len([x for x in today_appointments if x.status != 'cancelled' and x.type == 'instore'])
    appt_stats['open_instore'] = len([x for x in today_appointments if x.status == 'opened' and x.type == 'instore'])
    appt_stats['followup'] = len([x for x in today_appointments if x.type == 'followup'])
    appt_stats['open_followup'] = len([x for x in today_appointments if x.status == 'opened' and x.type == 'followup'])

    now = datetime.datetime.now()
    tomorrow_datetime = now + datetime.timedelta(days=1)
    current_endpoint = 'user.evening_call'

    rx_base_url, rx_url_params = get_filter_link('receptions.receptions', now, now, current_endpoint)
    rx_incomplete_params = rx_url_params.copy()
    rx_incomplete_params['status_filter'] = 'in-store'
    appt_base_url, appt_url_params = get_filter_link('appointments.appts', now, now, current_endpoint)

    tomorrow_appt_base_url, tomorrow_appt_url_params = get_filter_link('appointments.appts', tomorrow_datetime,
                                                                       tomorrow_datetime,
                                                                       current_endpoint)

    recent_campaigns = Campaign.find_all_by_store_in_recent_days(store_id, 15)

    order_base_url, order_url_params = get_filter_link('orders.orders', now, now, current_endpoint)
    monthly_order_url_params = order_url_params.copy()
    monthly_order_url_params['start_date'] = datetime.date.today().replace(day=1).strftime(DATE_FORMAT)
    monthly_order_url_params['end_date'] = now.strftime(DATE_FORMAT)

    return render_template('user/salesmanager/eveningcall.html', selected_menu=USER_EVENING_CALL, rx_stats=rx_stats,
                           orders_stats=orders_stats, today_receptions_count=len(today_receptions),
                           rx_incomplete_params=rx_incomplete_params,
                           sales_stats=sales_stats, appts_count=appts_count, total_rx_count=total_counts,
                           tomorrow_appts_count=tomorrow_appts_count, recent_campaigns=recent_campaigns,
                           recent_campaigns_count=len(recent_campaigns), rx_base_url=rx_base_url,
                           rx_url_params=rx_url_params, order_base_url=order_base_url,
                           tomorrow_appt_base_url=tomorrow_appt_base_url,
                           tomorrow_appt_url_params=tomorrow_appt_url_params,
                           order_url_params=order_url_params, appt_stats=appt_stats, appt_base_url=appt_base_url,
                           appt_url_params=appt_url_params,
                           monthly_order_url_params=monthly_order_url_params,
                           back_endpoint=request.args.get('back_endpoint', None))
Exemplo n.º 11
0
def get_active_campaigns(store_id):
    return Campaign.find_all_active_by_store(store_id,
                                             **get_page_info(request))