'Calculate Price'

@staticmethod
def climate_purchase_onaccept(form):

    import ClimateDataPortal
    vars = form.vars
    id = vars.id
    db = current.db
    ptable = db.climate_purchase
    purchase = db((ptable.id == id)).select(ptable.paid, limitby=(0, 1)).first()
    if (purchase and (purchase.paid == True)):
        pass
    else:
        parameter_id = vars.parameter_id
        table = db.climate_sample_table_spec
        query = (table.id == parameter_id)
        parameter_table = db(query).select(table.id, table.date_mapping, limitby=(0, 1)).first()
        parameter_table_id = parameter_table.id
        date_mapping_name = parameter_table.date_mapping
        period = date_mapping_name
        date_from = vars.date_from
        date_to = vars.date_to
        nationality = int(vars.nationality)
        table = db.climate_prices
        query = ((table.category == nationality) & (table.parameter_id == parameter_id))
        price_row = db(query).select(table.nrs_per_datum, limitby=(0, 1)).first()
        if (price_row is None):
            form.errors['price'] = ['There    is    no    price    set    for    this    data']
        else:
            price = price_row.nrs_per_datum
            currency = {1: '%.2f    NRs', 2: 'US$    %.2f'}[nationality]
            date_mapping = getattr(ClimateDataPortal, date_mapping_name)
            start_date_number = date_mapping.date_to_time_period(date_from)
            end_date_number = date_mapping.date_to_time_period(date_to)
            place_id = int(vars.station_id)
            datum_count = db.executesql(('SELECT    COUNT(*)    FROM    climate_sample_table_%(parameter_table_id)i    WHERE    place_id    =    %(place_id)i    AND    time_period    >=    %(start_date_number)i    AND    time_period    <=    %(end_date_number)i;' % locals()))[0][0]
            ptable[id] = {'price': (currency % (datum_count * price))}
