ERP.py 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. # 新增零件清單項目
  27. @app.route('/create_component_table_item', methods=['GET', 'POST'])
  28. def create_component_table_item():
  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. AccountingSubjects = "SELECT * FROM 會計科目表"
  35. mycursor.execute(AccountingSubjects)
  36. AccountingSubjects_data = mycursor.fetchall()
  37. # print("AccountingSubjects_data", AccountingSubjects_data)
  38. # # (('01', '成品'), ('02', '系統成品'), ('03', '模組半成品'), ('04', '零件'), ('05', '工具'))
  39. # 獲取類別欄位
  40. Category = "SELECT * FROM 類別表"
  41. mycursor.execute(Category)
  42. Category_data = mycursor.fetchall()
  43. # 獲取供應商欄位
  44. Supplier = "SELECT * FROM 供應商"
  45. mycursor.execute(Supplier)
  46. Supplier_data = mycursor.fetchall()
  47. # 獲取零件名稱
  48. Component = "SELECT 名稱 FROM 零件表"
  49. mycursor.execute(Component)
  50. Component_data = mycursor.fetchall()
  51. return render_template('create_component_table_item.html', title='新增零件', **locals())
  52. # ERP 測試 四層下拉式選單關聯
  53. @app.route('/drop_down_list', methods=['GET', 'POST'])
  54. def drop_down_list():
  55. conn = pymysql.connect(
  56. host='52.69.200.169',
  57. port=3306,
  58. user='coffee',
  59. password='skyeye',
  60. database='Coffee',
  61. charset='utf8'
  62. )
  63. cur = conn.cursor()
  64. #獲取欄位資料
  65. sql = "select * from product_info"
  66. cur.execute(sql)
  67. content = cur.fetchall()
  68. #獲取欄位名稱
  69. sql = "SHOW FIELDS FROM product_info"
  70. cur.execute(sql)
  71. labels = cur.fetchall()
  72. # print("labels: ", labels) # labels: (('產品', 'varchar(4)', 'YES', '', None, ''), ('系統', 'varchar(5)', 'YES', '', None, ''),
  73. labels = [g[0] for g in labels]
  74. # print("labels: ", labels) # labels: ['產品', '系統', '系統圖號', '狀態', '進貨狀態', '序號', '組序號',
  75. return render_template('drop_down_list.html', labels=labels, content=content)
  76. # ERP 測試
  77. @app.route('/search', methods=['GET', 'POST'])
  78. def search():
  79. # 開啟本機 erp 資料庫
  80. mydb = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='g53743001', database='erp', charset='utf8')
  81. # mydb = pymysql.connect(host='52.69.200.169', port=3306, user='coffee', password='skyeye', database='Coffee', charset='utf8')
  82. mycursor = mydb.cursor()
  83. # 獲取欄位資料
  84. # sql = '''SELECT * FROM table_component_name'''
  85. sql = '''SELECT 內部料號, 會計科目表.會計科目_名稱, 類別表.類別_名稱, 流水號, 供應商.公司, 名稱
  86. FROM (
  87. (零件表
  88. INNER JOIN 供應商
  89. ON 供應商.供應商 = 零件表.供應商
  90. )
  91. INNER JOIN 類別表
  92. ON 類別表.類別_編號 = 零件表.類別
  93. )
  94. INNER JOIN 會計科目表
  95. ON 會計科目表.會計科目_編號 = 零件表.會計科目
  96. '''
  97. mycursor.execute(sql)
  98. content = mycursor.fetchall()
  99. #獲取欄位名稱
  100. sql = "SHOW FIELDS FROM 零件表"
  101. mycursor.execute(sql)
  102. labels = mycursor.fetchall()
  103. # print("labels: ", labels)
  104. # # labels: (('產品', 'varchar(4)', 'YES', '', None, ''), ('系統', 'varchar(5)', 'YES', '', None, ''),
  105. labels = [g[0] for g in labels]
  106. # print("labels: ", labels)
  107. # # labels: ['產品', '系統', '系統圖號', '狀態', '進貨狀態', '序號', '組序號',
  108. # 獲取會計科目欄位
  109. AccountingSubjects = "SELECT * FROM 會計科目表"
  110. mycursor.execute(AccountingSubjects)
  111. AccountingSubjects_data = mycursor.fetchall()
  112. # print("AccountingSubjects_data", AccountingSubjects_data)
  113. # # (('01', '成品'), ('02', '系統成品'), ('03', '模組半成品'), ('04', '零件'), ('05', '工具'))
  114. # 獲取類別欄位
  115. Category = "SELECT * FROM 類別表"
  116. mycursor.execute(Category)
  117. Category_data = mycursor.fetchall()
  118. # 獲取供應商欄位
  119. Supplier = "SELECT * FROM 供應商"
  120. mycursor.execute(Supplier)
  121. Supplier_data = mycursor.fetchall()
  122. # 獲取零件名稱
  123. Component = "SELECT 名稱 FROM 零件表"
  124. mycursor.execute(Component)
  125. Component_data = mycursor.fetchall()
  126. return render_template('search.html', title = 'ERP Search', **locals())
  127. # 找到 serial number 最大值後 +1
  128. @app.route('/sn_get/<AccountSubject>/<Category>', methods=['GET', 'POST'])
  129. def zn_get(AccountSubject, Category):
  130. # 開啟本機 erp 資料庫
  131. mydb = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='g53743001', database='erp', charset='utf8')
  132. mycursor = mydb.cursor()
  133. # 取得流水號最大值
  134. sql = 'SELECT 流水號 FROM 零件表 '
  135. sql += 'WHERE 會計科目 = "' + AccountSubject + '" AND 類別 = "' + Category + '" ORDER BY 流水號 DESC LIMIT 1'
  136. print("sql: ", sql)
  137. mycursor.execute(sql)
  138. content = mycursor.fetchall()
  139. try:
  140. print("content: ", content[0][0], type(content[0][0]))
  141. new_sn = int(content[0][0])+1
  142. new_sn = '{0:04d}'.format(new_sn)
  143. # new_sn = content[0][0].zfill(4)
  144. except IndexError:
  145. new_sn = '0001'
  146. return jsonify({"new_sn":new_sn})
  147. # 從資料表找出符合篩選條件之資料
  148. @app.route('/sql_get', methods=['GET'])
  149. def sql_get():
  150. mydb = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='g53743001', database='erp', charset='utf8')
  151. mycursor = mydb.cursor()
  152. # 取得網頁傳回來的 SQL 指令
  153. # sql = "SELECT * FROM 零件表 WHERE 會計科目 = 04"
  154. info = request.args.to_dict('sql')
  155. sql = info['sql']
  156. print("sql: ", sql)
  157. mycursor.execute(sql)
  158. sql_data = mycursor.fetchall()
  159. # 獲取欄位名稱
  160. # 拆解取得資料表資料
  161. # print("sql_data: ", sql_data)
  162. # # sql_data: (('04030001T01', '04', '03', '0001', 'T01', '電阻-18Ω'), ... )
  163. # Rita 這裡在測試日期格式修改, 優化後調整
  164. # if 'INNER JOIN 庫存表' in sql:
  165. # labels = []
  166. # for g in sql_data:
  167. # # timeArray = time.strptime(a, "%Y-%m-%d %H:%M:%S")
  168. # print("g[0]: ", g[0], type(g[0]), g[0].strftime("%Y-%m-%d %H:%M:%S"))
  169. # timeString = g[0].strftime("%Y-%m-%d %H:%M:%S")
  170. # print("timeString: ", timeString, type(timeString))
  171. # labels.append(timeString)
  172. # # labels.append(time.strftime("%Y-%m-%d %H:%M:%S"))
  173. # labels.append(g[1])
  174. # labels.append(g[2])
  175. # else:
  176. # labels = [g for g in sql_data]
  177. labels = [g for g in sql_data]
  178. # # labels g[1]~g[5]: [('04030001T01', '04', '03', '0001', 'T01', '電阻-18Ω'), ...]
  179. print("labels: ", labels)
  180. # 取得欄位資料
  181. if 'INNER JOIN 規格表' in sql:
  182. sql_field = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '規格表'"
  183. elif 'INNER JOIN 庫存表' in sql:
  184. sql_field = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '庫存表' LIMIT 3"
  185. elif 'INNER JOIN 銷貨表' in sql:
  186. sql_field = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '銷貨表'"
  187. elif 'INNER JOIN 進貨表' in sql:
  188. sql_field = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '進貨表'"
  189. else:
  190. sql_field = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '零件表'"
  191. mycursor.execute(sql_field)
  192. sql_field = mycursor.fetchall()
  193. mydb.commit()
  194. return jsonify({"sql_data":sql_data,
  195. "labels":labels,
  196. "sql_field":sql_field})
  197. #自動關閉所有未使用、掛著的連接
  198. @app.teardown_appcontext
  199. def shutdown_session(exception=None):
  200. # db.session.remove()
  201. pass
  202. if __name__ == '__main__':
  203. # Rita 測試 Benson 網頁 http://192.168.50.65:5006/login
  204. app.run(debug=False, threaded=True, port=5010)
  205. #app.run(debug=False, threaded=True, port=5006, host='0.0.0.0')
  206. #使用WSGI開關,避免出現WARNING: This is a development server. d not use it in a production deployment.Use a production WSGI server instead.
  207. # server = pywsgi.WSGIServer(('0.0.0.0', 5006), app)
  208. # server.serve_forever()