def add_agency():
    from ..agency.models_agency import AgencyModel, DestinationModel

    token = request.form['token']
    if not token:
        flash("token empty", "danger")
        return redirect(url_for('Dashboard'))

    local_active_agency = AgencyModel.query(
        AgencyModel.local_status == True
    ).get()

    url_config = ConfigModel.query(
        ConfigModel.local_ref == local_active_agency.key
    ).get()

    url = ""+url_config.url_server+"/agency/get/"+token
    result = urlfetch.fetch(url)
    result = result.content
    result = json.loads(result)

    if result['status'] and result['status'] == 404:
        flash(result['message'], "danger")
    else:
        if result['agency']:
            old_data = AgencyModel.get_by_id(result['agency']['agency_id'])

            agency_new = None

            if not old_data:
                new_data = AgencyModel(id=result['agency']['agency_id'])
                new_data.name = result['agency']['agency_name']
                new_data.country = result['agency']['agency_country']
                new_data.phone = result['agency']['agency_phone']
                new_data.fax = result['agency']['agency_fax']
                new_data.address = result['agency']['agency_address']
                new_data.reduction = float(result['agency']['agency_reduction'])
                new_data.status = result['agency']['agency_status']
                new_data.is_achouka = result['agency']['agency_is_achouka']

                destination = DestinationModel.get_by_id(result['agency']['agency_destination'])
                new_data.destination = destination.key

                agency_new = new_data.put()
            else:
                flash("This token exist", "danger")

            if agency_new:
                conf = ConfigModel()
                conf.url_server = url_config.url_server
                conf.local_ref = agency_new
                conf.token_agency = token
                conf.put()

                flash("Agency added", "success")

    return redirect(url_for('Dashboard'))
def active_local_agency():

    from ..agency.models_agency import AgencyModel

    agency_id = int(request.args.get("agency_id"))

    agency = AgencyModel.get_by_id(agency_id)

    agency_active = AgencyModel.query(
        AgencyModel.local_status == True
    ).get()
    if agency_active:
        agency_active.local_status = False
        agency_active.put()

    agency.local_status = True
    agency_save = agency.put()

    url_config = ConfigModel.query(
        ConfigModel.local_ref == agency_save
    ).get()

    sychro_agency = SynchroModel.query(
        SynchroModel.agency_synchro == agency_save
    ).order(-SynchroModel.date).get()

    if sychro_agency:
        date = sychro_agency.date

        if not sychro_agency.time_departure:
            date_combine_departure = function.time_convert(sychro_agency.time_all)
        else:
            date_combine_departure = function.time_convert(sychro_agency.time_departure)

        if not sychro_agency.time_customer:
            date_combine_customer = function.time_convert(sychro_agency.time_all)
        else:
            date_combine_customer = function.time_convert(sychro_agency.time_customer)

        if not sychro_agency.time_ticket_sale_online:
            date_sale_online = function.time_convert(sychro_agency.time_all)
        else:
            date_sale_online = function.time_convert(sychro_agency.time_ticket_sale_online)

        if not sychro_agency.time_return_and_doublons_foreign:
            date_return_doublons = function.time_convert(sychro_agency.time_all)
        else:
            date_return_doublons = function.time_convert(sychro_agency.time_return_and_doublons_foreign)
    else:
        date = None
        date_combine_departure = None
        date_combine_customer = None
        date_sale_online= None
        date_return_doublons = None

    # ajout des informations
    data_vessel = vessel_api(url_config.url_server, url_config.token_agency, "/vessel/get/", date)
    data_currency = currency_api(url_config.url_server, url_config.token_agency, "/currency/get/", date)
    data_destination = destination_api(url_config.url_server, url_config.token_agency, "/destination/get/", date)
    travel_line_api(url_config.url_server, url_config.token_agency, "/travel/get/", date)
    my_agency = agency_api(url_config.url_server, url_config.token_agency, "/agency/get/", date)
    role_api(url_config.url_server, url_config.token_agency, "/role/get/", date)
    profil_api(url_config.url_server, url_config.token_agency, "/profil/get/", date)
    user_api(url_config.url_server, url_config.token_agency, "/user/get/", date)

    departure_api(url_config.url_server, url_config.token_agency, "/departure/get/", date, date_combine_departure)

    data_class = class_api(url_config.url_server, url_config.token_agency, "/class/get/", date)
    data_journey = journey_api(url_config.url_server, url_config.token_agency, "/journey/get/", date)
    data_category = category_api(url_config.url_server, url_config.token_agency, "/category/get/", date)
    tickettype_api(url_config.url_server, url_config.token_agency, "/tickets/get/", date)

    customer_api(url_config.url_server, url_config.token_agency, "/customer/get/", date, date_combine_customer)

    # Insertion des tickets alloues
    ticket_allocated_api(url_config.url_server, url_config.token_agency, "/tickets_allocated/get/", date)
    get_ticket_sale_online(url_config.url_server, url_config.token_agency, "/get_ticket_online/get/", date, date_sale_online)
    get_doublons_ticket_return_api(url_config.url_server, url_config.token_agency, "/tickets_doublons_ticket_return_sale/get/", date_return_doublons)

    # Netoyage de la base de donnee
    clean_vessel(data_vessel)
    clean_currency(data_currency)
    clean_destination(data_destination)
    clean_class(data_class)
    clean_journey(data_journey)
    clean_categpry(data_category)
    clean_ticket()

    if sychro_agency:
        sychro_agency.time_all = datetime.datetime.now().time()
        sychro_agency.put()
    else:
        Synchro = SynchroModel()
        Synchro.agency_synchro = url_config.local_ref
        Synchro.put()

    flash("Agency activated : "+agency.name, "success")
    return redirect(url_for('Dashboard'))
def synchronization():
    token = request.args.get('token')

    url_config = ConfigModel.query(
            ConfigModel.token_agency == token
    ).get()

    if token:
        date_synchro = SynchroModel.query(
            SynchroModel.agency_synchro == url_config.local_ref
        ).order(-SynchroModel.date).get()
    else:
        active_agency = AgencyModel.query(
            AgencyModel.local_status == True
        ).get()

        date_synchro = SynchroModel.query(
            SynchroModel.agency_synchro == active_agency.key
        ).order(-SynchroModel.date).get()

    if date_synchro:
        date = date_synchro.date

        if not date_synchro.time_departure:
            date_combine_departure = function.time_convert(date_synchro.time_all)
        else:
            date_combine_departure = function.time_convert(date_synchro.time_departure)

        if not date_synchro.time_customer:
            date_combine_customer = function.time_convert(date_synchro.time_all)
        else:
            date_combine_customer = function.time_convert(date_synchro.time_customer)

        if not date_synchro.time_customer_put:
            date_customer_put = function.time_convert(date_synchro.time_all)
        else:
            date_customer_put = function.time_convert(date_synchro.time_customer_put)

        if not date_synchro.time_ticket_sale_put:
            date_ticket_sale = function.time_convert(date_synchro.time_all)
        else:
            date_ticket_sale = function.time_convert(date_synchro.time_ticket_sale_put)

        if not date_synchro.time_ticket_sale_online:
            date_sale_online = function.time_convert(date_synchro.time_all)
        else:
            date_sale_online = function.time_convert(date_synchro.time_ticket_sale_online)

        if not date_synchro.time_return_and_doublons_foreign:
            date_return_doublons = function.time_convert(date_synchro.time_all)
        else:
            date_return_doublons = function.time_convert(date_synchro.time_return_and_doublons_foreign)

    else:
        date = None
        date_combine_departure = None
        date_combine_customer = None
        date_customer_put = None
        date_ticket_sale = None
        date_sale_online = None
        date_return_doublons = None

    # ajout des informations
    data_vessel = vessel_api(url_config.url_server, token, "/vessel/get/", date)
    data_currency = currency_api(url_config.url_server, token, "/currency/get/", date)
    data_destination = destination_api(url_config.url_server, token, "/destination/get/", date)
    travel_line_api(url_config.url_server, token, "/travel/get/", date)
    agency_api(url_config.url_server, token, "/agency/get/", date)
    role_api(url_config.url_server, token, "/role/get/", date)
    profil_api(url_config.url_server, token, "/profil/get/", date)
    user_api(url_config.url_server, token, "/user/get/", date)

    departure_api(url_config.url_server, token, "/departure/get/", date, date_combine_departure)

    data_class = class_api(url_config.url_server, token, "/class/get/", date)
    data_journey = journey_api(url_config.url_server, token, "/journey/get/", date)
    data_category = category_api(url_config.url_server, token, "/category/get/", date)
    tickettype_api(url_config.url_server, token, "/tickets/get/", date)
    #
    customer_api(url_config.url_server, token, "/customer/get/", date, date_combine_customer)
    #
    if date:
        customer_api_put(url_config.url_server, token, "/customer/put/", date, date_customer_put)
        ticket_sale_put_api(url_config.url_server, token, "/ticket_local_sale_put/put/", date, date_ticket_sale)

    # Insertion des tickets alloues
    ticket_allocated_api(url_config.url_server, token, "/tickets_allocated/get/", date)
    get_ticket_sale_online(url_config.url_server, token, "/get_ticket_online/get/", date, date_sale_online)
    get_doublons_ticket_return_api(url_config.url_server, token, "/tickets_doublons_ticket_return_sale/get/", date_return_doublons)

    set_status_ticket_put()

    # Netoyage de la base de donnee
    clean_vessel(data_vessel)
    clean_currency(data_currency)
    clean_destination(data_destination)
    clean_class(data_class)
    clean_journey(data_journey)
    clean_categpry(data_category)
    clean_ticket()

    if date_synchro:
        if token:
            Synchro = SynchroModel()
            Synchro.agency_synchro = url_config.local_ref
            Synchro.put()
        else:
            date_synchro.time_all = datetime.datetime.now().time()
            date_synchro.time_departure = datetime.datetime.now().time()
            date_synchro.time_customer = datetime.datetime.now().time()
            date_synchro.time_customer_put = datetime.datetime.now().time()
            date_synchro.time_ticket_sale_put = datetime.datetime.now().time()
            date_synchro.time_ticket_sale_online = datetime.datetime.now().time()
            date_synchro.time_return_and_doublons_foreign = datetime.datetime.now().time()
            date_synchro.put()
    else:
        Synchro = SynchroModel()
        Synchro.agency_synchro = url_config.local_ref
        Synchro.put()

    if token:
        flash('Synchronization Successful', 'success')
        return redirect(url_for('Home'))
    else:
        return "true"