Example #1
0
def add_user_to_local_db_from_fortnox(customer):
    tmp_usr = User(customer['Name'], customer['Email'], customer['Phone'],
                   customer['Address1'], customer['Address2'],
                   customer['City'], customer['Zipcode'], None,
                   customer['FortnoxID'], None, strip_ssn(customer),
                   get_gender_from_ssn(customer), None)
    tmp_usr.status = 'Inactive'
    db.session.add(tmp_usr)
    db.session.commit()
def add_user_to_local_db_from_fortnox(customer):
    tmp_usr = User(customer['Name'], customer['Email'], customer['Phone'],
                   customer['Address1'], customer['Address2'], customer['City'],
                   customer['Zipcode'], None, customer['FortnoxID'],
                   None, strip_ssn(customer),
                   get_gender_from_ssn(customer), None)
    tmp_usr.status = 'Inactive'
    db.session.add(tmp_usr)
    db.session.commit()
def sync_from_fortnox():
    """
    Helper script to sync the local database with the content of the database on fortnox.
    Either updates an existing customer or creates a new.
    """
    try:
        fortnox_data = Fortnox()
        customers = fortnox_data.get_all_customers()
        ret = []
        for element in customers:
            for customer in element:
                # Split firstname and lastname
                name = customer['Name'].split()
                # If the user only got a firstname, add blank to the lastname
                if len(name) < 2:
                    name.append('')

                cust = sql_user.SQLUser(None, customer['CustomerNumber'],
                                        name[0], name[1], customer['Email'],
                                        customer['Phone'], customer['Address1'],
                                        customer['Address2'], customer['City'],
                                        customer['ZipCode'], None,
                                        get_gender_from_ssn(customer),
                                        customer['OrganisationNumber'][:-5], None, None, None,
                                        None, None)
                ret.append(cust.dict())

        cl = client.UsersSqlClient()
        # If the user from Fortnox all ready exists in the database, update it. If it doesn't exist, create a new.
        for customer in ret:
            if cl.does_user_exist(customer['fortnox_id']):
                cl.add_user(customer)
            else:
                cl.update_user(customer)

        flash("Members from Fortnox is added/updated to the database!")
    except:
        flash("Something happend while adding/updating from fortnox")
def update_user_in_local_db_from_fortnox(customer):
    user = User.query.filter_by(fortnox_id=customer['FortnoxID']).first()
    if user is None:
        return "she wrote upon it; no such number, no such zone"
    else:
        user.name = customer['Name']
        user.email = customer['Email']
        user.phone = customer['Phone']
        user.address = customer['Address1']
        user.address2 = customer['Address2']
        user.city = customer['City']
        user.zip_code = customer['Zipcode']
        user.gender = get_gender_from_ssn(customer)
        user.ssn = strip_ssn(customer)
        user.expiry_date = user.expiry_date
        user.create_date = user.create_date
        user.tag_id = user.tag_id
        if(user.tagcounter is None):
            user.tagcounter = 0
        if user.status is None:
            user.status = 'Inactive'

        db.session.commit()
def update_user_in_local_db_from_fortnox(customer):
    user = User.query.filter_by(fortnox_id=customer['FortnoxID']).first()
    if user is None:
        return "she wrote upon it; no such number, no such zone"
    else:
        user.name = customer['Name']
        user.email = customer['Email']
        user.phone = customer['Phone']
        user.address = customer['Address1']
        user.address2 = customer['Address2']
        user.city = customer['City']
        user.zip_code = customer['Zipcode']
        user.gender = get_gender_from_ssn(customer)
        user.ssn = strip_ssn(customer)
        user.expiry_date = user.expiry_date
        user.create_date = user.create_date
        user.tag_id = user.tag_id
        if (user.tagcounter is None):
            user.tagcounter = 0
        if user.status is None:
            user.status = 'Inactive'

        db.session.commit()