Нема описа

dailyreport.py 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  1. from openpyxl import load_workbook
  2. import pyodbc
  3. import time
  4. import sys
  5. from pprint import pprint
  6. import datetime
  7. from openpyxl.styles import NamedStyle, Font
  8. from copy import deepcopy
  9. from openpyxl.chart import LineChart, Reference, Series
  10. from openpyxl.chart.text import RichText
  11. from openpyxl.drawing.text import RichTextProperties,Paragraph,ParagraphProperties, CharacterProperties
  12. # Specifying the ODBC driver, server name, database, etc. directly
  13. #cnxn = None
  14. #cursor = None
  15. '''cursor.execute('select * from data_wb')
  16. rows = cursor.fetchall()
  17. for row in rows:
  18. print(row)'''
  19. wb = load_workbook(filename = './dctemplate2.xlsx')
  20. #wb.save(filename = './createfrompy.xlsx');
  21. def findCustomer(code):
  22. global cursor
  23. global env
  24. tempResult = []
  25. views = []
  26. if env == "prod":
  27. views = ["bel_master_view", "e_master_view", "mg_master_view", "v_master_view"]
  28. else:
  29. views = ["bel_master_view_dev", "e_master_view_dev", "mg_master_view_dev", "v_master_view_dev"]
  30. ts = []
  31. select = "distinct MI24 as unbStd, PRO1C as customer, cast(PRO8 as varchar) + ' ' + cast(PRO9 as varchar) + cast(PRO10 as varchar) + 'x' + cast(PRO11 as varchar ) + 'x' + cast(PRO12 as varchar) as sizestring, cast(PRO13 as varchar) + ' ' + cast(PRO14 as varchar) + ' ' + cast(PRO15 as varchar) + ' ' + cast(PRO16 as varchar) + ' ' + cast(PRO17 as varchar) + ' ' + cast(PRO18 as varchar) as spec"
  32. where = "PRO1 = '{0}'".format(code)
  33. for v in views:
  34. sqlQuery = "select {0} from {1} where {2}".format(select, v, where)
  35. ts.append(sqlQuery)
  36. merged = []
  37. for t in ts:
  38. merged += cursor.execute(t)
  39. return merged
  40. def findInMasterView(select, where):
  41. global cursor
  42. global env
  43. tempResult = []
  44. views = []
  45. if env == "prod":
  46. views = ["bel_master_view", "e_master_view", "mg_master_view", "v_master_view"]
  47. else:
  48. views = ["bel_master_view_dev", "e_master_view_dev", "mg_master_view_dev", "v_master_view_dev"]
  49. ts = []
  50. for v in views:
  51. sqlQuery = "select {0} from {1} where {2}".format(select, v, where)
  52. ts.append(sqlQuery);
  53. qj = ';'.join(ts)
  54. #print(qj)
  55. merged = []
  56. for t in ts:
  57. merged += cursor.execute(t)
  58. return merged
  59. def updateLength(lotno):
  60. global cursor
  61. global wb
  62. q = "select * from data_wb where lot_no = '{0}' and judgement='OK' order by row_no asc".format(lotno)
  63. #print(q);
  64. return cursor.execute(q)
  65. def updateUb(lotno):
  66. global cursor
  67. global wb
  68. q = "select * from data_wb where lot_no = '{0}' and judgement='NG' order by row_no asc".format(lotno)
  69. #print(q);
  70. return cursor.execute(q)
  71. def getMachineName(machineId):
  72. global cursor
  73. q = "select * from machines_wb where id = {0}".format(machineId)
  74. #print(q)
  75. cursor.execute(q)
  76. rs = cursor.fetchall()
  77. return rs[0].name
  78. def getMachineDetail(machineId):
  79. global cursor
  80. q = "select * from machines_wb where id = {0}".format(machineId)
  81. #print(q)
  82. cursor.execute(q)
  83. rs = cursor.fetchall()
  84. return rs[0]
  85. def createDayChart(chartTitle):
  86. global wb
  87. ws = wb['Chart']
  88. wsd = wb['Daily Check']
  89. c1 = LineChart()
  90. c1.legend.position = "t";
  91. c1.title = chartTitle
  92. c1.title.font = Font(underline=Font.UNDERLINE_SINGLE)
  93. c1.y_axis.title = "Std. Weight(g)"
  94. c1.y_axis.number_format = "#,##0.00"
  95. c1.x_axis.title = "Date"
  96. c1.width = 30
  97. c1.height = 10
  98. #print(len(values))
  99. MINDATACOL = 2
  100. MAXDATACOL = 2 + 31
  101. lt = Reference(wsd, min_col=3, max_col=33, min_row=5, max_row=5)
  102. std = Reference(wsd, min_col=MINDATACOL, max_col=MAXDATACOL, min_row=6, max_row=6)
  103. data = Reference(wsd, min_col=MINDATACOL, max_col=MAXDATACOL, min_row=10, max_row=10)
  104. maxUB = Reference(wsd, min_col=MINDATACOL, max_col=MAXDATACOL, min_row=15, max_row=15)
  105. minUB = Reference(wsd, min_col=MINDATACOL, max_col=MAXDATACOL, min_row=21, max_row=21)
  106. avgUB = Reference(wsd, min_col=MINDATACOL, max_col=MAXDATACOL, min_row=26, max_row=26)
  107. vsStd = Reference(wsd, min_col=MINDATACOL, max_col=MAXDATACOL, min_row=32, max_row=32)
  108. vsStd2 = Reference(wsd, min_col=MINDATACOL, max_col=MAXDATACOL, min_row=37, max_row=37)
  109. c1.add_data(data, from_rows=True, titles_from_data=True)
  110. c1.add_data(maxUB, from_rows=True, titles_from_data=True)
  111. c1.add_data(minUB, from_rows=True, titles_from_data=True)
  112. c1.add_data(avgUB, from_rows=True, titles_from_data=True)
  113. c1.add_data(vsStd, from_rows=True, titles_from_data=True)
  114. c1.add_data(vsStd2, from_rows=True, titles_from_data=True)
  115. c1.add_data(std, from_rows=True, titles_from_data=True)
  116. s1 = c1.series[0]
  117. s1.marker.symbol = "triangle"
  118. s1.graphicalProperties.line.noFill = True
  119. s1 = c1.series[1]
  120. s1.marker.symbol = "circle"
  121. s1.graphicalProperties.line.noFill = True
  122. s1 = c1.series[2]
  123. s1.marker.symbol = "square"
  124. s1.graphicalProperties.line.noFill = True
  125. s1 = c1.series[3]
  126. s1.marker.symbol = "auto"
  127. s1.graphicalProperties.line.noFill = True
  128. s1 = c1.series[4]
  129. s1.marker.symbol = "diamond"
  130. s1.graphicalProperties.line.noFill = True
  131. s1 = c1.series[5]
  132. s1.marker.symbol = "auto"
  133. s1.graphicalProperties.line.noFill = True
  134. s1 = c1.series[6]
  135. #s1.marker.symbol = "diamond"
  136. #s1.graphicalProperties.line.noFill = True
  137. c1.set_categories(lt)
  138. '''c1.x_axis.txPr = RichText(bodyPr=RichTextProperties(anchor="ctr",anchorCtr="1",rot="-2700000",
  139. spcFirstLastPara="1",vertOverflow="ellipsis",wrap="square"),
  140. p=[Paragraph(pPr=ParagraphProperties(defRPr=CharacterProperties()), endParaRPr=CharacterProperties())])'''
  141. ws.add_chart(c1, "b3")
  142. #pass
  143. def createNightChart(chartTitle):
  144. global wb
  145. ws = wb['Chart']
  146. wsd = wb['Daily Check']
  147. c1 = LineChart()
  148. c1.legend.position = "t";
  149. c1.title = chartTitle
  150. c1.title.font = Font(underline=Font.UNDERLINE_SINGLE)
  151. c1.y_axis.title = "Std. Weight(g)"
  152. c1.y_axis.number_format = "#,##0.00"
  153. c1.x_axis.title = "Date"
  154. c1.width = 30
  155. c1.height = 10
  156. #print(len(values))
  157. MINDATACOL = 2
  158. MAXDATACOL = 2 + 31
  159. lt = Reference(wsd, min_col=3, max_col=33, min_row=5, max_row=5)
  160. std = Reference(wsd, min_col=MINDATACOL, max_col=MAXDATACOL, min_row=6, max_row=6)
  161. #begin data
  162. data = Reference(wsd, min_col=MINDATACOL, max_col=MAXDATACOL, min_row=51, max_row=51)
  163. maxUB = Reference(wsd, min_col=MINDATACOL, max_col=MAXDATACOL, min_row=56, max_row=56)
  164. minUB = Reference(wsd, min_col=MINDATACOL, max_col=MAXDATACOL, min_row=62, max_row=62)
  165. avgUB = Reference(wsd, min_col=MINDATACOL, max_col=MAXDATACOL, min_row=67, max_row=67)
  166. vsStd = Reference(wsd, min_col=MINDATACOL, max_col=MAXDATACOL, min_row=73, max_row=73)
  167. vsStd2 = Reference(wsd, min_col=MINDATACOL, max_col=MAXDATACOL, min_row=78, max_row=78)
  168. c1.add_data(data, from_rows=True, titles_from_data=True)
  169. c1.add_data(maxUB, from_rows=True, titles_from_data=True)
  170. c1.add_data(minUB, from_rows=True, titles_from_data=True)
  171. c1.add_data(avgUB, from_rows=True, titles_from_data=True)
  172. c1.add_data(vsStd, from_rows=True, titles_from_data=True)
  173. c1.add_data(vsStd2, from_rows=True, titles_from_data=True)
  174. c1.add_data(std, from_rows=True, titles_from_data=True)
  175. s1 = c1.series[0]
  176. s1.marker.symbol = "triangle"
  177. s1.graphicalProperties.line.noFill = True
  178. s1 = c1.series[1]
  179. s1.marker.symbol = "circle"
  180. s1.graphicalProperties.line.noFill = True
  181. s1 = c1.series[2]
  182. s1.marker.symbol = "square"
  183. s1.graphicalProperties.line.noFill = True
  184. s1 = c1.series[3]
  185. s1.marker.symbol = "auto"
  186. s1.graphicalProperties.line.noFill = True
  187. s1 = c1.series[4]
  188. s1.marker.symbol = "diamond"
  189. s1.graphicalProperties.line.noFill = True
  190. s1 = c1.series[5]
  191. s1.marker.symbol = "auto"
  192. s1.graphicalProperties.line.noFill = True
  193. s1 = c1.series[6]
  194. #s1.marker.symbol = "diamond"
  195. #s1.graphicalProperties.line.noFill = True
  196. c1.set_categories(lt)
  197. '''c1.x_axis.txPr = RichText(bodyPr=RichTextProperties(anchor="ctr",anchorCtr="1",rot="-2700000",
  198. spcFirstLastPara="1",vertOverflow="ellipsis",wrap="square"),
  199. p=[Paragraph(pPr=ParagraphProperties(defRPr=CharacterProperties()), endParaRPr=CharacterProperties())])'''
  200. ws.add_chart(c1, "b22")
  201. def createExcel(codeno, filters):
  202. global cursor
  203. global wb
  204. ws = wb["Daily Check"]
  205. machineName = getMachineName(codeno)
  206. machineDetail = getMachineDetail(codeno)
  207. ws["C2"].value = machineName
  208. ws["C3"].value = filters.replace("created_at between", "").replace("and", "to").replace("'", "")
  209. ws["C43"].value = machineName
  210. ws["C44"].value = filters.replace("created_at between", "").replace("and", "to").replace("'", "")
  211. ws["C4"].value = machineDetail.machineStd;
  212. if filters is not None:
  213. q = "select * from daily_checks_wb where machine_id = '{0}' and {1}".format(codeno, filters)
  214. else:
  215. q = "select * from daily_checks_wb where machine_id = '{0}'".format(codeno)
  216. #print(q)
  217. cursor.execute(q)
  218. rows = cursor.fetchall()
  219. #print(rows)
  220. a0740 = [];
  221. a1230 = []
  222. a1700 = []
  223. a0030 = []
  224. a1940 = []
  225. a0500 = []
  226. for i in range(31):
  227. a0740.append([])
  228. a1230.append([])
  229. a1700.append([])
  230. a0030.append([])
  231. a1940.append([])
  232. a0500.append([])
  233. for r in rows:
  234. i = r.created_at.day
  235. if r.shift == "07.40":
  236. a0740[i-1].append(r)
  237. if r.shift == "12.30":
  238. a1230[i-1].append(r)
  239. if r.shift == "17.00":
  240. #a1700.append(r)
  241. a1700[i-1].append(r)
  242. if r.shift == "00.30":
  243. a0030[i-1].append(r)
  244. if r.shift == "19.40":
  245. a1940[i-1].append(r)
  246. if r.shift == "05.00":
  247. a0500[i-1].append(r)
  248. #print(a0740)
  249. for idx, val in enumerate(a0740):
  250. j = 7
  251. k = 12
  252. users = []
  253. #print(val)
  254. ok = 0
  255. ng = 0
  256. for v in val:
  257. #print(j , idx + )
  258. #print(v.judgment)
  259. if v.judgement == "OK":
  260. ok += 1
  261. if ok > 3:
  262. continue
  263. ws.cell(row = j , column = idx + 3 , value = v.weight)
  264. j += 1
  265. if v.judgement == "NG":
  266. ng += 1
  267. if ng > 3:
  268. continue
  269. ws.cell(row = k , column = idx + 3 , value = v.weight)
  270. k += 1
  271. #print(v.empid)
  272. if v.empid not in users:
  273. #print("insert in users")
  274. users.append(v.empid)
  275. #print(val)
  276. if len(val) > 0:
  277. q = "select * from users where empid in ({0})".format(",".join("'{0}'".format(w) for w in users))
  278. #print(q)
  279. cursor.execute(q)
  280. us = cursor.fetchall()
  281. names = [x.fname + " " + x.lname for x in us]
  282. ws.cell(row = 17, column = idx + 3, value = ",".join(names))
  283. for idx, val in enumerate(a1230):
  284. j = 18
  285. k = 23
  286. users = []
  287. ok = ng = 0
  288. for v in val:
  289. #print(j , idx)
  290. if v.judgement == "OK":
  291. ok += 1
  292. if ok > 3:
  293. continue
  294. ws.cell(row = j , column = idx + 3 , value = v.weight)
  295. j += 1
  296. if v.judgement == "NG":
  297. ng += 1
  298. if ng > 3:
  299. continue
  300. ws.cell(row = k , column = idx + 3 , value = v.weight)
  301. k += 1
  302. if v.empid not in users:
  303. users.append(v.empid)
  304. if len(val) > 0:
  305. q = "select * from users where empid in ({0})".format(",".join("'{0}'".format(w) for w in users))
  306. #print(q)
  307. cursor.execute(q)
  308. us = cursor.fetchall()
  309. names = [x.fname + " " + x.lname for x in us]
  310. ws.cell(row = 28, column = idx + 3, value = ",".join(names))
  311. for idx, val in enumerate(a1700):
  312. j = 29
  313. k = 34
  314. users = []
  315. ok = ng = 0
  316. for v in val:
  317. #print(j , idx)
  318. if v.judgement == "OK":
  319. ok += 1
  320. if ok > 3:
  321. continue
  322. ws.cell(row = j , column = idx + 3 , value = v.weight)
  323. j += 1
  324. if v.judgement == "NG":
  325. ng += 1
  326. if ng > 3:
  327. continue
  328. ws.cell(row = k , column = idx + 3 , value = v.weight)
  329. k += 1
  330. if v.empid not in users:
  331. users.append(v.empid)
  332. if len(val) > 0:
  333. q = "select * from users where empid in ({0})".format(",".join("'{0}'".format(w) for w in users))
  334. #print(q)
  335. cursor.execute(q)
  336. us = cursor.fetchall()
  337. names = [x.fname + " " + x.lname for x in us]
  338. ws.cell(row = 39, column = idx + 3, value = ",".join(names))
  339. for idx, val in enumerate(a1940):
  340. j = 47
  341. k = 53
  342. users = []
  343. ok = ng = 0
  344. for v in val:
  345. #print(j , idx)
  346. if v.judgement == "OK":
  347. ok += 1
  348. if ok > 3:
  349. continue
  350. ws.cell(row = j , column = idx + 3 , value = v.weight)
  351. j += 1
  352. if v.judgement == "NG":
  353. ng += 1
  354. if ng > 3:
  355. continue
  356. ws.cell(row = k , column = idx + 3 , value = v.weight)
  357. k += 1
  358. if v.empid not in users:
  359. users.append(v.empid)
  360. if len(val) > 0:
  361. q = "select * from users where empid in ({0})".format(",".join("'{0}'".format(w) for w in users))
  362. #print(q)
  363. cursor.execute(q)
  364. us = cursor.fetchall()
  365. names = [x.fname + " " + x.lname for x in us]
  366. ws.cell(row = 58, column = idx + 3, value = ",".join(names))
  367. for idx, val in enumerate(a0030):
  368. j = 59
  369. k = 64
  370. users = []
  371. ok = ng = 0
  372. for v in val:
  373. #print(j , idx)
  374. if v.judgement == "OK":
  375. ok += 1
  376. if ok > 3:
  377. continue
  378. ws.cell(row = j , column = idx + 3 , value = v.weight)
  379. j += 1
  380. if v.judgement == "NG":
  381. ng += 1
  382. if ng > 3:
  383. continue
  384. ws.cell(row = k , column = idx + 3 , value = v.weight)
  385. k += 1
  386. if v.empid not in users:
  387. users.append(v.empid)
  388. if len(val) > 0:
  389. q = "select * from users where empid in ({0})".format(",".join("'{0}'".format(w) for w in users))
  390. #print(q)
  391. cursor.execute(q)
  392. us = cursor.fetchall()
  393. names = [x.fname + " " + x.lname for x in us]
  394. ws.cell(row = 69, column = idx + 3, value = ",".join(names))
  395. for idx, val in enumerate(a0500):
  396. j = 70
  397. k = 75
  398. users = []
  399. ok = ng = 0
  400. for v in val:
  401. #print(j , idx)
  402. if v.judgement == "OK":
  403. ok += 1
  404. if ok > 3:
  405. continue
  406. ws.cell(row = j , column = idx + 3 , value = v.weight)
  407. j += 1
  408. if v.judgement == "NG":
  409. ng += 1
  410. if ng > 3:
  411. continue
  412. ws.cell(row = k , column = idx + 3 , value = v.weight)
  413. k += 1
  414. if v.empid not in users:
  415. users.append(v.empid)
  416. if len(val) > 0:
  417. q = "select * from users where empid in ({0})".format(",".join("'{0}'".format(w) for w in users))
  418. #print(q)
  419. cursor.execute(q)
  420. us = cursor.fetchall()
  421. names = [x.fname + " " + x.lname for x in us]
  422. ws.cell(row = 80, column = idx + 3, value = ",".join(names))
  423. createDayChart("Daily Check of Day Shift ({0})".format(machineName))
  424. createNightChart("Daily Check of Night Shift ({0})".format(machineName))
  425. ut = int(time.time())
  426. filename = "dc{0}.xlsx".format(str(ut))
  427. outfile = './public/excel/'+filename
  428. wb.save(filename = outfile)
  429. print(filename)
  430. if __name__ == "__main__":
  431. env = sys.argv[1]
  432. machine = sys.argv[2]
  433. #print(len(sys.argv))
  434. df = None
  435. if len(sys.argv) == 4:
  436. filter = sys.argv[3]
  437. #import ast
  438. #df = ast.literal_eval(filter)
  439. #print(df['date'])
  440. df = filter
  441. global cnxn
  442. global cursor
  443. if env == "prod":
  444. cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER=192.168.0.253;DATABASE=OB2011DB;UID=user1;PWD=1234')
  445. cursor = cnxn.cursor()
  446. else:
  447. cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER=localhost;DATABASE=OB2011DB;UID=admin;PWD=1234')
  448. cursor = cnxn.cursor()
  449. #print("code is "+code)
  450. createExcel(machine, df)