def sale_report(sale_id):
    sale_devices = query_db(
        "select sd.sale_device_id from tbl_sale_device sd join tbl_device d on d.device_id = sd.device_id join tbl_type t on t.type_id = d.type_id where sale_id=%s order by t.type_name, d.device_name"
        % sale_id)
    sale_name = get_sale_name_by_sale_id(sale_id)
    sale_reports = []
    for sale_device in sale_devices:
        sale_device_id = sale_device[0]
        device_id = query_db(
            "select device_id from tbl_sale_device where sale_device_id=%s" %
            sale_device_id)[0][0]
        device_details = get_extended_device_details_by_sale_device_id(
            sale_device_id)
        user_emails = query_db(
            "select u.user_email from tbl_user u join tbl_user_sale_device usd on u.user_id = usd.user_id where usd.won = 1 and usd.sale_device_id = %s order by u.user_email"
            % sale_device_id)
        user_names = []
        for user_email in user_emails:
            user_names.append(' '.join(
                map(str.capitalize, user_email[0].split('@')[0].split('.'))))
        sale_reports.append((device_details, user_names))
    return render_template('sale_report.html',
                           sale_reports=sale_reports,
                           sale_name=sale_name,
                           projecthash=get_hash_of_project())
def show_active_sales():
    sales = query_db(
        'select sale_id, sale_name, sale_date, active from tbl_sale where active=1'
    )
    if len(sales) == 1:
        return redirect(url_for('show_sale', sale_id=sales[0][0]))
    return render_template('show_active_sales.html',
                           sales=sales,
                           projecthash=get_hash_of_project())
def edit_sale(sale_id):
    sale_name = get_sale_name_by_sale_id(sale_id)
    sale_details = query_db(
        'select sd.device_id, sd.quantity, d.device_name, d.device_description, t.type_name, count(user_sale_device_id), d.price from tbl_sale_device sd join tbl_device d on sd.device_id=d.device_id join tbl_type t on d.type_id=t.type_id left join tbl_user_sale_device usd on usd.sale_device_id=sd.sale_device_id where sd.sale_id=%s group by device_id order by t.type_name, d.device_name'
        % sale_id)
    return render_template('edit_sale.html',
                           sale_id=sale_id,
                           sale_name=sale_name,
                           sale_details=sale_details,
                           projecthash=get_hash_of_project())
def admin_page():
    if not session['logged_in']:
        return redirect(url_for('login'))
    sale_details = query_db(
        "select sale_id, sale_name, sale_date from tbl_sale")
    admin_details = query_db(
        "select admin_id, admin_name, admin_email from tbl_admin")
    return render_template('admin_page.html',
                           sale_details=sale_details,
                           admin_details=admin_details,
                           projecthash=get_hash_of_project())
def show_bucket(sale_id, device_id):
    bucket_members = query_db(
        "select u.user_id, u.user_email from tbl_user u join tbl_user_sale_device usd on usd.user_id = u.user_id join tbl_sale_device sd on sd.sale_device_id = usd.sale_device_id where sd.sale_id = %s and sd.device_id = %s order by u.user_email"
        % (sale_id, device_id))
    sale_name = get_sale_name_by_sale_id(sale_id)
    device_name = get_device_name_by_device_id(device_id)
    return render_template('show_bucket.html',
                           sale_id=sale_id,
                           device_name=device_name,
                           sale_name=sale_name,
                           bucket_members=bucket_members,
                           projecthash=get_hash_of_project())
def request_add_to_bucket(sale_id, device_id):
    device_name = get_device_name_by_device_id(device_id)
    sale_name = get_sale_name_by_sale_id(sale_id)
    sale_date = get_sale_date_by_sale_id(sale_id)
    close_date = sale_date + timedelta(hours=24)
    return render_template('request_add_to_bucket.html',
                           sale_id=sale_id,
                           sale_name=sale_name,
                           device_id=device_id,
                           device_name=device_name,
                           sale_date=sale_date,
                           close_date=close_date,
                           projecthash=get_hash_of_project())
def login():
    error = None
    if request.method == 'POST':
        admin_name = request.form['username']
        plaintext_password = request.form['password']
        if check_auth(admin_name, plaintext_password):
            session['logged_in'] = True
            flash('You were logged in.')
            return redirect(url_for('admin_page'))
        error = 'Invalid username and/or password'
    return render_template('login.html',
                           error=error,
                           projecthash=get_hash_of_project())
def remove_device_from_sale(sale_id, device_id, methods=['POST', 'GET']):
    if request.method == 'POST':
        sale_device_id = get_sale_device_id(device_id, sale_id)
        delete_from_db("delete from tbl_sale_device where sale_device_id=%s" %
                       sale_device_id)
        return redirect(url_for('show_sale', sale_id=sale_id))
    message = "Are you sure you want to remove the %s from %s?" % (
        get_device_name_by_device_id(device_id),
        get_sale_name_by_sale_id(sale_id))
    return render_template('confirm_delete.html',
                           message=message,
                           name1="sale_id",
                           value1=sale_id,
                           name2="device_id",
                           value2=device_id,
                           projecthash=get_hash_of_project())
def request_bucket_list(sale_id):
    if request.method == 'POST':
        user_email = request.form['user_email']
        items = get_bucket_list(user_email, sale_id)
        (sale_name, sale_date) = get_sale_details_by_sale_id(sale_id)
        company_name = get_company_name()
        send_email(
            render_template('send_bucket_list.html',
                            items=items,
                            sale_name=sale_name,
                            sale_date=sale_date,
                            company_name=company_name), user_email,
            'Bucket List')
        flash("Your bucket list will be sent to %s" % user_email)
        return redirect(url_for('show_sale', sale_id=sale_id))
    else:
        sale_name = get_sale_name_by_sale_id(sale_id)
        return render_template('request_bucket_list.html',
                               sale_name=sale_name,
                               projecthash=get_hash_of_project())