HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux spn-python 5.15.0-89-generic #99-Ubuntu SMP Mon Oct 30 20:42:41 UTC 2023 x86_64
User: arjun (1000)
PHP: 8.1.2-1ubuntu2.20
Disabled: NONE
Upload Files
File: //home/arjun/projects/buyercall_forms/buyercall/buyercall/blueprints/reports/tasks.py
import logging
import traceback
from flask import current_app, render_template
from buyercall.app import create_celery_app
from buyercall.blueprints.reports.views import send_daily_report
from buyercall.blueprints.partnership.models import PartnershipAccount
from celery.exceptions import SoftTimeLimitExceeded
from sqlalchemy import and_, text
import pendulum
from datetime import datetime, timedelta, timezone
from buyercall.extensions import db
from buyercall.app import create_app
from buyercall.lib.flask_mailplus import _try_renderer_template

log = logging.getLogger(__name__)

celery = create_celery_app(current_app)


@celery.task(soft_time_limit=300)
def send_daily_report_email(**kwargs):
    """
    Send daily leads report

    :return: Result of sending daily report
    """
    # Create a context for the database connection.
    app = current_app or create_app()
    db.app = app
    with app.app_context():
        from buyercall.blueprints.reports.models import Report, ReportUserTie
        # Check to see if there's a daily report in the db
        report = Report.query.filter(Report.name == 'Daily Leads Report').first()

        try:
            # Retrieve all daily reports associated with a user
            if report:
                daily_reports = ReportUserTie.query.filter(ReportUserTie.report_id == report.id).all()
                if daily_reports:
                    # Reiterate through each report and establish data to send daily email
                    count_down = 0
                    for report in daily_reports:
                        # Send a mms using function in bw celery task
                        send_daily_report_email_per_partnership_account.apply_async(
                            args=[report.partnership_accounts_id, report.report_id, report.user_id],
                            countdown=count_down)
                        count_down += 60
                else:
                    log.info('There is no daily reports in the report user tie table')
            else:
                log.info('There is no daily leads report')
        except Exception as e:
            log.error(traceback.format_exc())
        return


@celery.task(soft_time_limit=240)
def send_daily_report_email_per_partnership_account(pa_id, report_id, user_id, **kwargs):
    """
    Send daily leads report per partnership account id

    :return: Result of sending daily report per partnership account
    """
    try:
        from buyercall.blueprints.widgets.models import split_emails
        from buyercall.blueprints.partnership.models import Partnership, PartnershipAccount
        import pendulum
        # Create a context for the database connection.
        app = current_app or create_app()
        db.app = app

        # A small function to determine color of the value differences used in report
        def color_indicator(value):
            if value < 0:
                color = '#FF0000'
            elif value == 0:
                color = '#000'
            else:
                color = '#008000'
            return color

        with app.app_context():
            # Create db connection
            conn = db.engine.connect()

            # Retrieve partnership and partnership account to get account name and logo
            partner_account = PartnershipAccount.query.filter(
                PartnershipAccount.id == pa_id).first()
            partner = Partnership.query.filter(Partnership.id == partner_account.partnership_id).first()

            # Determine the times used in reporting
            # The daily report gets send out at 00:00 EST each day
            # This means we need to retrieve yesterday's data in the reports
            yesterday = pendulum.yesterday().to_date_string()
            day_before_yesterday = pendulum.today().subtract(days=2).to_date_string()
            current_week = pendulum.yesterday().week_of_year
            previous_week = pendulum.yesterday().week_of_year - 1
            current_month = pendulum.yesterday().month
            previous_month = pendulum.yesterday().month - 1
            current_year = pendulum.yesterday().year
            last_year = pendulum.yesterday().year - 1

            limit_lead_count_date = pendulum.yesterday().subtract(days=65).to_date_string()

            # Retrieve all leads from the db for a specific partnership account
            pa_contacts = conn.execute(
                text("SELECT co.id, co.created_on, co.agent_id, co.status FROM contacts co "
                     "WHERE co.partnership_account_id = :pa_id "
                     "AND co.created_on > :limit"),
                pa_id=pa_id,
                limit=limit_lead_count_date)

            # Create dict of all leads that bigger than limit lead count date
            leads = []
            for co in pa_contacts:
                co_dict = dict(
                    id=co.id,
                    created_on=co.created_on,
                    agent_id=co.agent_id,
                    status=co.status
                )
                leads.append(co_dict)

            # Retrieve all calls from the db for a specific partnership account
            pa_calls = conn.execute(
                text("SELECT ca.id, ca.agent_id, ca.created_on, ca.call_type, ca.inbound_id, ca.contact_id, ca.status "
                     "FROM leads ca "
                     "WHERE ca.partnership_account_id = :pa_id "
                     "AND ca.created_on > :limit"),
                pa_id=pa_id,
                limit=limit_lead_count_date)

            # Create dict of all calls that bigger than limit lead count date
            calls = []
            for cal in pa_calls:
                cal_dict = dict(
                    id=cal.id,
                    created_on=cal.created_on,
                    agent_id=cal.agent_id,
                    call_type=cal.call_type,
                    inbound_id=cal.inbound_id,
                    contact_id=cal.contact_id,
                    status=cal.status
                )
                calls.append(cal_dict)

            # Retrieve all messages from the db for a specific partnership account
            pa_messages = conn.execute(
                text("SELECT mg.id, mg.agent_id, mg.created_on, mg.direction, mg.inbound_id, mg.contact_id, mg.status "
                     "FROM messages mg "
                     "WHERE mg.partnership_account_id = :pa_id "
                     "AND mg.created_on > :limit"),
                pa_id=pa_id,
                limit=limit_lead_count_date)

            # Create dict of all messages that bigger than limit message count date
            messages = []
            for msg in pa_messages:
                msg_dict = dict(
                    id=msg.id,
                    created_on=msg.created_on,
                    agent_id=msg.agent_id,
                    direction=msg.direction,
                    inbound_id=msg.inbound_id,
                    contact_id=msg.contact_id,
                    status=msg.status
                )
                messages.append(msg_dict)
            # Retrieve all messages from the db for a specific partnership account
            pa_form_leads = conn.execute(
                text("SELECT fl.id, fl.created_on, fl.form_id, fl.contact_id FROM form_leads fl "
                     "WHERE fl.partnership_account_id = :pa_id "
                     "AND fl.created_on > :limit"),
                pa_id=pa_id,
                limit=limit_lead_count_date)

            # Create dict of all messages that bigger than limit message count date
            form_leads = []
            for fl in pa_form_leads:
                fl_dict = dict(
                    id=fl.id,
                    created_on=fl.created_on,
                    form_id=fl.form_id,
                    contact_id=fl.contact_id
                )
                form_leads.append(fl_dict)

            # The total unique leads for yesterday and day before for an account
            day_leads = 0
            day_before_leads = 0
            week_leads = 0
            week_before_leads = 0
            month_leads = 0
            month_before_leads = 0
            last_year_month_before_leads = 0
            day_call_leads = 0
            day_before_call_leads = 0
            day_form_leads = 0
            day_before_form_leads = 0
            day_message_leads = 0
            day_before_message_leads = 0
            day_completed_leads = 0
            day_missed_leads = 0
            day_not_assigned_leads = 0
            day_assigned_leads = 0
            day_completed_calls = 0
            day_missed_calls = 0
            day_inbound_calls = 0
            day_outbound_calls = 0
            day_inbound_msg = 0
            day_outbound_msg = 0
            day_partial_form = 0
            day_full_form = 0

            # Retrieve agent data
            account_agents = conn.execute(
                text("SELECT ag.id, ag.firstname, ag.lastname FROM agents ag WHERE ag.partnership_account_id = :pa_id"),
                pa_id=pa_id)
            agents = []
            agent_leads_exist = 'No'
            for a in account_agents:
                lead_count = 0
                out_call_count = 0
                # Retrieve all contacts associated with an agent to get lead count
                for ac in leads:
                    if ac['agent_id'] == a.id:
                        contact_lead_date_transform = ac['created_on'].strftime("%Y-%m-%dT%H:%M:%SZ")
                        contact_lead_date = pendulum.parse(contact_lead_date_transform).to_date_string()
                        if contact_lead_date == yesterday:
                            lead_count = lead_count + 1
                            agent_leads_exist = 'Yes'
                # Retrieve all contacts associated with an agent to get lead count
                for aoc in calls:
                    if aoc['agent_id'] == a.id:
                        out_call_lead_date_transform = aoc['created_on'].strftime("%Y-%m-%dT%H:%M:%SZ")
                        out_call_lead_date = pendulum.parse(out_call_lead_date_transform).to_date_string()
                        if out_call_lead_date == yesterday and aoc['call_type'] == 'outbound':
                            out_call_count = out_call_count + 1
                            agent_leads_exist = 'Yes'
                agent_fullname = a.firstname + ' ' + a.lastname
                agent_dict = dict(
                    agent=agent_fullname,
                    lead_count=lead_count,
                    outbound_calls=out_call_count)
                agents.append(agent_dict)

            # Retrieve phone number details
            account_phonenumbers = conn.execute(text("SELECT pn.id, pn.friendly_name FROM phonenumbers pn "
                                                     "WHERE pn.partnership_account_id = :pa_id "
                                                     "AND pn.is_deactivated = '0'"),
                                                pa_id=pa_id)
            phonenumbers = []
            pn_leads_exist = 'No'
            for tn in account_phonenumbers:
                in_calls = 0
                out_calls = 0
                in_msgs = 0
                out_msgs = 0
                for tnc in calls:
                    if tnc['inbound_id'] == tn.id:
                        call_date_transform = tnc['created_on'].strftime("%Y-%m-%dT%H:%M:%SZ")
                        call_date = pendulum.parse(call_date_transform).to_date_string()
                        if call_date == yesterday and tnc['call_type'] == 'inbound':
                            in_calls = in_calls + 1
                            pn_leads_exist = 'Yes'
                        elif call_date == yesterday and tnc['call_type'] == 'outbound':
                            out_calls = out_calls + 1
                            pn_leads_exist = 'Yes'
                for tnm in messages:
                    if tnm['inbound_id'] == tn.id:
                        msg_date_transform = tnm['created_on'].strftime("%Y-%m-%dT%H:%M:%SZ")
                        msg_date = pendulum.parse(msg_date_transform).to_date_string()
                        if msg_date == yesterday and tnm['direction'] == 'inbound':
                            in_msgs = in_msgs + 1
                            pn_leads_exist = 'Yes'
                        elif msg_date == yesterday and tnm['direction'] == 'outbound':
                            out_msgs = out_msgs + 1
                            pn_leads_exist = 'Yes'
                phone_number_dict = dict(
                    phone_number=tn.friendly_name,
                    in_calls=in_calls,
                    out_calls=out_calls,
                    in_msgs=in_msgs,
                    out_msgs=out_msgs)
                phonenumbers.append(phone_number_dict)

            # Retrieve form lead details
            account_forms = conn.execute(text("SELECT fm.id, fm.display_name FROM external_forms fm "
                                              "WHERE fm.partnership_account_id = :pa_id "
                                              "AND fm.is_deactivated = '0'"),
                                         pa_id=pa_id)
            forms = []
            form_leads_exist = 'No'
            for fms in account_forms:
                form_lead_count = 0
                for efl in form_leads:
                    if efl['form_id'] == fms.id:
                        form_lead_date_transform = efl['created_on'].strftime("%Y-%m-%dT%H:%M:%SZ")
                        form_lead_date = pendulum.parse(form_lead_date_transform).to_date_string()
                        if form_lead_date == yesterday:
                            form_lead_count = form_lead_count + 1
                            form_leads_exist = 'Yes'
                form_lead_dict = dict(
                    form=fms.display_name,
                    form_lead_count=form_lead_count)
                forms.append(form_lead_dict)
            # Contain statuses and lead count assigned to each status
            statuses = []

            # Reiterate over the leads and determine the totals of leads for specific time periods
            # The reiteration will update the lead totals above
            for lead_item in leads:
                lead_date_transform = lead_item['created_on'].strftime("%Y-%m-%dT%H:%M:%SZ")
                lead_date = pendulum.parse(lead_date_transform).to_date_string()
                lead_week = pendulum.parse(lead_date).week_of_year
                lead_month = pendulum.parse(lead_date).month
                lead_year = pendulum.parse(lead_date).year

                if lead_date == yesterday and lead_year == current_year:
                    day_leads = day_leads + 1

                    # Identify the unique leads originated from phone calls
                    latest_call_date = ''
                    for call_item in calls:
                        if call_item['contact_id'] == lead_item['id']:
                            if latest_call_date:
                                if call_item['created_on'] > latest_call_date:
                                    latest_call_date = call_item['created_on']
                            else:
                                latest_call_date = call_item['created_on']
                            if call_item['status'] == 'completed' \
                                    and pendulum.parse(latest_call_date.strftime("%Y-%m-%dT%H:%M:%SZ"))\
                                    .to_date_string() == yesterday:
                                day_completed_leads = day_completed_leads + 1
                            elif pendulum.parse(latest_call_date.strftime("%Y-%m-%dT%H:%M:%SZ"))\
                                    .to_date_string() == yesterday:
                                day_missed_leads = day_missed_leads + 1
                            # Calculate total calls for yesterday
                            if call_item['status'] == 'completed' or call_item['status'] == 'in-progress':
                                day_completed_calls = day_completed_calls + 1
                            elif call_item['status'] == 'missed' or 'ringing':
                                day_missed_calls = day_missed_calls + 1

                            if call_item['call_type'] == 'inbound':
                                day_inbound_calls = day_inbound_calls + 1
                            elif call_item['call_type'] == 'outbound':
                                day_outbound_calls = day_outbound_calls + 1

                    latest_message_date = ''
                    for message_item in messages:
                        if message_item['contact_id'] == lead_item['id']:
                            if latest_message_date:
                                if message_item['created_on'] > latest_message_date:
                                    latest_message_date = message_item['created_on']
                            else:
                                latest_message_date = message_item['created_on']
                            if not latest_call_date:
                                complete_msg_chat = 0
                                if message_item['status'] == 'received':
                                    complete_msg_chat = complete_msg_chat + 1
                                if message_item['status'] == 'sent':
                                    complete_msg_chat = complete_msg_chat + 1
                                if complete_msg_chat == 2:
                                    day_completed_leads = day_completed_leads + 1
                                else:
                                    day_missed_leads = day_missed_leads + 1
                            # Calculate total messages for yesterday
                            if message_item['status'] == 'received':
                                day_inbound_msg = day_inbound_msg + 1
                            elif message_item['status'] == 'sent':
                                day_outbound_msg = day_outbound_msg + 1

                    latest_form_lead_date = ''
                    for form_lead_item in form_leads:
                        if form_lead_item['contact_id'] == lead_item['id']:
                            if latest_form_lead_date:
                                if form_lead_item['created_on'] > latest_form_lead_date:
                                    latest_form_lead_date = form_lead_item['created_on']
                            else:
                                latest_form_lead_date = form_lead_item['created_on']

                            # Calculate difference between full and partial form submit
                            form_field = conn.execute(text("SELECT flf.field_value FROM form_lead_fields flf "
                                                           "WHERE flf.lead_id = :form_id "
                                                           "AND flf.field_id = 'privacypolicyfield'"),
                                                      form_id=form_lead_item['id'])
                            if form_field:
                                for fv in form_field:
                                    if fv.field_value != '':
                                        day_full_form = day_full_form + 1
                                    else:
                                        day_partial_form = day_partial_form + 1

                    # Determine how the lead originated (form, call or message) and which happened first
                    if latest_call_date and latest_message_date and latest_form_lead_date:
                        if latest_call_date < latest_message_date and latest_call_date < latest_form_lead_date:
                            day_call_leads = day_call_leads + 1
                        elif latest_message_date < latest_call_date and latest_message_date < latest_form_lead_date:
                            day_message_leads = day_message_leads + 1
                        elif latest_form_lead_date < latest_call_date and latest_form_lead_date < latest_message_date:
                            day_form_leads = day_form_leads + 1
                    elif latest_call_date and latest_message_date:
                        if latest_call_date < latest_message_date:
                            day_call_leads = day_call_leads + 1
                        elif latest_message_date < latest_call_date:
                            day_message_leads = day_message_leads + 1
                    elif latest_call_date and latest_form_lead_date:
                        if latest_call_date < latest_form_lead_date:
                            day_call_leads = day_call_leads + 1
                        elif latest_form_lead_date < latest_call_date:
                            day_form_leads = day_form_leads + 1
                    elif latest_message_date and latest_form_lead_date:
                        if latest_message_date < latest_form_lead_date:
                            day_message_leads = day_message_leads + 1
                        elif latest_form_lead_date < latest_message_date:
                            day_form_leads = day_form_leads + 1
                    elif latest_call_date:
                        day_call_leads = day_call_leads + 1
                    elif latest_message_date:
                        day_message_leads = day_message_leads + 1
                    elif latest_form_lead_date:
                        day_form_leads = day_form_leads + 1

                    # Calculate the assigned and unassigned leads
                    if lead_item['agent_id'] in [None, '']:
                        day_not_assigned_leads = day_not_assigned_leads + 1
                    else:
                        day_assigned_leads = day_assigned_leads + 1

                    # Determine status counts for leads
                    status_lead_count = 0
                    if not any(sd.get('status') == lead_item['status'] for sd in statuses):
                        status_dict = dict(
                            status=lead_item['status'],
                            lead_count=1
                        )
                        statuses.append(status_dict)
                    else:
                        sd = next(stat for stat in statuses if stat['status'] == lead_item['status'])
                        sd['lead_count'] += 1

                if lead_date == day_before_yesterday and lead_year == current_year:
                    day_before_leads = day_before_leads + 1
                    # Identify the unique leads originated from phone calls
                    day_before_call_date = ''
                    for call_item in calls:
                        if call_item['contact_id'] == lead_item['id']:
                            if day_before_call_date:
                                if call_item['created_on'] > day_before_call_date:
                                    day_before_call_date = call_item['created_on']
                            else:
                                day_before_call_date = call_item['created_on']
                    day_before_message_date = ''
                    for message_item in messages:
                        if message_item['contact_id'] == lead_item['id']:
                            if day_before_message_date:
                                if message_item['created_on'] > day_before_message_date:
                                    day_before_message_date = message_item['created_on']
                            else:
                                day_before_message_date = message_item['created_on']
                    day_before_form_lead_date = ''
                    for form_lead_item in form_leads:
                        if form_lead_item['contact_id'] == lead_item['id']:
                            if day_before_form_lead_date:
                                if form_lead_item['created_on'] > day_before_form_lead_date:
                                    day_before_form_lead_date = form_lead_item['created_on']
                            else:
                                day_before_form_lead_date = form_lead_item['created_on']
                    if day_before_call_date and day_before_message_date and day_before_form_lead_date:
                        if day_before_call_date < day_before_message_date and day_before_call_date < day_before_form_lead_date:
                            day_before_call_leads = day_before_call_leads + 1
                        elif day_before_message_date < day_before_call_date and day_before_message_date < day_before_form_lead_date:
                            day_before_message_leads = day_before_message_leads + 1
                        elif day_before_form_lead_date < day_before_call_date and day_before_form_lead_date < day_before_message_date:
                            day_before_form_leads = day_before_form_leads + 1
                    elif day_before_call_date and day_before_message_date:
                        if day_before_call_date < day_before_message_date:
                            day_before_call_leads = day_before_call_leads + 1
                        elif day_before_message_date < day_before_call_date:
                            day_before_message_leads = day_before_message_leads + 1
                    elif day_before_call_date and day_before_form_lead_date:
                        if day_before_call_date < day_before_form_lead_date:
                            day_before_call_leads = day_before_call_leads + 1
                        elif day_before_form_lead_date < day_before_call_date:
                            day_before_form_leads = day_before_form_leads + 1
                    elif day_before_message_date and day_before_form_lead_date:
                        if day_before_message_date < day_before_form_lead_date:
                            day_before_message_leads = day_before_message_leads + 1
                        elif day_before_form_lead_date < day_before_message_date:
                            day_before_form_leads = day_before_form_leads + 1
                    elif day_before_call_date:
                        day_before_call_leads = day_before_call_leads + 1
                    elif day_before_message_date:
                        day_before_message_leads = day_before_message_leads + 1
                    elif day_before_form_lead_date:
                        day_before_form_leads = day_before_form_leads + 1

                if lead_week == current_week and lead_year == current_year:
                    week_leads = week_leads + 1

                if lead_week == previous_week and lead_year == current_year:
                    week_before_leads = week_before_leads + 1

                if lead_month == current_month and lead_year == current_year:
                    month_leads = month_leads + 1

                if lead_month == previous_month and lead_year == current_year:
                    month_before_leads = month_before_leads + 1

                if lead_month == 12 and lead_year == last_year:
                    last_year_month_before_leads = last_year_month_before_leads + 1

            # Calculate the difference between leads in % value
            if day_before_leads > 0:
                day_leads_diff = round(
                    (float(day_leads) - float(day_before_leads)) / float(
                        day_before_leads) * 100, 2)
            else:
                day_leads_diff = 0

            if week_before_leads > 0:
                week_leads_diff = round(
                    (float(week_leads) - float(week_before_leads)) / float(
                        week_before_leads) * 100, 2)
            else:
                week_leads_diff = 0

            if last_year_month_before_leads > 0:
                last_year_month_leads_diff = round(
                    (float(month_leads) - float(last_year_month_before_leads)) / float(
                        last_year_month_before_leads) * 100, 2)
            else:
                last_year_month_leads_diff = 0

            if month_before_leads > 0:
                month_leads_diff = round(
                    (float(month_leads) - float(month_before_leads)) / float(
                        month_before_leads) * 100, 2)
            else:
                month_leads_diff = 0

            # Calculate the difference for lead types
            if day_before_call_leads > 0:
                day_calls_diff = round(
                    (float(day_call_leads) - float(day_before_call_leads)) / float(
                        day_before_call_leads) * 100, 2)
            else:
                day_calls_diff = 0

            if day_before_message_leads > 0:
                day_messages_diff = round(
                    (float(day_message_leads) - float(day_before_message_leads)) / float(
                        day_before_message_leads) * 100, 2)
            else:
                day_messages_diff = 0

            if day_before_form_leads > 0:
                day_forms_diff = round(
                    (float(day_form_leads) - float(day_before_form_leads)) / float(
                        day_before_form_leads) * 100, 2)
            else:
                day_forms_diff = 0

            # Assign a color to the percentage difference between time periods based on positive and negative
            day_diff_style = color_indicator(day_leads_diff)
            week_diff_style = color_indicator(week_leads_diff)
            month_diff_style = color_indicator(month_leads_diff)
            day_calls_diff_style = color_indicator(day_calls_diff)
            day_messages_diff_style = color_indicator(day_messages_diff)
            day_forms_diff_style = color_indicator(day_forms_diff)

            # Add a + plus sign to the % difference if it's positive. The negative value already has a minus
            if day_leads_diff > 0:
                day_leads_diff = '+' + str(day_leads_diff)

            if week_leads_diff > 0:
                week_leads_diff = '+' + str(week_leads_diff)

            if month_leads_diff > 0:
                month_leads_diff = '+' + str(month_leads_diff)

            if day_calls_diff > 0:
                day_calls_diff = '+' + str(day_calls_diff)

            if day_messages_diff > 0:
                day_messages_diff = '+' + str(day_messages_diff)

            if day_forms_diff > 0:
                day_forms_diff = '+' + str(day_forms_diff)

            # Create a dict from the list of leads returned above
            ctx = {'leads': leads}
            ctx['agents'] = agents
            ctx['agent_leads_exist'] = agent_leads_exist
            ctx['status'] = statuses
            ctx['phonenumbers'] = phonenumbers
            ctx['pn_leads_exist'] = pn_leads_exist
            ctx['forms'] = forms
            ctx['form_leads_exist'] = form_leads_exist
            ctx['current_date'] = yesterday
            # Declare the total unique leads for current time period
            ctx['current_month'] = current_month
            ctx['day_leads'] = day_leads
            ctx['week_leads'] = week_leads
            ctx['month_leads'] = month_leads
            ctx['day_call_leads'] = day_call_leads
            ctx['day_message_leads'] = day_message_leads
            ctx['day_form_leads'] = day_form_leads
            ctx['day_completed_leads'] = day_completed_leads
            ctx['day_missed_leads'] = day_missed_leads
            ctx['day_not_assigned_leads'] = day_not_assigned_leads
            ctx['day_assigned_leads'] = day_assigned_leads
            ctx['day_completed_calls'] = day_completed_calls
            ctx['day_missed_calls'] = day_missed_calls
            ctx['day_inbound_calls'] = day_inbound_calls
            ctx['day_outbound_calls'] = day_outbound_calls
            ctx['day_inbound_msg'] = day_inbound_msg
            ctx['day_outbound_msg'] = day_outbound_msg
            ctx['day_partial_form'] = day_partial_form
            ctx['day_full_form'] = day_full_form
            # Declare the total unique leads for previous time period
            ctx['day_before_leads'] = day_before_leads
            ctx['week_before_leads'] = week_before_leads
            ctx['month_before_leads'] = month_before_leads
            ctx['last_year_month_before_leads'] = last_year_month_before_leads
            ctx['day_before_call_leads'] = day_before_call_leads
            ctx['day_before_message_leads'] = day_before_message_leads
            ctx['day_before_form_leads'] = day_before_form_leads
            # Declare the % difference calculation between time periods
            ctx['day_leads_diff'] = day_leads_diff
            ctx['week_leads_diff'] = week_leads_diff
            ctx['month_leads_diff'] = month_leads_diff
            ctx['last_year_month_leads_diff'] = last_year_month_leads_diff
            ctx['day_calls_diff'] = day_calls_diff
            ctx['day_messages_diff'] = day_messages_diff
            ctx['day_forms_diff'] = day_forms_diff
            # Declare the color to indicate neg or pos number in report
            ctx['day_diff_style'] = day_diff_style
            ctx['week_diff_style'] = week_diff_style
            ctx['month_diff_style'] = month_diff_style
            ctx['day_calls_diff_style'] = day_calls_diff_style
            ctx['day_messages_diff_style'] = day_messages_diff_style
            ctx['day_forms_diff_style'] = day_forms_diff_style

            # SPECIFY COMPANY NAME AND LOGO
            ctx['partner_logo'] = partner.logo
            ctx['company'] = partner.name
            ctx['account_name'] = partner_account.name
            from buyercall.blueprints.reports.models import ReportUserTie
            report = ReportUserTie.query.filter(and_(ReportUserTie.partnership_accounts_id == pa_id,
                                                     ReportUserTie.report_id == report_id,
                                                     ReportUserTie.user_id == user_id)).first()

            if 'emails' in report.emails:
                # Check what emails there are
                splited_em = split_emails(report.emails['emails'])

                # SPLIT THE STRING OF EMAIL ADDRESSES AND ADD , OR ; FOR FORMATTING AND ADD IT TO EMAIL LIST
                emails = list()
                emails.extend(split_emails(report.emails['emails']))

                template = 'mail/daily_leads'
                html = _try_renderer_template(template, ext='html', **ctx)
                try:
                    from buyercall.lib.util_ses_email import send_ses_email
                    send_ses_email(recipients=emails,
                                   p_id=partner.id,
                                   subject='Daily Lead Report',
                                   html=html)
                except Exception as e:
                    log.error(traceback.format_exc())

            db.engine.dispose()
            return True
    except SoftTimeLimitExceeded:
        # Create a context for the database connection.
        app = current_app or create_app()
        db.app = app
        with app.app_context():
            from buyercall.lib.util_ses_email import send_ses_email
            email_message = 'The daily report failed for partnership account id: ' + str(pa_id) + 'and user id: ' \
                            + str(user_id)
            support_emails = app.config.get('SUPPORT_EMAILS', [])
            send_ses_email(recipients=support_emails,
                           p_id=1,
                           subject='Daily Report failed',
                           text=email_message)
            return


@celery.task(soft_time_limit=600)
def generate_credit_service_provider_report_pdf(user_id, **kwargs):
    from buyercall.blueprints.user.models import User
    user = User.query.filter(User.id == user_id).first()
    try:
        # Create a context for the database connection.
        app = current_app or create_app()
        db.app = app
        # Create db connection
        conn = db.engine.connect()

        # A small function to determine color of the value differences used in report
        def color_indicator(value):
            if value < 0:
                color = '#FF0000'
            elif value == 0:
                color = '#000'
            else:
                color = '#008000'
            return color

        def add_plus(value):
            if value > 0:
                value = "+" + str(value)
            return str(value)

        def up_or_down(value):
            if value > 0:
                value = True
            else:
                value = False
            return value

        # month's dict
        months = {1: "January", 2: "February", 3: "March", 4: "April", 5: "May", 6: "June", 7: "July",
                  8: "August", 9: "September", 10: "October ", 11: "November ", 12: "December"}

        yesterday_date = datetime.now(timezone.utc).date() - timedelta(days=1)
        day_before_date = datetime.now(timezone.utc).date() - timedelta(days=2)
        current_month_number = datetime.now(timezone.utc).month
        last_month_number = datetime.now(timezone.utc).month - 1
        if last_month_number <= 0:
            last_month_number = 12 + last_month_number
        last_month_two_number = datetime.now(timezone.utc).month - 2
        if last_month_two_number <= 0:
            last_month_two_number = 12 + last_month_two_number
        last_month_three_number = datetime.now(timezone.utc).month - 3
        if last_month_three_number <= 0:
            last_month_three_number = 12 + last_month_three_number
        current_month_name = months.get(current_month_number, '')
        last_month_name = months.get(last_month_number, '')
        last_two_month_name = months.get(last_month_two_number, '')
        last_three_month_name = months.get(last_month_three_number, '')
        company = user.company.replace(" ", "").lower()
        if company and company in ('offerlogix', 'offerlogixllc'):
            logo = 'https://widget.buyercall.com/offerlogix/img/offerlogix.jpg'
        else:
            logo = 'https://buyercall-logo.s3.us-west-2.amazonaws.com/BuyerCall-main+logo.png'
        # Today's date
        date = str(datetime.today().strftime('%Y-%m-%d'))

        # Create the dict that will host the data and include overall data
        report_data = dict(
            total_partners=0,
            total_partnership_accounts=0,
            yesterday_total_leads=0,
            yesterday_credit_leads=0,
            day_before_total_leads=0,
            day_before_credit_leads=0,
            current_month_total_leads=0,
            current_month_credit_leads=0,
            last_month_total_leads=0,
            last_month_credit_leads=0,
            last_two_month_total_leads=0,
            last_two_month_credit_leads=0,
            last_three_month_total_leads=0,
            percentage_success_yesterday=0,
            percentage_success_current_month=0,
            percentage_success_last_month=0,
            percentage_success_last_two_month=0,
            yesterday_diff='',
            month_1_diff='',
            month_2_diff='',
            month_3_diff='',
            yesterday_diff_color='#000',
            month_1_diff_color='#000',
            month_2_diff_color='#000',
            month_3_diff_color='#000',
            yesterday_total_check=False,
            month_1_total_check=False,
            month_2_total_check=False,
            month_3_total_check=False,
            partner_data=[],
        )

        # Retrieve accounts with OfferLogix as credit service provider
        credit_accounts = conn.execute(
            text("SELECT pa.id, pa.name, pa.partner_account_code, pac.service_provider, pa.partnership_id, "
                 "p.name as partner FROM partnership_accounts pa "
                 "INNER JOIN partnerships as p "
                 "ON pa.partnership_id = p.id "
                 "INNER JOIN partnership_account_credit_tie as pac "
                 "ON pa.id = pac.partnership_account_id "
                 "WHERE pa.name != :pa_not_name "
                 "AND pac.service_provider = :service_provider "
                 "AND pa.is_active = '1'"),
            pa_not_name='Mall Motors LTD',
            service_provider='offerlogix')

        # Create dict of all leads that bigger than limit lead count date
        partnership_list = []
        credit_account_ids = []

        for pa in credit_accounts:
            if pa.partnership_id not in partnership_list:
                p_dict = dict(
                    p_id=pa.partnership_id,
                    p_name=pa.partner,
                    p_total_accounts=0,
                    yesterday_total_leads=0,
                    yesterday_credit_leads=0,
                    day_before_total_leads=0,
                    day_before_credit_leads=0,
                    current_month_total_leads=0,
                    current_month_credit_leads=0,
                    last_month_total_leads=0,
                    last_month_credit_leads=0,
                    last_two_month_total_leads=0,
                    last_two_month_credit_leads=0,
                    last_three_month_total_leads=0,
                    percentage_success_yesterday=0,
                    percentage_success_current_month=0,
                    percentage_success_last_month=0,
                    percentage_success_last_two_month=0,
                    yesterday_diff='',
                    month_1_diff='',
                    month_2_diff='',
                    month_3_diff='',
                    yesterday_diff_color='#000',
                    month_1_diff_color='#000',
                    month_2_diff_color='#000',
                    month_3_diff_color='#000',
                    yesterday_total_check=False,
                    month_1_total_check=False,
                    month_2_total_check=False,
                    month_3_total_check=False,
                    p_accounts=[],
                )
                pa_dict = dict(
                    a_id=pa.id,
                    a_name=pa.name,
                    a_p_code=pa.partner_account_code,
                    service_provider=pa.service_provider,
                    yesterday_total_leads=0,
                    yesterday_credit_leads=0,
                    day_before_total_leads=0,
                    day_before_credit_leads=0,
                    current_month_total_leads=0,
                    current_month_credit_leads=0,
                    last_month_total_leads=0,
                    last_month_credit_leads=0,
                    last_two_month_total_leads=0,
                    last_two_month_credit_leads=0,
                    last_three_month_total_leads=0,
                    percentage_success_yesterday=0,
                    percentage_success_current_month=0,
                    percentage_success_last_month=0,
                    percentage_success_last_two_month=0,
                    yesterday_diff='',
                    month_1_diff='',
                    month_2_diff='',
                    month_3_diff='',
                    yesterday_diff_color='#000',
                    month_1_diff_color='#000',
                    month_2_diff_color='#000',
                    month_3_diff_color='#000',
                    yesterday_total_check=False,
                    month_1_total_check=False,
                    month_2_total_check=False,
                    month_3_total_check=False,
                    )
                p_dict["p_accounts"].append(pa_dict)
                p_dict["p_total_accounts"] += 1
                report_data["partner_data"].append(p_dict)
                if pa.id not in credit_account_ids:
                    credit_account_ids.append(pa.id)

                partnership_list.append(pa.partnership_id)
            else:
                for ca in report_data["partner_data"]:
                    if pa.partnership_id == ca["p_id"]:
                        pa_dict = dict(
                            a_id=pa.id,
                            a_name=pa.name,
                            a_p_code=pa.partner_account_code,
                            service_provider=pa.service_provider,
                            yesterday_total_leads=0,
                            yesterday_credit_leads=0,
                            day_before_total_leads=0,
                            day_before_credit_leads=0,
                            current_month_total_leads=0,
                            current_month_credit_leads=0,
                            last_month_total_leads=0,
                            last_month_credit_leads=0,
                            last_two_month_total_leads=0,
                            last_two_month_credit_leads=0,
                            last_three_month_total_leads=0,
                            percentage_success_yesterday=0,
                            percentage_success_current_month=0,
                            percentage_success_last_month=0,
                            percentage_success_last_two_month=0,
                            yesterday_diff='',
                            month_1_diff='',
                            month_2_diff='',
                            month_3_diff='',
                            yesterday_diff_color='#000',
                            month_1_diff_color='#000',
                            month_2_diff_color='#000',
                            month_3_diff_color='#000',
                            yesterday_total_check=False,
                            month_1_total_check=False,
                            month_2_total_check=False,
                            month_3_total_check=False,
                        )
                        ca["p_accounts"].append(pa_dict)
                        ca["p_total_accounts"] += 1
                        if pa.id not in credit_account_ids:
                            credit_account_ids.append(pa.id)

        report_data["total_partners"] = len(partnership_list)
        report_data["total_partnership_accounts"] = len(credit_account_ids)

        credit_account_id_tuple = tuple(credit_account_ids)
        if len(credit_account_ids) != 0:
            credit_contacts = conn.execute(
                text("SELECT co.id, co.created_on, co.partnership_account_id FROM contacts co "
                     "WHERE  co.partnership_account_id in :account_ids "
                     "AND co.is_eq_lead = '1' "
                     "AND co.created_on > current_timestamp - interval '160 days'"),
                account_ids=credit_account_id_tuple
            )
            a_credit_reports = conn.execute(
                text("SELECT DISTINCT ON (cr.contact_id) cr.id, cr.created_on, cr.contact_id, "
                     "cr.partnership_account_id "
                     "FROM credit_reports cr "
                     "WHERE  cr.partnership_account_id in :account_ids "
                     "AND cr.service_provider = :service_provider "
                     "AND cr.credit_score != '' "
                     "AND cr.created_on > current_timestamp - interval '160 days'"),
                account_ids=credit_account_id_tuple,
                service_provider='Offerlogix'
            )
        else:
            credit_contacts = None
            a_credit_reports = None

        if credit_contacts:
            for c_c in credit_contacts:
                for sp in report_data["partner_data"]:
                    for spa in sp["p_accounts"]:
                        if spa["a_id"] == c_c.partnership_account_id:
                            if c_c.created_on.strftime('%Y-%m-%d') == str(yesterday_date):
                                spa["yesterday_total_leads"] += 1
                                sp["yesterday_total_leads"] += 1
                                report_data["yesterday_total_leads"] += 1
                            if c_c.created_on.strftime('%Y-%m-%d') == str(day_before_date):
                                spa["day_before_total_leads"] += 1
                                sp["day_before_total_leads"] += 1
                                report_data["day_before_total_leads"] += 1
                            if c_c.created_on.strftime('%-m') == str(current_month_number):
                                spa["current_month_total_leads"] += 1
                                sp["current_month_total_leads"] += 1
                                report_data["current_month_total_leads"] += 1
                            if c_c.created_on.strftime('%-m') == str(last_month_number):
                                spa["last_month_total_leads"] += 1
                                sp["last_month_total_leads"] += 1
                                report_data["last_month_total_leads"] += 1
                            if c_c.created_on.strftime('%-m') == str(last_month_two_number):
                                spa["last_two_month_total_leads"] += 1
                                sp["last_two_month_total_leads"] += 1
                                report_data["last_two_month_total_leads"] += 1
                            if c_c.created_on.strftime('%-m') == str(last_month_three_number):
                                spa["last_three_month_total_leads"] += 1
                                sp["last_three_month_total_leads"] += 1
                                report_data["last_three_month_total_leads"] += 1

                            yesterday_compare = spa["yesterday_total_leads"] - spa["day_before_total_leads"]
                            month_1_compare = spa["current_month_total_leads"] - spa["last_month_total_leads"]
                            month_2_compare = spa["last_month_total_leads"] - spa["last_two_month_total_leads"]
                            month_3_compare = spa["last_two_month_total_leads"] - spa["last_three_month_total_leads"]

                            # Determine difference between days and months
                            spa["yesterday_diff"] = add_plus(yesterday_compare)
                            spa["month_1_diff"] = add_plus(month_1_compare)
                            spa["month_2_diff"] = add_plus(month_2_compare)
                            spa["month_3_diff"] = add_plus(month_3_compare)

                            # colors for differences
                            spa["yesterday_diff_color"] = color_indicator(yesterday_compare)
                            spa["month_1_diff_color"] = color_indicator(month_1_compare)
                            spa["month_2_diff_color"] = color_indicator(month_2_compare)
                            spa["month_3_diff_color"] = color_indicator(month_3_compare)

                            # Check if totals went up or down
                            spa["yesterday_total_check"] = up_or_down(yesterday_compare)
                            spa["month_1_total_check"] = up_or_down(month_1_compare)
                            spa["month_2_total_check"] = up_or_down(month_2_compare)
                            spa["month_3_total_check"] = up_or_down(month_3_compare)

                    sp_yesterday_compare = sp["yesterday_total_leads"] - sp["day_before_total_leads"]
                    sp_month_1_compare = sp["current_month_total_leads"] - sp["last_month_total_leads"]
                    sp_month_2_compare = sp["last_month_total_leads"] - sp["last_two_month_total_leads"]
                    sp_month_3_compare = sp["last_two_month_total_leads"] - sp["last_three_month_total_leads"]

                    # Determine difference between days and months
                    sp["yesterday_diff"] = add_plus(sp_yesterday_compare)
                    sp["month_1_diff"] = add_plus(sp_month_1_compare)
                    sp["month_2_diff"] = add_plus(sp_month_2_compare)
                    sp["month_3_diff"] = add_plus(sp_month_3_compare)

                    # colors for differences
                    sp["yesterday_diff_color"] = color_indicator(sp_yesterday_compare)
                    sp["month_1_diff_color"] = color_indicator(sp_month_1_compare)
                    sp["month_2_diff_color"] = color_indicator(sp_month_2_compare)
                    sp["month_3_diff_color"] = color_indicator(sp_month_3_compare)

                    # Check if totals went up or down
                    sp["yesterday_total_check"] = up_or_down(sp_yesterday_compare)
                    sp["month_1_total_check"] = up_or_down(sp_month_1_compare)
                    sp["month_2_total_check"] = up_or_down(sp_month_2_compare)
                    sp["month_3_total_check"] = up_or_down(sp_month_3_compare)

        if a_credit_reports:
            for cr_c in a_credit_reports:
                for spc in report_data["partner_data"]:
                    for spac in spc["p_accounts"]:
                        if spac["a_id"] == cr_c.partnership_account_id:
                            if cr_c.created_on.strftime('%Y-%m-%d') == str(yesterday_date):
                                spac["yesterday_credit_leads"] += 1
                                spc["yesterday_credit_leads"] += 1
                                report_data["yesterday_credit_leads"] += 1
                            if cr_c.created_on.strftime('%Y-%m-%d') == str(day_before_date):
                                spac["day_before_credit_leads"] += 1
                                spc["day_before_credit_leads"] += 1
                                report_data["day_before_credit_leads"] += 1
                            if cr_c.created_on.strftime('%-m') == str(current_month_number):
                                spac["current_month_credit_leads"] += 1
                                spc["current_month_credit_leads"] += 1
                                report_data["current_month_credit_leads"] += 1
                            if cr_c.created_on.strftime('%-m') == str(last_month_number):
                                spac["last_month_credit_leads"] += 1
                                spc["last_month_credit_leads"] += 1
                                report_data["last_month_credit_leads"] += 1
                            if cr_c.created_on.strftime('%-m') == str(last_month_two_number):
                                spac["last_two_month_credit_leads"] += 1
                                spc["last_two_month_credit_leads"] += 1
                                report_data["last_two_month_credit_leads"] += 1

                            if spac["yesterday_total_leads"] != 0:
                                spac["percentage_success_yesterday"] = (
                                    int(spac["yesterday_credit_leads"] / spac["yesterday_total_leads"] * 100))
                            if spac["current_month_total_leads"] != 0:
                                spac["percentage_success_current_month"] = (
                                    int(spac["current_month_credit_leads"] / spac["current_month_total_leads"] * 100))
                            if spac["last_month_total_leads"] != 0:
                                spac["percentage_success_last_month"] = (
                                    int(spac["last_month_credit_leads"] / spac["last_month_total_leads"] * 100))
                            if spac["last_two_month_total_leads"] != 0:
                                spac["percentage_success_two_month"] = (
                                    int(spac["last_two_month_credit_leads"] / spac["last_two_month_total_leads"] * 100))

                    if spc["yesterday_total_leads"] != 0:
                        spc["percentage_success_yesterday"] = (
                            int(spc["yesterday_credit_leads"] / spc["yesterday_total_leads"] * 100))
                    if spc["current_month_total_leads"] != 0:
                        spc["percentage_success_current_month"] = (
                            int(spc["current_month_credit_leads"] / spc["current_month_total_leads"] * 100))
                    if spc["last_month_total_leads"] != 0:
                        spc["percentage_success_last_month"] = (
                            int(spc["last_month_credit_leads"] / spc["last_month_total_leads"] * 100))
                    if spc["last_two_month_total_leads"] != 0:
                        spc["percentage_success_last_two_month"] = (
                            int(spc["last_two_month_credit_leads"] / spc["last_two_month_total_leads"] * 100))

        all_yesterday_compare = report_data["yesterday_total_leads"] - report_data["day_before_total_leads"]
        all_month_1_compare = report_data["current_month_total_leads"] - report_data["last_month_total_leads"]
        all_month_2_compare = report_data["last_month_total_leads"] - report_data["last_two_month_total_leads"]
        all_month_3_compare = report_data["last_two_month_total_leads"] - report_data["last_three_month_total_leads"]

        # Determine difference between days and months
        report_data["yesterday_diff"] = add_plus(all_yesterday_compare)
        report_data["month_1_diff"] = add_plus(all_month_1_compare)
        report_data["month_2_diff"] = add_plus(all_month_2_compare)
        report_data["month_3_diff"] = add_plus(all_month_3_compare)

        # colors for differences
        report_data["yesterday_diff_color"] = color_indicator(all_yesterday_compare)
        report_data["month_1_diff_color"] = color_indicator(all_month_1_compare)
        report_data["month_2_diff_color"] = color_indicator(all_month_2_compare)
        report_data["month_3_diff_color"] = color_indicator(all_month_3_compare)

        # Check if totals went up or down
        report_data["yesterday_total_check"] = up_or_down(all_yesterday_compare)
        report_data["month_1_total_check"] = up_or_down(all_month_1_compare)
        report_data["month_2_total_check"] = up_or_down(all_month_2_compare)
        report_data["month_3_total_check"] = up_or_down(all_month_3_compare)

        if report_data["yesterday_total_leads"] != 0:
            report_data["percentage_success_yesterday"] = (
                int(report_data["yesterday_credit_leads"] / report_data["yesterday_total_leads"] * 100))
        if report_data["current_month_total_leads"] != 0:
            report_data["percentage_success_current_month"] = (
                int(report_data["current_month_credit_leads"] / report_data["current_month_total_leads"] * 100))
        if report_data["last_month_total_leads"] != 0:
            report_data["percentage_success_last_month"] = (
                int(report_data["last_month_credit_leads"] / report_data["last_month_total_leads"] * 100))
        if report_data["last_two_month_total_leads"] != 0:
            report_data["percentage_success_last_two_month"] = (
                int(report_data["last_two_month_credit_leads"] / report_data["last_two_month_total_leads"] * 100))

        # Make a PDF straight from HTML in a string.
        html = render_template('credit_service_provider_report_pdf.html', logo=logo, date=date,
                               partner=partnership_list, report_data=report_data, current_month=current_month_name,
                               last_month=last_month_name, last_two_month=last_two_month_name,
                               last_three_month=last_three_month_name)
        return html
    except SoftTimeLimitExceeded:
        from buyercall.lib.util_ses_email import send_ses_email
        email_message = 'The credit account count report failed for user ' + str(user.partnership_account_id) + 'and user id: ' + str(user_id)
        support_emails = app.config.get('SUPPORT_EMAILS', [])
        send_ses_email(recipients=support_emails,
                       p_id=1,
                       subject='Credit Account Count Report Failed',
                       text=email_message)
        return


@celery.task(soft_time_limit=600)
def update_reporting_tables():
    """
    Update the reporting tables with the current leads data for contacts, form_leads, messages,
    calls and credit reports

    return: True
    """
    try:
        # Create a context for the database connection.
        app = current_app or create_app()
        db.app = app

        with app.app_context():
            from datetime import datetime
            # Get today's date
            today_date = datetime.today().strftime('%Y-%m-%d')
            today_date_time = datetime.today().strftime('%Y-%m-%d %H:%M:%S')
            # Create db connection
            conn = db.engine.connect()
            # Select the current top id in the table
            id_query_contacts = conn.execute(text("SELECT last_value as contact_id FROM contacts_reporting_id_seq"))
            id_query_calls = conn.execute(text("SELECT last_value as call_id FROM leads_reporting_id_seq"))
            id_query_messages = conn.execute(text("SELECT last_value as message_id FROM messages_reporting_id_seq"))
            id_query_form_leads = conn.execute(text("SELECT last_value as form_lead_id "
                                                    "FROM form_leads_reporting_id_seq"))
            id_query_credit_reports = conn.execute(text("SELECT last_value as credit_report_id "
                                                        "FROM credit_reports_reporting_id_seq"))

            # Get current highest id in tables
            current_contact_id = 0
            for iqcs in id_query_contacts:
                print(iqcs.contact_id)
                current_contact_id = iqcs.contact_id
            current_call_id = 0
            for iqcl in id_query_calls:
                print(iqcl.call_id)
                current_call_id = iqcl.call_id
            current_message_id = 0
            for iqm in id_query_messages:
                print(iqm.message_id)
                current_message_id = iqm.message_id
            current_form_lead_id = 0
            for iqfl in id_query_form_leads:
                print(iqfl.form_lead_id)
                current_form_lead_id = iqfl.form_lead_id
            current_credit_report_id = 0
            for iqcr in id_query_credit_reports:
                print(iqcr.credit_report_id)
                current_credit_report_id = iqcr.credit_report_id

            # Select leads and insert into reporting tables
            conn.execute(text("INSERT INTO contacts_reporting SELECT co.created_on, co.id, co.city, co.state, co.zip, "
                              "co.external_source_type, co.partnership_account_id, "
                              "co.is_do_not_call, co.is_unsubscribe, co.agent_assigned, co.status, "
                              "co.bdc_status, co.marketing_source, co.campaign_id, co.agent_id, "
                              "co.api_source, co.api_token_id, co.is_eq_lead "
                              "FROM contacts co "
                              "WHERE co.id > :current_contact_id"),
                         current_contact_id=current_contact_id)
            contact_reporting_id = conn.execute(text("SELECT SETVAL('public.contacts_reporting_id_seq', "
                                                     "COALESCE(MAX(id), 1) ) as last_value_id "
                                                     "FROM public.contacts_reporting;"))

            conn.execute(text("INSERT INTO leads_reporting SELECT ls.created_on, ls.id, ls.partnership_account_id, "
                              "ls.status, ls.call_source, ls.progress_status, ls.duration, ls.starttime, ls.endtime, "
                              "ls.call_type, ls.call_sid, ls.missed_call_cause, ls.cause_description, ls.recording_id, "
                              "ls.widget_guid, ls.inbound_id, ls.agent_id, ls.contact_id, ls.response_time_seconds "
                              "FROM leads ls "
                              "WHERE ls.id > :current_call_id"),
                         current_call_id=current_call_id)
            call_reporting_id = conn.execute(text("SELECT SETVAL('public.leads_reporting_id_seq', "
                                                  "COALESCE(MAX(id), 1) ) as last_value_id "
                                                  "FROM public.leads_reporting;"))

            conn.execute(text("INSERT INTO messages_reporting SELECT ms.created_on, ms.id, ms.type, ms.provider, "
                              "ms.provider_message_id, ms.num_media_files, ms.status, ms.delivery_code, "
                              "ms.delivery_description, ms.direction, ms.inbound_id, ms.partnership_account_id, "
                              "ms.contact_id, ms.agent_id "
                              "FROM messages ms "
                              "WHERE ms.id > :current_message_id"),
                         current_message_id=current_message_id)
            message_reporting_id = conn.execute(text("SELECT SETVAL('public.messages_reporting_id_seq', "
                                                     "COALESCE(MAX(id), 1) ) as last_value_id "
                                                     "FROM public.messages_reporting;"))

            conn.execute(text("INSERT INTO form_leads_reporting SELECT fl.created_on, fl.id, fl.form_id, "
                              "fl.partnership_account_id, fl.external_api_lead_id, fl.email_sent, fl.contact_id "
                              "FROM form_leads fl "
                              "WHERE fl.id > :current_form_lead_id"),
                         current_form_lead_id=current_form_lead_id)
            form_lead_reporting_id = conn.execute(text("SELECT SETVAL('public.form_leads_reporting_id_seq', "
                                                       "COALESCE(MAX(id), 1) ) as last_value_id "
                                                       "FROM public.form_leads_reporting;"))

            conn.execute(text("INSERT INTO credit_reports_reporting SELECT cr.created_on, cr.id, cr.service_provider, "
                              "cr.transaction_id, cr.credit_bureau, cr.product_type, cr.description, cr.contact_id, "
                              "cr.partnership_account_id "
                              "FROM credit_reports cr "
                              "WHERE cr.id > :current_credit_report_id"),
                         current_credit_report_id=current_credit_report_id)
            credit_report_reporting_id = conn.execute(text("SELECT SETVAL('public.credit_reports_reporting_id_seq', "
                                                           "COALESCE(MAX(id), 1) ) as last_value_id "
                                                           "FROM public.credit_reports_reporting;"))

            contacts_reporting_set_value = 0
            for c in contact_reporting_id:
                contacts_reporting_set_value = c.last_value_id

            calls_reporting_set_value = 0
            for cl in call_reporting_id:
                calls_reporting_set_value = cl.last_value_id

            messages_reporting_set_value = 0
            for m in message_reporting_id:
                messages_reporting_set_value = m.last_value_id

            form_leads_reporting_set_value = 0
            for flr in form_lead_reporting_id:
                form_leads_reporting_set_value = flr.last_value_id

            credit_reports_reporting_set_value = 0
            for crr in credit_report_reporting_id:
                credit_reports_reporting_set_value = crr.last_value_id

            from buyercall.lib.util_ses_email import send_ses_email
            email_message = 'The daily reporting table sync: ' + str(today_date_time) + \
                            '\n\ncurrent contact id: ' + str(current_contact_id) + \
                            '\ncurrent contact report id: ' + str(contacts_reporting_set_value) + \
                            '\n\ncurrent call id: ' + str(current_call_id) + \
                            '\ncurrent call report id: ' + str(calls_reporting_set_value) + \
                            '\n\ncurrent message id: ' + str(current_message_id) + \
                            '\ncurrent message report id: ' + str(messages_reporting_set_value) + \
                            '\n\ncurrent form lead id: ' + str(current_form_lead_id) + \
                            '\ncurrent form lead report id: ' + str(form_leads_reporting_set_value) + \
                            '\n\ncurrent credit report id: ' + str(current_credit_report_id) + \
                            '\ncurrent credit report report id: ' + str(credit_reports_reporting_set_value)
            support_emails = app.config.get('SUPPORT_EMAILS', [])
            send_ses_email(recipients=support_emails,
                           p_id=1,
                           subject='Daily Reporting Table Sync - ' + str(today_date),
                           text=email_message)

            db.engine.dispose()
            return True
    except SoftTimeLimitExceeded:
        # Create a context for the database connection.
        app = current_app or create_app()
        db.app = app
        with app.app_context():
            from buyercall.lib.util_ses_email import send_ses_email
            email_message = 'The daily update of reporting tables failed for: ' + str(pendulum.today())
            support_emails = app.config.get('SUPPORT_EMAILS', [])
            send_ses_email(recipients=support_emails,
                           p_id=1,
                           subject='Daily Reporting Table Updates Failed - ' + str(pendulum.today()),
                           text=email_message)
            return