def new_tuning_page():
    title = 'Nova personalização'
    default = None

    database = DatabaseAdapter()

    edit_id = request.args.get('tunning_id')

    if edit_id:
        title = 'Editar personalização'
        default = {
            'tunning_id':
            edit_id,
            'customer':
            request.args.get('customer'),
            'vehicle':
            request.args.get('vehicle'),
            'date':
            request.args.get('date'),
            'quotas':
            request.args.get('quotas'),
            'value':
            request.args.get('value'),
            'services_details':
            database.select_custom_sql('tunnings_service_details',
                                       where_value=str(edit_id)),
            'parts_details':
            database.select_custom_sql('tunnings_part_details',
                                       where_value=str(edit_id))
        }

    # Combo box
    customers = database.select_all('clientes', 'cpf', 'nome')
    skills = database.select_all('especialidade', '*')
    auto_parts = database.select_all('pecas', 'cod_peca', 'valor', 'descricao')

    database.close()
    return render_template('new_tuning.html',
                           title=title,
                           combo_box_customers=customers,
                           combo_box_skills=skills,
                           combo_box_parts=auto_parts,
                           default=default)
def tunning_page():
    database = DatabaseAdapter()
    tunnings = database.select_custom_sql('tunnings')

    services_details = [
        database.select_custom_sql('tunnings_service_details',
                                   where_value=str(tunning_id[0]))
        for tunning_id in tunnings
    ]

    parts_details = [
        database.select_custom_sql('tunnings_part_details',
                                   where_value=str(tunning_id[0]))
        for tunning_id in tunnings
    ]

    database.close()
    return render_template('tunnings.html',
                           title='Personalizações',
                           rows=tunnings,
                           services_details=services_details,
                           parts_details=parts_details)
def auto_parts_page():
    database = DatabaseAdapter()

    combo_values = database.select_all_joined(
        from_table='modelo md',
        join_table='marcas mc',
        on='cod_marca',
        columns=['cod_modelo', "mc.descricao || ' ' || md.descricao"])
    new_value_label = 'Inserir modelo'

    auto_parts = database.select_custom_sql("auto_parts")

    database.close()
    return render_template('auto_parts.html',
                           title='Auto peças',
                           rows=auto_parts,
                           combo_box=combo_values,
                           new_value=new_value_label)
def get_combo_values_function():
    database = DatabaseAdapter()

    value = request.args.get('value')
    entity = request.args.get('entity')

    sql_templates = {
        'new-tuning-vehicle': 'new_tuning_get_customer_vehicles',
        'new-tuning-services': 'new_tuning_get_service_by_skill',
        'new-tuning-mechanics': 'new_tuning_get_mechanics_by_skill',
        'new-tuning-parts': 'new_tuning_get_parts_by_id',
        'tunnings-by-customer': 'tunnings_by_customer'
    }

    combo_values = database.select_custom_sql(sql_templates[entity],
                                              where_value=value)

    template = get_table_template(entity)
    return render_template(template, combo_box=combo_values)
def vehicles_customers_page():
    database = DatabaseAdapter()

    # Combo box1
    vehicles_models = database.select_all_joined(
        from_table='modelo md',
        join_table='marcas mc',
        on='cod_marca',
        columns=['cod_modelo', "mc.descricao || ' ' || md.descricao"])
    # Combo box2
    customers = database.select_all('clientes', 'cpf', 'nome')

    vehicles = database.select_custom_sql("vehicles_customers")

    database.close()
    return render_template('vehicles_customers.html',
                           title='Veículos proprietários',
                           rows=vehicles,
                           combo_box1=vehicles_models,
                           combo_box2=customers)