Advertisement
yogesh_tko

Accounting Migration

May 15th, 2019
999
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.97 KB | None | 0 0
  1. #### EXECUTE THIS SCRIPT AFTER YOU HAVE MIGRATED INVOICE DATA
  2. ### account.invoice, account.invoice.line, account.move.line
  3. ### account.full.reconcile(Only name field is enough)
  4.  
  5. from collections import OrderedDict
  6. import xmlrpc.client
  7. import ssl
  8. import sys
  9.  
  10. src_url = "https://industruino.com"
  11. dst_url = "http://industruino.v120.mylocal.com"
  12. src_db = "Industruino"
  13. dst_db = "industruino.v120.mylocal.com"
  14. src_username = 'connect@industruino.com'
  15. src_password = 'M1D8JHMLR4MVSC8V'
  16. dst_username = 'connect@industruino.com'
  17. dst_password = 'a'
  18. uid = 1
  19.  
  20. context_src = {}
  21. context_dst = {}
  22. skipped_groups = []
  23.  
  24. exit = False
  25. try:
  26. common = xmlrpc.client.ServerProxy('{}/xmlrpc/2/common'.format(src_url))
  27. user_id = common.authenticate(src_db, src_username, src_password, {})
  28. if not user_id:
  29. exit = True
  30. print("Unable to connect Source DB")
  31. else:
  32. print("Connection successful to Source DB")
  33. except:
  34. exit = True
  35. print("Unable to connect Source DB")
  36. try:
  37. common = xmlrpc.client.ServerProxy('{}/xmlrpc/2/common'.format(dst_url))
  38. user_id = common.authenticate(dst_db, dst_username, dst_password, {})
  39. if not user_id:
  40. exit = True
  41. print("Unable to connect Destination DB")
  42. else:
  43. print("Connection successful to Destination DB")
  44. except:
  45. exit = True
  46. print("Unable to connect Destination DB")
  47. if exit:
  48. sys.exit()
  49.  
  50. src_conn = xmlrpc.client.ServerProxy('{}/xmlrpc/2/object'.format(src_url), context=ssl._create_unverified_context())
  51.  
  52. dst_conn = xmlrpc.client.ServerProxy('{}/xmlrpc/2/object'.format(dst_url), context=ssl._create_unverified_context())
  53.  
  54.  
  55. def create_records(cursor, model, values, type='src'):
  56. if type == 'src':
  57. password = src_password
  58. database = src_db
  59. else:
  60. password = dst_password
  61. database = dst_db
  62. id = cursor.execute_kw(database, uid, password, model, 'create', [values])
  63. print ("created record %s : %s" %(model,id))
  64. return id
  65.  
  66. def write_records(cursor, model, record_id, values, type='src'):
  67. print ("Updating records %s : %s" %(model, record_id))
  68. if type == 'src':
  69. password = src_password
  70. database = src_db
  71. else:
  72. password = dst_password
  73. database = dst_db
  74. id = cursor.execute_kw(database, uid, password, model, 'write', [[record_id],values])
  75. print ("Updated record %s : %s" %(model,id))
  76. return id
  77.  
  78. def search_read_list_to_dict(result_list):
  79. result_dict = {}
  80. for dict in result_list:
  81. for key, value in dict.items():
  82. result_dict.update({dict['id']: dict['login']})
  83. return result_dict
  84.  
  85.  
  86. def format_groups(result_list):
  87. result_dict = {}
  88. for dict in result_list:
  89. for key, value in dict.items():
  90. if key == 'id':
  91. result_dict.update({dict['full_name']: (dict['id'], dict['category_id'], dict['users'])})
  92. return result_dict
  93.  
  94.  
  95. def search_records(cursor, model, domain=[[]], fields=[], type='src'):
  96. if type == 'src':
  97. password = src_password
  98. database = src_db
  99. else:
  100. password = dst_password
  101. database = dst_db
  102. print("Searching model %s with domain %s" % (model, domain))
  103. records = cursor.execute_kw(database, uid, password,
  104. model, 'search', domain)
  105. print("records found for model %s : %s" % (model, len(records)))
  106. return records
  107.  
  108. def read_records_with_id(cursor, model, record_ids=[], type='src'):
  109. if type == 'src':
  110. password = src_password
  111. database = src_db
  112. else:
  113. password = dst_password
  114. database = dst_db
  115. records = cursor.execute_kw(database, uid, password,
  116. model, 'read', [record_ids])
  117. return records
  118.  
  119. def read_records(cursor, model, domain=[[]], fields=[], type='src'):
  120. if type == 'src':
  121. password = src_password
  122. database = src_db
  123. else:
  124. password = dst_password
  125. database = dst_db
  126. record_ids = search_records(cursor, model, domain, fields)
  127. records = cursor.execute_kw(database, uid, password,
  128. model, 'read', [record_ids])
  129. print ("Found record.................", records)
  130. return records
  131.  
  132.  
  133. def update_record(cursor, model, id, vals={}, type='src'):
  134. print("updating record %s with vals %s" % (id, vals))
  135. if type == 'src':
  136. password = src_password
  137. database = src_db
  138. else:
  139. password = dst_password
  140. database = dst_db
  141. cursor.execute_kw(database, uid, password, model, 'write', [[id], vals])
  142. return True
  143.  
  144.  
  145. def search_read_records(cursor, model, domain=[[]], fields=[], type='src'):
  146. if type == 'src':
  147. password = src_password
  148. database = src_db
  149. else:
  150. password = dst_password
  151. database = dst_db
  152. records = cursor.execute_kw(database, uid, password,
  153. model, 'search_read', domain, {'fields': fields})
  154.  
  155. return records
  156.  
  157.  
  158. src_conn = xmlrpc.client.ServerProxy('{}/xmlrpc/2/object'.format(src_url), context=ssl._create_unverified_context())
  159.  
  160. dst_conn = xmlrpc.client.ServerProxy('{}/xmlrpc/2/object'.format(dst_url), context=ssl._create_unverified_context())
  161.  
  162. #['id','in',(1235,1286)]
  163. objects = OrderedDict({
  164. 'account.voucher': {'fields': [], 'domain': [], 'context_store': True},
  165.  
  166. })
  167. for object, values in objects.items():
  168. domain = values.get('domain', [])
  169. fields = values.get('fields', [])
  170. # Source Records
  171. src_records = search_read_records(src_conn, object, [domain], fields, type='src')
  172. for record in src_records:
  173. print ("Rec9ord.....................")
  174. vals = {'payment_type' : 'inbound',
  175. 'partner_type' : 'customer',
  176. 'partner_id' : record['partner_id'][0],
  177. 'amount' : abs(record['amount']),
  178. 'journal_id' : record['journal_id'][0],
  179. 'payment_date': record['date'],
  180. 'communication' : record['name'],
  181. 'payment_method_id' : 1,
  182. }
  183. ## Create Payment
  184. payment_id = create_records(dst_conn, 'account.payment', vals, type='dst')
  185. ## Set move lines
  186. for move_id in record['move_ids']:
  187. write_records(dst_conn, 'account.move.line', move_id, {'payment_id' : payment_id}, type='dst')
  188. ## Create Payments
  189.  
  190.  
  191.  
  192.  
  193. ## Read Invoices
  194. # Setting Payment information
  195. src_invoices = search_read_records(src_conn, 'account.invoice', [], ['id','payment_ids'], type='src')
  196. for invoice in src_invoices:
  197. for move_id in invoice['payment_ids']:
  198. dst_records = read_records_with_id(dst_conn, 'account.move.line', move_id, type='dst')
  199. for record in dst_records:
  200. if record['payment_id']:
  201. print ("linking payments...........%s to invoice %s", ( (4,record['payment_id'][0]),invoice['id'] ))
  202. write_records(dst_conn, 'account.invoice', invoice['id'], {'payment_ids': [(4,record['payment_id'][0])]}, type='dst')
  203.  
  204.  
  205. ## Read Reconciliation
  206. src_reconcile = search_read_records(src_conn, 'account.move.reconcile', [], ['id','line_id'], type='src')
  207. for reconcile in src_reconcile:
  208. for line_id in reconcile['line_id']:
  209. print(reconcile['line_id'])
  210. print ("lines..................", line_id, reconcile['id'])
  211. write_records(dst_conn, 'account.move.line', line_id, {'full_reconcile_id': reconcile['id']}, type='dst')
  212.  
  213.  
  214.  
  215.  
  216.  
  217.  
  218.  
  219. ### SERVER ACTION TO LINK PAYMENTS IN INVOICES
  220. # for item in env['account.full.reconcile'].search([]):
  221. # for line in item.reconciled_line_ids:
  222. # if line.credit > 0:
  223. # debit_move_id = item.reconciled_line_ids.filtered(lambda p: p.debit> 0)
  224. # if len(debit_move_id):
  225. # env['account.partial.reconcile'].create({
  226. # 'debit_move_id':debit_move_id.id,
  227. # 'credit_move_id': line.id,
  228. # 'amount': line.credit,
  229. # 'full_reconcile_id':item.id,
  230. # 'company_currency_id':1,
  231. # })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement