Advertisement
Guest User

Magento Sync

a guest
Dec 13th, 2012
423
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.58 KB | None | 0 0
  1. from datetime import datetime
  2. from decimal import Decimal
  3. from optparse import OptionParser
  4. from os import path
  5.  
  6. from settings import get_settings
  7. from pos_connection import PosConnection
  8. from categories import get_category_mapping
  9. from magento_api import MagentoAPI, flatten_category_tree
  10. from gui import run_gui
  11.  
  12.  
  13. class Logger(object):
  14.     def __init__(self, settings):
  15.         self.file = open(settings.get('log', 'file_name'), 'a')
  16.  
  17.     def created_category(self, category):
  18.         self.file.write('%(time)s - Created Category #%(id)i: %(name)s, Level %(level)i\n' % {
  19.             'time': datetime.now(),
  20.             'id': category.id,
  21.             'name': category.name,
  22.             'level': category.get_level(),
  23.         })
  24.  
  25.     def sync_products(self, skus):
  26.         self.file.write('%(time)s - Sync Products: %(sku)s\n' % {
  27.             'time': datetime.now(),
  28.             'sku': ', '.join(skus),
  29.         })
  30.  
  31.     def set_inventory(self, sku, qty):
  32.         self.file.write('%(time)s - Set Inventory %(sku)s: %(qty)s\n' % {
  33.             'time': datetime.now(),
  34.             'sku': sku,
  35.             'qty': qty,
  36.         })
  37.  
  38.     def progress(self, string):
  39.         print string
  40.  
  41. class Sync(object):
  42.   def __init__(self, settings):
  43.         self.settings = settings
  44.         self.logger = Logger(settings)
  45.         self.pos_connection = PosConnection(settings)
  46.         self.magento_api = MagentoAPI(settings)
  47.         self.category_mapping = get_category_mapping(self.settings)
  48.  
  49.     def sync_categories(self):
  50.         magento_categories = flatten_category_tree(self.magento_api.get_category_tree())
  51.         categories_to_sync = []
  52.  
  53.         def sync_category(category):
  54.             if category.name in magento_categories:
  55.                 category.id = magento_categories[category.name]
  56.             else:
  57.                 categories_to_sync.append(category)
  58.  
  59.             for child in category.children:
  60.                 sync_category(child)
  61.  
  62.         sync_category(self.category_mapping.root)
  63.  
  64.         self.logger.progress("Syncing %i categories" % len(categories_to_sync))
  65.         for category in categories_to_sync:
  66.             category.id = int(self.magento_api.create_category(category.parent.id, category.name))
  67.             self.logger.created_category(category)
  68.  
  69.     def sync_products(self):
  70.         magento_products = self.magento_api.get_product_list()
  71.         magento_skus = set(x['sku'] for x in magento_products)
  72.  
  73.         products_to_sync = []
  74.         for item in self.pos_connection.get_items():
  75.             if item['item_no'] in magento_skus:
  76.                 continue
  77.             if item['category'] in self.category_mapping.pos_categories:
  78.                 item['category_ids'] = [self.category_mapping.pos_categories[item['category']].id]
  79.             products_to_sync.append(item)
  80.  
  81.         self.logger.progress("Syncing %i products" % len(products_to_sync))
  82.         if products_to_sync:
  83.             self.magento_api.create_products(products_to_sync)
  84.     def sync_inventory(self):
  85.         self.pos_connection.force_inventory_table()
  86.         pos_quantities = list(self.pos_connection.get_item_quantities())
  87.         skus = [x['item_no'] for x in pos_quantities]
  88.         magento_quantities = self.magento_api.get_quantities(skus)
  89.  
  90.         self.logger.progress("Syncing %i inventories" % len(pos_quantities))
  91.         total = len(pos_quantities)
  92.         current = 0.0
  93.         last_percent = 0.00
  94.         for quantity in pos_quantities:
  95.             sku = quantity['item_no']
  96.             last_qty = quantity['sync_quantity']
  97.             pos_qty = quantity['quantity_on_hand']
  98.             if last_qty is None:
  99.                 self.magento_api.update_quantity(sku, pos_qty)
  100.                 self.pos_connection.add_sync_quantity(sku, pos_qty)
  101.  
  102.                 self.logger.set_inventory(sku, pos_qty)
  103.             else:
  104.                 magento_qty = Decimal(magento_quantities[sku])
  105.                 new_qty = last_qty + (pos_qty - last_qty) + (magento_qty - last_qty)
  106.                 if new_qty != magento_qty:
  107.                     self.magento_api.update_quantity(sku, new_qty)
  108.                 if new_qty != pos_qty:
  109.                     self.pos_connection.set_item_quantity(sku, new_qty)
  110.                 if new_qty != last_qty:
  111.                     self.pos_connection.set_sync_quantity(sku, new_qty)
  112.  
  113.                 self.logger.set_inventory(sku, new_qty)
  114.  
  115.             current_percent = round(current / total, 2) * 100
  116.             if last_percent < current_percent :
  117.                 self.logger.progress('Syncing Inventory: %.2f%%' % current_percent)
  118.                 last_percent = current_percent
  119.             current += 1.0
  120.  
  121.     def __call__(self):
  122.         self.sync_categories()
  123.         self.sync_products()
  124.         self.sync_inventory()
  125.         self.logger.progress("Finished!")
  126. def main():
  127.     dir_name = path.dirname(__file__)
  128.     if dir_name:
  129.         cfg_file = "%s/sync.cfg" % dir_name
  130.     else:
  131.         cfg_file = "sync.cfg"
  132.  
  133.     parser = OptionParser()
  134.     parser.add_option(
  135.         "-g",
  136.         "--gui",
  137.         dest = "use_gui",
  138.         default = True,
  139.         help = "Use GUI (Default: True)",
  140.     )
  141.     parser.add_option(
  142.         "-c",
  143.         "--config",
  144.         dest = "cfg_file",
  145.         default = cfg_file,
  146.         help = "Configuration file (required)",
  147.     )
  148.     options, _ = parser.parse_args()
  149.  
  150.     if options.cfg_file:
  151.         settings = get_settings(options.cfg_file)
  152.         sync = Sync(settings)
  153.  
  154.         if options.use_gui:
  155.             run_gui(sync)
  156.         else:
  157.             sync()
  158.     else:
  159.         parser.print_help()
  160.  
  161. if __name__ == '__main__':
  162.     main()
  163.  
  164.  
  165.  
  166. # the output:
  167. """
  168.  
  169. Syncing 0 categories
  170. Loading Magento products
  171. Loading Magento Products: 3.39%
  172. Loading Magento Products: 6.79%
  173. Loading Magento Products: 10.18%
  174. Loading Magento Products: 13.58%
  175. Loading Magento Products: 16.97%
  176. Loading Magento Products: 20.37%
  177. Loading Magento Products: 23.76%
  178. Loading Magento Products: 27.16%
  179. Loading Magento Products: 30.55%
  180. Loading Magento Products: 33.94%
  181. Loading Magento Products: 37.34%
  182. Loading Magento Products: 40.73%
  183. Loading Magento Products: 44.13%
  184. Loading Magento Products: 47.52%
  185. Loading Magento Products: 50.92%
  186. Loading Magento Products: 54.31%
  187. Loading Magento Products: 57.71%
  188. Loading Magento Products: 61.10%
  189. Loading Magento Products: 64.49%
  190. Loading Magento Products: 67.89%
  191. Loading Magento Products: 71.28%
  192. Loading Magento Products: 74.68%
  193. Loading Magento Products: 78.07%
  194. Loading Magento Products: 81.47%
  195. Loading Magento Products: 84.86%
  196. Loading Magento Products: 88.26%
  197. Loading Magento Products: 91.65%
  198. Loading Magento Products: 95.04%
  199. Loading Magento Products: 98.44%
  200. Loading Magento Products: 100.00%
  201. Loading POS products
  202. 'NoneType' object is not iterable
  203. eef1000 description None
  204. 'NoneType' object is not iterable
  205. bnjb description None
  206. 'NoneType' object is not iterable
  207. hadm11 name None
  208. 'NoneType' object is not iterable
  209. hadm11 short_description None
  210. 'NoneType' object is not iterable
  211. hadm11 description None
  212. 'NoneType' object is not iterable
  213. mbr60o1 description None
  214. 'NoneType' object is not iterable
  215. layan1 description None
  216. 'NoneType' object is not iterable
  217. mbr60o10 description None
  218. 'NoneType' object is not iterable
  219. mbr60o description None
  220. 'NoneType' object is not iterable
  221. eb20 description None
  222. 'NoneType' object is not iterable
  223. ethermbf description None
  224. 'NoneType' object is not iterable
  225. ebbh description None
  226. 'NoneType' object is not iterable
  227. bipa description None
  228. 'NoneType' object is not iterable
  229. mbr120o1 description None
  230. 'NoneType' object is not iterable
  231. efb12d description None
  232. 'NoneType' object is not iterable
  233. mbr120o description None
  234. Updating Products: 5.32%
  235. Updating Products: 10.64%
  236. Updating Products: 15.96%
  237. Updating Products: 21.28%
  238. Updating Products: 26.60%
  239. Updating Products: 31.91%
  240. Updating Products: 37.23%
  241. Updating Products: 42.55%
  242. Updating Products: 47.87%
  243. Updating Products: 53.19%
  244. Updating Products: 58.51%
  245. Updating Products: 63.83%
  246. Updating Products: 69.15%
  247. Updating Products: 74.47%
  248. Updating Products: 79.79%
  249. Updating Products: 85.11%
  250. Updating Products: 90.43%
  251. Updating Products: 95.74%
  252. Updating Products: 100.00%
  253. Loading Magento Images: 3.39%
  254. Loading Magento Images: 6.79%
  255. Loading Magento Images: 10.18%
  256. Loading Magento Images: 13.58%
  257. Loading Magento Images: 16.97%
  258. Loading Magento Images: 20.37%
  259. Loading Magento Images: 23.76%
  260. Loading Magento Images: 27.16%
  261. Loading Magento Images: 30.55%
  262. Loading Magento Images: 33.94%
  263. Loading Magento Images: 37.34%
  264. Loading Magento Images: 40.73%
  265. Loading Magento Images: 44.13%
  266. Loading Magento Images: 47.52%
  267. Loading Magento Images: 50.92%
  268. Loading Magento Images: 54.31%
  269. Loading Magento Images: 57.71%
  270. Loading Magento Images: 61.10%
  271. Loading Magento Images: 64.49%
  272. Loading Magento Images: 67.89%
  273. Loading Magento Images: 71.28%
  274. Loading Magento Images: 74.68%
  275. Loading Magento Images: 78.07%
  276. Loading Magento Images: 81.47%
  277. Loading Magento Images: 84.86%
  278. Loading Magento Images: 88.26%
  279. Loading Magento Images: 91.65%
  280. Loading Magento Images: 95.04%
  281. Loading Magento Images: 98.44%
  282. Loading Magento Images: 100.00%
  283. Exception in Tkinter callback
  284. Traceback (most recent call last):
  285.  File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1410, in __call__
  286.    return self.func(*args)
  287.  File "./sync.py", line 207, in __call__
  288.    self.sync_products()
  289.  File "./sync.py", line 103, in sync_products
  290.    self._update_images(pos_products, product_images)
  291.  File "./sync.py", line 194, in _update_images
  292.    'magento': [x['file'] for x in magento_products[sku]],
  293. TypeError: string indices must be integers, not str
  294.  
  295. """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement