partmgr-import-partdb 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. #
  4. # PartMgr - Part-DB V0.1.3 RW import filter
  5. #
  6. # Copyright 2014 Michael Buesch <m@bues.ch>
  7. #
  8. # This program is free software; you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation; either version 2 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License along
  19. # with this program; if not, write to the Free Software Foundation, Inc.,
  20. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. #
  22. from partmgr.core.database import *
  23. import sys
  24. import getopt
  25. import mysql.connector
  26. class Partdb_Part(object):
  27. def __init__(self,
  28. id, id_category, name, instock, mininstock,
  29. comment, id_footprint, id_storeloc, id_supplier,
  30. supplierpartnr):
  31. self.id = id
  32. self.id_category = id_category
  33. self.name = name
  34. self.instock = instock
  35. self.mininstock = mininstock
  36. self.comment = comment
  37. self.id_footprint = id_footprint
  38. self.id_storeloc = id_storeloc
  39. self.id_supplier = id_supplier
  40. self.supplierpartnr = supplierpartnr
  41. class Partdb_Category(object):
  42. def __init__(self,
  43. id, name, parentnode):
  44. self.id = id
  45. self.name = name
  46. self.parentnode = parentnode
  47. class Partdb_Footprint(object):
  48. def __init__(self,
  49. id, name):
  50. self.id = id
  51. self.name = name
  52. class Partdb_Storeloc(object):
  53. def __init__(self,
  54. id, name):
  55. self.id = id
  56. self.name = name
  57. class Partdb_Supplier(object):
  58. def __init__(self,
  59. id, name):
  60. self.id = id
  61. self.name = name
  62. class Partdb_Price(object):
  63. def __init__(self,
  64. id, part_id, ma, price, timestamp):
  65. self.id = id
  66. self.part_id = part_id
  67. self.ma = ma
  68. self.price = price
  69. self.timestamp = timestamp
  70. def addCategories(catMap, db, conn, parentId):
  71. c = conn.cursor(buffered = True)
  72. c.execute("SELECT id, name, parentnode "
  73. "FROM categories "
  74. "WHERE parentnode=%s "
  75. "ORDER BY name;",
  76. (str(parentId),))
  77. for row in c:
  78. srcCat = Partdb_Category(*row)
  79. if parentId:
  80. parent = catMap[parentId]
  81. else:
  82. parent = None
  83. cat = Category(name = srcCat.name,
  84. parent = parent)
  85. db.modifyCategory(cat)
  86. catMap[srcCat.id] = cat
  87. addCategories(catMap, db, conn, srcCat.id)
  88. c.close()
  89. def addFootprints(footpMap, db, conn):
  90. c = conn.cursor()
  91. c.execute("SELECT id, name "
  92. "FROM footprints;")
  93. for row in c:
  94. srcFootp = Partdb_Footprint(*row)
  95. footp = Footprint(name = srcFootp.name)
  96. db.modifyFootprint(footp)
  97. footpMap[srcFootp.id] = footp
  98. c.close()
  99. def addSuppliers(suppMap, db, conn):
  100. c = conn.cursor()
  101. c.execute("SELECT id, name "
  102. "FROM suppliers;")
  103. for row in c:
  104. srcSupp = Partdb_Supplier(*row)
  105. supp = Supplier(name = srcSupp.name)
  106. db.modifySupplier(supp)
  107. suppMap[srcSupp.id] = supp
  108. c.close()
  109. def addLocation(locMap, db, conn):
  110. c = conn.cursor()
  111. c.execute("SELECT id, name "
  112. "FROM storeloc;")
  113. for row in c:
  114. srcLoc = Partdb_Storeloc(*row)
  115. loc = Location(name = srcLoc.name)
  116. db.modifyLocation(loc)
  117. locMap[srcLoc.id] = loc
  118. c.close()
  119. def importFromPartdb(db, conn):
  120. if db.countRootCategories():
  121. print("Error: The target database %s is not empty" %\
  122. db.filename)
  123. return 1
  124. # Build the category tree.
  125. catMap = {} # key = partdb-id, value = Category()
  126. addCategories(catMap, db, conn, 0)
  127. # Build the footprints.
  128. footpMap = {} # key = partdb-id, value = Footprint()
  129. addFootprints(footpMap, db, conn)
  130. # Build the store locations.
  131. locMap = {} # key = partdb-id, value = Location()
  132. addLocation(locMap, db, conn)
  133. # Build the suppliers.
  134. suppMap = {} # key = partdb-id, value = Supplier()
  135. addSuppliers(suppMap, db, conn)
  136. # Get all prices.
  137. priceMap = {} # key = partdb-part-id, value = Partdb_Price()
  138. c = conn.cursor()
  139. c.execute("SELECT id, part_id, ma, preis, t "
  140. "FROM preise;")
  141. for row in c:
  142. price = Partdb_Price(*row)
  143. priceMap[price.part_id] = price
  144. c.close()
  145. # Build the stock items.
  146. c = conn.cursor()
  147. c.execute("SELECT id, id_category, name, instock, mininstock, "
  148. "comment, id_footprint, id_storeloc, id_supplier, "
  149. "supplierpartnr "
  150. "FROM parts;")
  151. for row in c:
  152. srcPart = Partdb_Part(*row)
  153. cat = catMap[srcPart.id_category]
  154. footp = footpMap[srcPart.id_footprint]
  155. supp = suppMap[srcPart.id_supplier]
  156. loc = locMap[srcPart.id_storeloc]
  157. part = Part(name = srcPart.name,
  158. category = cat)
  159. db.modifyPart(part)
  160. minQty = srcPart.mininstock
  161. if minQty == 0:
  162. targetQty = 0
  163. else:
  164. targetQty = minQty + int(round((minQty * 0.25)))
  165. if targetQty == minQty:
  166. targetQty += 1
  167. stockItem = StockItem(name = srcPart.name,
  168. description = srcPart.comment,
  169. part = part,
  170. category = cat,
  171. footprint = footp,
  172. minQuantity = minQty,
  173. targetQuantity = targetQty)
  174. db.modifyStockItem(stockItem)
  175. try:
  176. price = priceMap[srcPart.id].price
  177. priceStamp = priceMap[srcPart.id].timestamp
  178. except KeyError:
  179. price = Origin.NO_PRICE
  180. priceStamp = 0
  181. origin = Origin(name = "",
  182. stockItem = stockItem,
  183. supplier = supp,
  184. orderCode = srcPart.supplierpartnr,
  185. price = price,
  186. priceTimeStamp = priceStamp)
  187. db.modifyOrigin(origin)
  188. storage = Storage(name = "",
  189. stockItem = stockItem,
  190. location = loc,
  191. quantity = srcPart.instock)
  192. db.modifyStorage(storage)
  193. c.close()
  194. return 0
  195. def usage():
  196. print("PartMgr - Part-DB-V0.1.3-RW import filter")
  197. print()
  198. print("Usage: partmgr-import-partdb TARGETFILE.pmg SQLHOST SQLUSER SQLPASSWORD [SQLDATABASE]")
  199. print()
  200. print(" TARGETFILE.pmg The target database file to write to")
  201. print(" SQLHOST The Part-DB MySQL hostname")
  202. print(" SQLUSER The Part-DB MySQL username")
  203. print(" SQLPASSWORD The Part-DB MySQL password")
  204. print(" SQLDATABASE The Part-DB MySQL database (default: partdb)")
  205. def main():
  206. try:
  207. (opts, args) = getopt.getopt(sys.argv[1:],
  208. "h",
  209. [ "help", ])
  210. except getopt.GetoptError as e:
  211. printError(str(e))
  212. usage()
  213. return 1
  214. for (o, v) in opts:
  215. if o in ("-h", "--help"):
  216. usage()
  217. return 0
  218. if len(args) < 4 or len(args) > 5:
  219. usage()
  220. return 1
  221. targetfile = args[0]
  222. host = args[1]
  223. username = args[2]
  224. password = args[3]
  225. if len(args) > 4:
  226. database = args[4]
  227. else:
  228. database = "partdb"
  229. app = QApplication(sys.argv)
  230. conn = None
  231. db = None
  232. try:
  233. conn = mysql.connector.connect(user = username,
  234. password = password,
  235. host = host,
  236. database = database)
  237. db = Database(targetfile)
  238. res = importFromPartdb(db, conn)
  239. db.close(collectGarbage = (res == 0),
  240. updateRevision = (res == 0))
  241. conn.close()
  242. return res
  243. except (mysql.connector.Error, PartMgrError) as e:
  244. try:
  245. if db:
  246. db.close(commit = False)
  247. except Exception:
  248. pass
  249. try:
  250. if conn:
  251. conn.close()
  252. except Exception:
  253. pass
  254. print(e)
  255. return 1
  256. if __name__ == "__main__":
  257. sys.exit(main())