Skip to content

Instantly share code, notes, and snippets.

@ray-odoo
Last active July 15, 2020 19:39
Show Gist options
  • Save ray-odoo/e963a5b6f3f19ffd6ef30ee1bcd20d2b to your computer and use it in GitHub Desktop.
Save ray-odoo/e963a5b6f3f19ffd6ef30ee1bcd20d2b to your computer and use it in GitHub Desktop.
# only tested with the speficic import file with the following schema:
# Internal ID,ID,Name,Asset Type,Asset Original Cost,Asset Current Cost,Serial Number,Quantity,Date Created,Purchase Date,Depreciation Start Date,Current Net Book Value,Cumulative Depreciation
import xlrd
import xmlrpc.client
import requests
import base64
import datetime
db = 'put_the_name_of_your_database_here'
database_url = 'put_the_url_of_your_odoo_server_here'
username = 'the_user_who_will_access_and_create'
pw = 'the_password_of_the_user'
# check that access and login works
uid = xmlrpc.client.ServerProxy('{}/xmlrpc/2/common'.format(database_url)).authenticate(db, username, pw, {})
if uid:
print("----------\nServer login successful")
#setup a connection that we can reuse
connection = xmlrpc.client.ServerProxy('{}/xmlrpc/2/object'.format(database_url))
#hardcode the ID of the Company we want to use
company = 1
#hardcode the ID of the Journal we want to use
#assets
journal = 9
#hardcode the name of the file we are using to import
filename = 'assets.xlsx'
workbook = xlrd.open_workbook(filename)
#pick the correct tab
sheetname = 'FAMAssetList'
worksheet = workbook.sheet_by_name(sheetname)
#first, check that every asset type is recorded
error_found = False
for rowcount in range(1, worksheet.nrows):
asset_type = worksheet.cell(rowcount,3).value
odoo_asset_model = connection.execute_kw(db, uid, pw, 'account.asset', 'search', [[['company_id','=',company],['state','=','model'],['name','=',asset_type]]], {'limit':1})
if not odoo_asset_model:
print("Error: Unable to find Asset Model in Odoo for provided Asset Type '%s'" % (asset_type))
error_found = True
if error_found:
exit()
#now, loop again this time creating assets
for rowcount in range(1, worksheet.nrows):
internal_id = int(worksheet.cell(rowcount,0).value)
id = worksheet.cell(rowcount,1).value
name = worksheet.cell(rowcount,2).value
asset_type = worksheet.cell(rowcount,3).value
odoo_asset_model = connection.execute_kw(db, uid, pw, 'account.asset', 'search', [[['company_id','=',company],['state','=','model'],['name','=',asset_type]]], {'limit':1})
asset_original_cost = worksheet.cell(rowcount,4).value
asset_current_cost = worksheet.cell(rowcount,5).value
serial_number = worksheet.cell(rowcount,6).value
quantity = int(worksheet.cell(rowcount,7).value)
date_created = datetime.datetime(*xlrd.xldate_as_tuple(worksheet.cell(rowcount,8).value, workbook.datemode))
purchase_date = datetime.datetime(*xlrd.xldate_as_tuple(worksheet.cell(rowcount,9).value, workbook.datemode))
start_date = datetime.datetime(*xlrd.xldate_as_tuple(worksheet.cell(rowcount,10).value, workbook.datemode))
book_value = worksheet.cell(rowcount,11).value
depreciation = worksheet.cell(rowcount,12).value
new_asset = {
'asset_type': 'purchase',
'company_id': company,
'journal_id': journal,
'state': 'draft',
'name': name,
'model_id': odoo_asset_model[0],
'account_depreciation_expense_id': connection.execute_kw(db, uid, pw, 'account.asset', 'search_read', [[['company_id','=',company],['state','=','model'],['id','=',odoo_asset_model]]], {'limit':1,'fields': ['account_depreciation_expense_id']})[0].get('account_depreciation_expense_id')[0],
'original_value': asset_original_cost,
'acquisition_date': str(purchase_date),
'first_depreciation_date': str(start_date),
'book_value': book_value,
}
odoo_asset_record = connection.execute_kw(db, uid, pw, 'account.asset', 'create', [new_asset])
if not odoo_asset_record:
print("Error: Something went wrong for Asset '%s'" % (name))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment