ERP.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. # TODO 資料表是否會重複開啟? -> def?
  2. from logging import info
  3. from datetime import datetime
  4. from flask import Flask, render_template, request, jsonify
  5. import pymysql
  6. # from flask_sqlalchemy import SQLAlchemy
  7. # from sqlalchemy.orm import query, session
  8. from flask_migrate import Migrate
  9. import time
  10. from app import create_app
  11. #app, db, mqtt = create_app()
  12. app = Flask(__name__)
  13. # # app.config['SQLALCHEMY_DATABASE_URI'] = "mysql+pymysql://root:g53743001@localhost:3306/coffeetest"
  14. # app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
  15. # db = SQLAlchemy(app)
  16. # # db.init_app(app)
  17. # Migrate(app, db)
  18. # 首頁測試
  19. @app.route('/')
  20. def index():
  21. return render_template('index.html')
  22. # 使用者名稱測試
  23. @app.route('/user/<name>')
  24. def user(name):
  25. return render_template('hello.html', name=name)
  26. # 建立 BOM 表
  27. @app.route('/create_BOM', methods=['GET', 'POST'])
  28. def create_BOM():
  29. # 開啟本機 erp 資料庫
  30. mydb = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='g53743001', database='erp', charset='utf8')
  31. # mydb = pymysql.connect(host='52.69.200.169', port=3306, user='coffee', password='skyeye', database='Coffee', charset='utf8')
  32. mycursor = mydb.cursor()
  33. # 獲取欄位資料
  34. # sql = '''SELECT * FROM table_component_name'''
  35. sql = '''SELECT 內部料號, 會計科目表.會計科目_名稱, 類別表.類別_名稱, 流水號, 供應商.公司, 名稱
  36. FROM (
  37. (零件表
  38. INNER JOIN 供應商
  39. ON 供應商.供應商 = 零件表.供應商
  40. )
  41. INNER JOIN 類別表
  42. ON 類別表.類別_編號 = 零件表.類別
  43. )
  44. INNER JOIN 會計科目表
  45. ON 會計科目表.會計科目_編號 = 零件表.會計科目
  46. '''
  47. mycursor.execute(sql)
  48. content = mycursor.fetchall()
  49. #獲取欄位名稱
  50. sql = "SHOW FIELDS FROM 零件表"
  51. mycursor.execute(sql)
  52. labels = mycursor.fetchall()
  53. # print("labels: ", labels)
  54. # # labels: (('產品', 'varchar(4)', 'YES', '', None, ''), ('系統', 'varchar(5)', 'YES', '', None, ''),
  55. labels = [g[0] for g in labels]
  56. # print("labels: ", labels)
  57. # # labels: ['產品', '系統', '系統圖號', '狀態', '進貨狀態', '序號', '組序號',
  58. # 獲取會計科目欄位
  59. AccountingSubjects = "SELECT * FROM 會計科目表"
  60. mycursor.execute(AccountingSubjects)
  61. AccountingSubjects_data = mycursor.fetchall()
  62. # print("AccountingSubjects_data", AccountingSubjects_data)
  63. # # (('01', '成品'), ('02', '系統成品'), ('03', '模組半成品'), ('04', '零件'), ('05', '工具'))
  64. # 獲取類別欄位
  65. Category = "SELECT * FROM 類別表"
  66. mycursor.execute(Category)
  67. Category_data = mycursor.fetchall()
  68. # 獲取供應商欄位
  69. Supplier = "SELECT * FROM 供應商"
  70. mycursor.execute(Supplier)
  71. Supplier_data = mycursor.fetchall()
  72. # 獲取零件名稱
  73. Component = "SELECT 名稱 FROM 零件表"
  74. mycursor.execute(Component)
  75. Component_data = mycursor.fetchall()
  76. return render_template('create_BOM.html', title='建立 BOM 表', **locals())
  77. # 新增零件清單項目
  78. @app.route('/create_component_table_item', methods=['GET', 'POST'])
  79. def create_component_table_item():
  80. # 開啟本機 erp 資料庫
  81. mydb = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='g53743001', database='erp', charset='utf8')
  82. # mydb = pymysql.connect(host='52.69.200.169', port=3306, user='coffee', password='skyeye', database='Coffee', charset='utf8')
  83. mycursor = mydb.cursor()
  84. # # --- 新增零件用 -----------------------------------------------------------------------------------------
  85. # 獲取會計科目欄位
  86. AccountingSubjects = "SELECT * FROM 會計科目表"
  87. mycursor.execute(AccountingSubjects)
  88. AccountingSubjects_data = mycursor.fetchall()
  89. # print("AccountingSubjects_data", AccountingSubjects_data)
  90. # # (('01', '成品'), ('02', '系統成品'), ('03', '模組半成品'), ('04', '零件'), ('05', '工具'))
  91. # 獲取類別欄位
  92. Category = "SELECT * FROM 類別表"
  93. mycursor.execute(Category)
  94. Category_data = mycursor.fetchall()
  95. # 獲取供應商欄位
  96. Supplier = "SELECT * FROM 供應商"
  97. mycursor.execute(Supplier)
  98. Supplier_data = mycursor.fetchall()
  99. # 獲取零件名稱
  100. Component = "SELECT 名稱 FROM 零件表"
  101. mycursor.execute(Component)
  102. Component_data = mycursor.fetchall()
  103. # # --- 新增會計科目項目用 -----------------------------------------------------------------------------------------
  104. accountingsubject_sql = 'SELECT 會計科目_編號 FROM 會計科目表 ORDER BY 會計科目_編號 DESC LIMIT 1'
  105. # print("accountingsubject_sql: ", accountingsubject_sql)
  106. mycursor.execute(accountingsubject_sql)
  107. content = mycursor.fetchall()
  108. try:
  109. # print("content: ", content[0][0], type(content[0][0]))
  110. new_as_sn = int(content[0][0])+1
  111. new_as_sn = '{0:02d}'.format(new_as_sn)
  112. except IndexError:
  113. new_as_sn = '01'
  114. # # --- 新增類別項目用 -----------------------------------------------------------------------------------------
  115. category_sql = 'SELECT 類別_編號 FROM 類別表 ORDER BY 類別_編號 DESC LIMIT 1'
  116. mycursor.execute(category_sql)
  117. content = mycursor.fetchall()
  118. try:
  119. new_ca_sn = int(content[0][0])+1
  120. new_ca_sn = '{0:02d}'.format(new_ca_sn)
  121. except IndexError:
  122. new_ca_sn = '01'
  123. # # --- 新增供應商項目用 -----------------------------------------------------------------------------------------
  124. supplier_sql = 'SELECT 供應商 FROM 供應商 ORDER BY 供應商 DESC LIMIT 1'
  125. mycursor.execute(supplier_sql)
  126. content = mycursor.fetchall()
  127. try:
  128. print("content[0][0][1:]: ", content[0][0][1:])
  129. new_su_sn = int(content[0][0][1:])+1
  130. new_su_sn = content[0][0][:1] + '{0:02d}'.format(new_su_sn)
  131. except IndexError:
  132. new_su_sn = 'T01'
  133. return render_template('create_component_table_item.html', title='新增零件', **locals())
  134. # ERP 測試 四層下拉式選單關聯
  135. @app.route('/drop_down_list', methods=['GET', 'POST'])
  136. def drop_down_list():
  137. conn = pymysql.connect(
  138. host='52.69.200.169',
  139. port=3306,
  140. user='coffee',
  141. password='skyeye',
  142. database='Coffee',
  143. charset='utf8'
  144. )
  145. cur = conn.cursor()
  146. #獲取欄位資料
  147. sql = "select * from product_info"
  148. cur.execute(sql)
  149. content = cur.fetchall()
  150. #獲取欄位名稱
  151. sql = "SHOW FIELDS FROM product_info"
  152. cur.execute(sql)
  153. labels = cur.fetchall()
  154. # print("labels: ", labels) # labels: (('產品', 'varchar(4)', 'YES', '', None, ''), ('系統', 'varchar(5)', 'YES', '', None, ''),
  155. labels = [g[0] for g in labels]
  156. # print("labels: ", labels) # labels: ['產品', '系統', '系統圖號', '狀態', '進貨狀態', '序號', '組序號',
  157. return render_template('drop_down_list.html', labels=labels, content=content)
  158. # ERP 測試
  159. @app.route('/search', methods=['GET', 'POST'])
  160. def search():
  161. # 開啟本機 erp 資料庫
  162. mydb = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='g53743001', database='erp', charset='utf8')
  163. # mydb = pymysql.connect(host='52.69.200.169', port=3306, user='coffee', password='skyeye', database='Coffee', charset='utf8')
  164. mycursor = mydb.cursor()
  165. # 獲取欄位資料
  166. # sql = '''SELECT * FROM table_component_name'''
  167. sql = '''SELECT 內部料號, 會計科目表.會計科目_名稱, 類別表.類別_名稱, 流水號, 供應商.公司, 名稱
  168. FROM (
  169. (零件表
  170. INNER JOIN 供應商
  171. ON 供應商.供應商 = 零件表.供應商
  172. )
  173. INNER JOIN 類別表
  174. ON 類別表.類別_編號 = 零件表.類別
  175. )
  176. INNER JOIN 會計科目表
  177. ON 會計科目表.會計科目_編號 = 零件表.會計科目
  178. '''
  179. mycursor.execute(sql)
  180. content = mycursor.fetchall()
  181. #獲取欄位名稱
  182. sql = "SHOW FIELDS FROM 零件表"
  183. mycursor.execute(sql)
  184. labels = mycursor.fetchall()
  185. # print("labels: ", labels)
  186. # # labels: (('產品', 'varchar(4)', 'YES', '', None, ''), ('系統', 'varchar(5)', 'YES', '', None, ''),
  187. labels = [g[0] for g in labels]
  188. # print("labels: ", labels)
  189. # # labels: ['產品', '系統', '系統圖號', '狀態', '進貨狀態', '序號', '組序號',
  190. # 獲取會計科目欄位
  191. AccountingSubjects = "SELECT * FROM 會計科目表"
  192. mycursor.execute(AccountingSubjects)
  193. AccountingSubjects_data = mycursor.fetchall()
  194. # print("AccountingSubjects_data", AccountingSubjects_data)
  195. # # (('01', '成品'), ('02', '系統成品'), ('03', '模組半成品'), ('04', '零件'), ('05', '工具'))
  196. # 獲取類別欄位
  197. Category = "SELECT * FROM 類別表"
  198. mycursor.execute(Category)
  199. Category_data = mycursor.fetchall()
  200. # 獲取供應商欄位
  201. Supplier = "SELECT * FROM 供應商"
  202. mycursor.execute(Supplier)
  203. Supplier_data = mycursor.fetchall()
  204. # 獲取零件名稱
  205. Component = "SELECT 名稱 FROM 零件表"
  206. mycursor.execute(Component)
  207. Component_data = mycursor.fetchall()
  208. return render_template('search.html', title = 'ERP Search', **locals())
  209. # 找到 serial number 最大值後 +1
  210. @app.route('/sn_get/<AccountSubject>/<Category>', methods=['GET', 'POST'])
  211. def zn_get(AccountSubject, Category):
  212. # 開啟本機 erp 資料庫
  213. mydb = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='g53743001', database='erp', charset='utf8')
  214. mycursor = mydb.cursor()
  215. # 取得流水號最大值
  216. sql = 'SELECT 流水號 FROM 零件表 '
  217. sql += 'WHERE 會計科目 = "' + AccountSubject + '" AND 類別 = "' + Category + '" ORDER BY 流水號 DESC LIMIT 1'
  218. print("sql: ", sql)
  219. mycursor.execute(sql)
  220. content = mycursor.fetchall()
  221. try:
  222. print("content: ", content[0][0], type(content[0][0]))
  223. new_sn = int(content[0][0])+1
  224. new_sn = '{0:04d}'.format(new_sn)
  225. # new_sn = content[0][0].zfill(4)
  226. except IndexError:
  227. new_sn = '0001'
  228. return jsonify({"new_sn":new_sn})
  229. # 從資料表找出符合篩選條件之資料
  230. @app.route('/sql_get', methods=['GET'])
  231. def sql_get():
  232. mydb = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='g53743001', database='erp', charset='utf8')
  233. mycursor = mydb.cursor()
  234. # 取得網頁傳回來的 SQL 指令
  235. # sql = "SELECT * FROM 零件表 WHERE 會計科目 = 04"
  236. info = request.args.to_dict('sql')
  237. sql = info['sql']
  238. # print("sql: ", sql)
  239. mycursor.execute(sql)
  240. sql_data = mycursor.fetchall()
  241. # 獲取欄位名稱
  242. # 拆解取得資料表資料
  243. # print("sql_data: ", sql_data)
  244. # # sql_data: (('04030001T01', '04', '03', '0001', 'T01', '電阻-18Ω'), ... )
  245. # Rita 這裡在測試日期格式修改, 優化後調整
  246. # if 'INNER JOIN 庫存表' in sql:
  247. # labels = []
  248. # for g in sql_data:
  249. # # timeArray = time.strptime(a, "%Y-%m-%d %H:%M:%S")
  250. # print("g[0]: ", g[0], type(g[0]), g[0].strftime("%Y-%m-%d %H:%M:%S"))
  251. # timeString = g[0].strftime("%Y-%m-%d %H:%M:%S")
  252. # print("timeString: ", timeString, type(timeString))
  253. # labels.append(timeString)
  254. # # labels.append(time.strftime("%Y-%m-%d %H:%M:%S"))
  255. # labels.append(g[1])
  256. # labels.append(g[2])
  257. # else:
  258. # labels = [g for g in sql_data]
  259. labels = [g for g in sql_data]
  260. # # labels g[1]~g[5]: [('04030001T01', '04', '03', '0001', 'T01', '電阻-18Ω'), ...]
  261. print("labels: ", labels)
  262. # 取得欄位資料
  263. if 'INNER JOIN 規格表' in sql:
  264. sql_field = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '規格表'"
  265. elif 'INNER JOIN 庫存表' in sql:
  266. sql_field = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '庫存表' LIMIT 3"
  267. elif 'INNER JOIN 銷貨表' in sql:
  268. sql_field = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '銷貨表'"
  269. elif 'INNER JOIN 進貨表' in sql:
  270. sql_field = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '進貨表'"
  271. # elif 'T' in sql:
  272. # table_name = sql[sql.find('`')+1:]
  273. # table_name = sql[:sql.find('`')]
  274. # sql_field = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '" + table_name + "'"
  275. else:
  276. sql_field = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '零件表'"
  277. print("sql_field: ", sql_field)
  278. mycursor.execute(sql_field)
  279. sql_field = mycursor.fetchall()
  280. print("sql_field: ", sql_field)
  281. mydb.commit()
  282. return jsonify({"sql_data":sql_data,
  283. "labels":labels,
  284. "sql_field":sql_field})
  285. #自動關閉所有未使用、掛著的連接
  286. @app.teardown_appcontext
  287. def shutdown_session(exception=None):
  288. # db.session.remove()
  289. pass
  290. if __name__ == '__main__':
  291. # Rita 測試 Benson 網頁 http://192.168.50.65:5006/login
  292. app.run(debug=False, threaded=True, port=5010)
  293. #app.run(debug=False, threaded=True, port=5006, host='0.0.0.0')
  294. #使用WSGI開關,避免出現WARNING: This is a development server. d not use it in a production deployment.Use a production WSGI server instead.
  295. # server = pywsgi.WSGIServer(('0.0.0.0', 5006), app)
  296. # server.serve_forever()