SHARE
TWEET

Untitled

a guest May 16th, 2019 85 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/python3
  2. # coding: utf-8
  3.  
  4. import odoolib
  5. import sys
  6. import random
  7. import threading
  8.  
  9. from datetime import datetime, date
  10.  
  11.  
  12. def closing(username):
  13.     connection = odoolib.get_connection(hostname="localhost", database="12hlc", login=username, password=username, port=80)
  14.  
  15.     config_model = connection.get_model('pos.config')
  16.     session_model = connection.get_model('pos.session')
  17.     order_model = connection.get_model('pos.order')
  18.  
  19.     # Search config
  20.     config_id = config_model.search([('name', 'ilike', pos_id)])
  21.     if len(config_id) > 1:
  22.         config_id = config_id[0]
  23.  
  24.     # Open session
  25.     config_model.open_session_cb(config_id)
  26.     config_model.open_session_cb(config_id)
  27.     pos_config = config_model.read(config_id, ['current_session_id', 'journal_ids', 'pricelist_id'])[0]
  28.     session_id = pos_config['current_session_id'][0]
  29.     order_ids = order_model.search([['session_id', '=', session_id]])
  30.  
  31.     t1 = datetime.now()
  32.     session_model.action_pos_session_closing_control(session_id)
  33.     t2 = datetime.now()
  34.     print("%s closed session %s in %s seconds (%s orders)" % (username, session_id, (t2-t1).seconds, len(order_ids)))
  35.  
  36.  
  37. def selling(username, size):
  38.     connection = odoolib.get_connection(hostname="localhost", database="12hlc", login=username, password=username, port=80)
  39.  
  40.     config_model = connection.get_model('pos.config')
  41.     order_model = connection.get_model('pos.order')
  42.     product_model = connection.get_model("product.product")
  43.     abs_model = connection.get_model('account.bank.statement')
  44.  
  45.     # Search config
  46.     config_id = config_model.search([('name', 'ilike', pos_id)])
  47.     if len(config_id) > 1:
  48.         config_id = config_id[0]
  49.  
  50.     # Open session
  51.     config_model.open_session_cb(config_id)
  52.     config_model.open_session_cb(config_id)
  53.     pos_config = config_model.read(config_id, ['current_session_id', 'journal_ids', 'pricelist_id'])[0]
  54.     session_id = pos_config['current_session_id'][0]
  55.     pricelist_id = pos_config['pricelist_id'][0]
  56.     product_ids = product_model.search([['sale_ok', '=', True], ['available_in_pos', '=', True]])
  57.     abs_ids = abs_model.search_read([['state', '=', 'open'], ['pos_session_id', '=', session_id]], ['account_id','journal_id'])
  58.  
  59.     for i in range(0, size):
  60.         uid = '%s_%s' % (i, datetime.now().isoformat())
  61.         payment_method = random.choice(abs_ids)
  62.  
  63.         order_data = {
  64.             'amount_paid': 55000,
  65.             'amount_return': 0,
  66.             'amount_total': 55000,
  67.             'amount_tax': 5000,
  68.             'creation_date': datetime.now().isoformat(),
  69.             'customer_count': 1,
  70.             'fiscal_position_id': False,
  71.             'floor': False,
  72.             'floor_id': False,
  73.             'loyalty_points': 0,
  74.             'name': 'order_%s' % uid,
  75.             'partner_id': False,
  76.             'pos_session_id': session_id,
  77.             'pricelist_id': pricelist_id,
  78.             'sequence_number': i,
  79.             'table_id': False,
  80.             'uid': uid,
  81.             'user_id': False,
  82.             'lines': [[0, 0, {
  83.                 'discount': 0,
  84.                 'id': 1,
  85.                 # 'note': "",
  86.                 'pack_lot_ids': [],
  87.                 'price_subtotal': 50000,
  88.                 'price_subtotal_incl': 55000,
  89.                 'price_unit': 55000,
  90.                 'product_id': random.choice(product_ids),
  91.                 'qty': 1,
  92.                 # 'sale_advisor': 1046,
  93.                 'tax_ids': [[6, 0, [4]]]
  94.             }]],
  95.             'statement_ids': [[0, 0, {
  96.                 'account_id': payment_method['account_id'][0],
  97.                 'amount': 55000,
  98.                 'journal_id': payment_method['journal_id'][0],
  99.                 'name': date.today().strftime("%Y-%m-%d"),
  100.                 'statement_id': payment_method['id'],
  101.             }]]
  102.         }
  103.         res = order_model.create_from_ui([{
  104.             'id': uid,
  105.             'data': order_data,
  106.             'to_invoice': False
  107.         }])
  108.         print(username, res)
  109.  
  110.  
  111. if __name__ == "__main__":
  112.     if len(sys.argv) < 3:
  113.         print("Usage : lt_pos.py concurrency task")
  114.         print("")
  115.         print("Ex : lt_pos.py 3 sell")
  116.         print("     lt_pos.py 4 close")
  117.         print(" To choose the number of pos order to create:")
  118.         print("     lt_pos.py 4 sell 1000")
  119.     else:
  120.         concurrency = int(sys.argv[1])
  121.  
  122.         lst_thd = []
  123.         for i in range(5, concurrency+5):
  124.             pos_id = "00000%s" % i
  125.             pos_id = pos_id[-5:]
  126.             username = 'sm%s@mydb.com' % pos_id
  127.             if sys.argv[2] == "sell":
  128.                 sizing = 1000
  129.                 if len(sys.argv) > 3:
  130.                     sizing = int(sys.argv[3])
  131.                 thd = threading.Thread(target=selling, args=(username, sizing))
  132.             else:
  133.                 thd = threading.Thread(target=closing, args=(username,))
  134.             thd.start()
  135.             lst_thd.append(thd)
  136.  
  137.         for thd in lst_thd:
  138.             thd.join()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top