ERP.py 77 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646
  1. # coding=UTF-8
  2. # This Python file uses the following encoding: utf-8
  3. # 1229 Test
  4. # TODO 資料表是否會重複開啟? -> def?
  5. from logging import info
  6. from datetime import datetime
  7. from operator import le
  8. import string
  9. from turtle import title
  10. from flask import Flask, make_response, render_template, request, jsonify, send_from_directory, redirect, session
  11. import pymysql
  12. from flask_migrate import Migrate
  13. import time
  14. # from app import create_app
  15. import pandas as pd
  16. from sqlalchemy import true
  17. import xlsxwriter
  18. import pdfkit
  19. from werkzeug.utils import secure_filename
  20. import os
  21. # import sys #reload()之前必須要引入模組
  22. # import importlib
  23. # importlib.reload(sys)
  24. app = Flask(__name__)
  25. # 首頁測試
  26. @app.route('/')
  27. def index():
  28. if 'user_id' in session:
  29. return render_template('index.html', params=locals())
  30. else:
  31. return render_template('login.html')
  32. @app.route('/logout')
  33. def logout():
  34. if 'user_id' in session:
  35. del session['user_id']
  36. del session['user_name']
  37. del session['user_manager']
  38. del session['user_is_RDmanager']
  39. del session['user_is_MEmanager']
  40. del session['user_is_PUmanager']
  41. return redirect('/')
  42. # 上傳測試 本機測試
  43. CURRENT_PATH = os.path.dirname(__file__)
  44. UPLOAD_FOLDER = os.path.join(CURRENT_PATH, 'static', 'Component_IMG')
  45. app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
  46. ALLOWED_EXTENSIONS = set(['png', 'jpeg', 'jpg'])
  47. def allowed_file(filename):
  48. print('.' in filename)
  49. print("filename: ", filename, " type(filename): ", type(filename))
  50. print(filename.split('.')[1].lower() in ALLOWED_EXTENSIONS)
  51. return '.' in filename and filename.split('.')[1].lower() in ALLOWED_EXTENSIONS
  52. @app.route('/upload', methods=['GET', 'POST'])
  53. def upload_file():
  54. if request.method == 'POST':
  55. uploaded_files = request.files.getlist("file[]")
  56. filenames = []
  57. for file in uploaded_files:
  58. print("file.filename: ", file.filename)
  59. if file and allowed_file(file.filename):
  60. filename = secure_filename(file.filename)
  61. file.save(os.path.join(app.config['UPLOAD_FOLDER'],filename))
  62. filenames.append(filename)
  63. return render_template('result.html', filenames=filenames)
  64. @app.route('/upload/<filename>', methods=['GET', 'POST'])
  65. def uploaded_file(filename):
  66. return send_from_directory(app.config['UPLOAD_FOLDER'],filename)
  67. # 下載測試
  68. @app.route('/download/<file>')
  69. def download(file):
  70. return send_from_directory('PDF',file, as_attachment=True)
  71. @app.route('/DL/<host>/<formtype>/<PR_Form_number>_<sup>')
  72. def DL(host, formtype, PR_Form_number, sup):
  73. # 230 測試
  74. config = pdfkit.configuration(wkhtmltopdf='/usr/local/bin/wkhtmltopdf')
  75. # 本機測試
  76. # config = pdfkit.configuration(wkhtmltopdf='C:/Program Files/wkhtmltopdf/bin/wkhtmltopdf.exe')
  77. options = {
  78. 'encoding': 'UTF-8',
  79. 'cookie': [
  80. ('user_id', session['user_id'])
  81. ],
  82. }
  83. print("session: ", session)
  84. CURRENT_PATH = os.path.dirname(__file__)
  85. print("CURRENT_PATH: ", CURRENT_PATH)
  86. RFQ_Form_number = '0712' + PR_Form_number[4:8]
  87. # pdfkit.from_url("https://www.google.com.tw/", "C:/Users/USER/Desktop/out.pdf", configuration=config, options=options)
  88. # pdfkit.from_url('http://192.168.50.65:5012/RFQ_form_07110001Txx_T17',
  89. # 'C:/Users/USER/Desktop/RFQ-' + PR_Form_number + '_' + sup + '.pdf', configuration=config)
  90. # 本機測試
  91. # if formtype == 'PR':
  92. # print("PDF_create PR")
  93. # pdfkit.from_url('http://192.168.50.65:5012/PR_form_' + PR_Form_number + '_' + sup,
  94. # 'C:/Users/USER/Rita/ERP_Rita_tt/PDF/' + PR_Form_number[0:8] + sup + '.pdf',
  95. # configuration=config)
  96. # file = PR_Form_number[0:8] + sup + '.pdf'
  97. # elif formtype == 'RFQ':
  98. # print("PDF_create RFQ")
  99. # pdfkit.from_url('http://192.168.50.65:5012/RFQ_form_' + PR_Form_number + '_' + sup,
  100. # 'C:/Users/USER/Rita/ERP_Rita_tt/PDF/' + RFQ_Form_number + sup + '.pdf',
  101. # configuration=config)
  102. # file = RFQ_Form_number + sup + '.pdf'
  103. # else:
  104. # print("PDF_create else RFQ")
  105. # pdfkit.from_url('http://192.168.50.65:5012/RFQ_form_' + PR_Form_number + '_' + sup,
  106. # 'C:/Users/USER/Rita/ERP_Rita_tt/PDF/' + RFQ_Form_number + sup + '.pdf',
  107. # configuration=config)
  108. # file = RFQ_Form_number + sup + '.pdf'
  109. # 230 測試
  110. # pdfkit.from_url('http://192.168.50.106:5012/RFQ_form_' + PR_Form_number + '_' + sup,
  111. # '/home/gs1801/Rita/ERP_Rita_tt/PDF/RFQ-' + PR_Form_number + '_' + sup + '.pdf',
  112. # configuration=config)
  113. IP = request.remote_addr
  114. print("IP: ", IP)
  115. if formtype == 'PR':
  116. print("PDF_create PR")
  117. pdfkit.from_url(host + '/PR_form_' + PR_Form_number + '_' + sup,
  118. CURRENT_PATH + '/PDF/' + PR_Form_number[0:8] + sup + '.pdf',
  119. configuration=config)
  120. file = PR_Form_number[0:8] + sup + '.pdf'
  121. elif formtype == 'RFQ':
  122. print("PDF_create RFQ")
  123. pdfkit.from_url(host + '/RFQ_form_' + PR_Form_number + '_' + sup,
  124. CURRENT_PATH + '/PDF/' + RFQ_Form_number + sup + '.pdf',
  125. configuration=config)
  126. file = RFQ_Form_number + sup + '.pdf'
  127. else:
  128. print("PDF_create else RFQ")
  129. pdfkit.from_url(host + '/RFQ_form_' + PR_Form_number + '_' + sup,
  130. CURRENT_PATH + '/PDF/' + RFQ_Form_number + sup + '.pdf',
  131. configuration=config)
  132. file = RFQ_Form_number + sup + '.pdf'
  133. # return jsonify({
  134. # "response":"OK"
  135. # })
  136. return send_from_directory('PDF',file, as_attachment=True)
  137. # 登入
  138. @app.route('/login', methods=['GET', 'POST'])
  139. def login():
  140. if request.method == 'GET':
  141. if 'user_id' in session:
  142. return redirect('/')
  143. else:
  144. return render_template('login.html', title = 'GET')
  145. elif request.method == 'POST':
  146. user_id = request.form['user_id']
  147. print("user_id: ", user_id)
  148. mydb = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='Gold@53743001', database='erp', charset='utf8')
  149. mycursor = mydb.cursor()
  150. try:
  151. sql = "SELECT 員工編號, 員工英文名字, 直屬主管員工編號, RD部門主管, ME部門主管, PU部門主管 " + \
  152. "FROM 員工列表 " + \
  153. "WHERE 員工編號 = '" + user_id + "'"
  154. mycursor.execute(sql)
  155. user_name = mycursor.fetchall()
  156. print("user_id: ", user_name[0][0])
  157. print("user_name: ", user_name[0][1])
  158. print("user_manager: ", user_name[0][2])
  159. print("user_is_RDmanager: ", user_name[0][3])
  160. print("user_is_MEmanager: ", user_name[0][4])
  161. print("user_is_PUmanager: ", user_name[0][5])
  162. mydb.commit()
  163. mydb.close()
  164. if user_name:
  165. session['user_id'] = user_name[0][0]
  166. session['user_name'] = user_name[0][1]
  167. session['user_manager'] = user_name[0][2]
  168. session['user_is_RDmanager'] = user_name[0][3]
  169. session['user_is_MEmanager'] = user_name[0][4]
  170. session['user_is_PUmanager'] = user_name[0][5]
  171. # if session['user_is_PUmanager']:
  172. # print('You are PUmanager')
  173. # elif session['user_is_RDmanager'] or session['user_is_MEmanager']:
  174. # print('You are RD/MEmanager')
  175. # elif 'HRPU' in session['user_id']:
  176. # print('You are PU')
  177. # elif 'HRRD' in session['user_id'] or 'HRME' in session['user_id']:
  178. # print('You are RD/ME')
  179. # return redirect('/PR_user_07110001Txx')
  180. return redirect('/')
  181. except IndexError:
  182. return render_template('login.html', title = '無此帳號')
  183. # 通知列表
  184. @app.route('/notice', methods=['GET', 'POST'])
  185. def notice():
  186. if request.method == 'GET':
  187. if 'user_id' in session:
  188. user_id = session['user_id']
  189. user_name = session['user_name']
  190. mydb = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='Gold@53743001', database='erp', charset='utf8')
  191. # mydb = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='g53743001', database='erp', charset='utf8')
  192. mycursor = mydb.cursor()
  193. # 取得現有請購單單號
  194. sql = "SELECT `通知編號`,`通知對象`,`日期`,`表單單號`,`內容`,`已完成`" + \
  195. " FROM `通知列表`" + \
  196. " WHERE `已完成`='0' AND `通知對象`='" + user_id + "'" + \
  197. " ORDER BY `通知編號`"
  198. mycursor.execute(sql)
  199. notice_list = mycursor.fetchall()
  200. # print("None: ", notice_list == ())
  201. mydb.commit()
  202. mydb.close()
  203. user_id = session['user_id']
  204. if notice_list == ():
  205. return redirect('/PR_list')
  206. else:
  207. return render_template('notice.html', title = '通知', **locals())
  208. else:
  209. return render_template('login.html', title = 'GET')
  210. # 使用者名稱測試
  211. @app.route('/user/<name>')
  212. def user(name):
  213. return render_template('hello.html', name = name)
  214. # 詢價單
  215. # @app.route('/RFQ_user_<PR_Form_number>_<sup>')
  216. # def RFQ_user(PR_Form_number, sup):
  217. # mydb = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='Gold@53743001', database='erp', charset='utf8')
  218. # # mydb = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='g53743001', database='erp', charset='utf8')
  219. # mycursor = mydb.cursor()
  220. # # 取得內部料號/零件名稱/原廠料號 新增內部料號時使用
  221. # sql = "SELECT 零件表.內部料號, 零件表.名稱, 規格表.原廠料號 " + \
  222. # "FROM 零件表 " + \
  223. # "INNER JOIN 規格表 " + \
  224. # "ON 零件表.內部料號 = 規格表.內部料號 "
  225. # mycursor.execute(sql)
  226. # Component_id_name_salesnum = mycursor.fetchall()
  227. # if sup[:1] == 'T':
  228. # sql = "SELECT " + PR_Form_number + ".內部料號, 零件表.名稱, 規格表.原廠料號, " + \
  229. # "數量, 規格表.單位, 零件表.供應商, 單價, 營業稅, 總金額, 連結, " + PR_Form_number + ".請購備註 " + \
  230. # "FROM ( " + PR_Form_number + " " + \
  231. # "INNER JOIN 零件表 " + \
  232. # "ON " + PR_Form_number + ".內部料號 = 零件表.內部料號 " + \
  233. # ") " + \
  234. # "INNER JOIN 規格表 " + \
  235. # "ON " + PR_Form_number + ".內部料號 = 規格表.內部料號 " + \
  236. # "WHERE 零件表.供應商 = '" + sup + "'"
  237. # else:
  238. # sql = "SELECT " + PR_Form_number + ".內部料號, 零件表.名稱, 規格表.原廠料號, " + \
  239. # "數量, 規格表.單位, 零件表.供應商, 單價, 營業稅, 總金額, 連結, " + PR_Form_number + ".備註 " + \
  240. # "FROM ( " + PR_Form_number + " " + \
  241. # "INNER JOIN 零件表 " + \
  242. # "ON " + PR_Form_number + ".內部料號 = 零件表.內部料號 " + \
  243. # ") " + \
  244. # "INNER JOIN 規格表 " + \
  245. # "ON " + PR_Form_number + ".內部料號 = 規格表.內部料號 "
  246. # print("sql: ", sql)
  247. # mycursor.execute(sql)
  248. # PR_data = mycursor.fetchall()
  249. # return render_template('RFQ_user_POtest.html', title = 'ERP 詢價', **locals())
  250. # 請購單列表
  251. @app.route('/PR_list')
  252. def PR_list():
  253. if 'user_id' in session:
  254. mydb = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='Gold@53743001', database='erp', charset='utf8')
  255. # mydb = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='g53743001', database='erp', charset='utf8')
  256. mycursor = mydb.cursor()
  257. user_name = session['user_name']
  258. user_id = session['user_id']
  259. user_manager = session['user_manager']
  260. # # 取得現有請購單單號
  261. # sql = "SELECT `TABLE_NAME` " + \
  262. # "FROM information_schema.TABLES " + \
  263. # "WHERE TABLE_NAME LIKE '%0711%' AND TABLE_NAME LIKE '%Txx%' AND `TABLE_SCHEMA`='erp'"
  264. # mycursor.execute(sql)
  265. # PR_tables = mycursor.fetchall()
  266. # 通知提示
  267. sql = "SELECT `通知編號`,`通知對象`,`日期`,`表單單號`,`內容`,`已完成`" + \
  268. " FROM `通知列表`" + \
  269. " WHERE `已完成`='0' AND `通知對象`='" + user_id + "'" + \
  270. " ORDER BY `通知編號`"
  271. mycursor.execute(sql)
  272. notice_list = mycursor.fetchall()
  273. print("[PR_list] notice_list: ", notice_list)
  274. # 取得現有請購單單號
  275. sql = "SELECT `請購草稿單號`,`表單狀態`,`員工列表`.`員工英文名字`" + \
  276. "FROM `表單詳細資料`" + \
  277. "INNER JOIN `員工列表`" + \
  278. "ON `表單詳細資料`.`請購人員工編號` = `員工列表`.`員工編號`"
  279. mycursor.execute(sql)
  280. PR_tables = mycursor.fetchall()
  281. # print("PR_tables[-1][0][4:8]", PR_tables[-1][0][4:8])
  282. # 取得最新的請購單單號 +1
  283. try:
  284. new_PR_companyid = '{0:04d}'.format(int(PR_tables[-1][0][4:8]) + 1)
  285. except:
  286. new_PR_companyid = '0001'
  287. mydb.commit()
  288. mydb.close()
  289. return render_template('PR_list.html', title = 'ERP 請購列表', **locals())
  290. else:
  291. return render_template('login.html', title = 'GET')
  292. # 新增 請購單
  293. # @app.route('/PR_new_<PR_Form_number>')
  294. # def PR_new(PR_Form_number):
  295. # mydb = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='Gold@53743001', database='erp', charset='utf8')
  296. # # mydb = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='g53743001', database='erp', charset='utf8')
  297. # mycursor = mydb.cursor()
  298. # # 取得內部料號/零件名稱/原廠料號 新增內部料號時使用
  299. # sql = "SELECT 零件表.內部料號, 零件表.名稱, 規格表.原廠料號 " + \
  300. # "FROM 零件表 " + \
  301. # "INNER JOIN 規格表 " + \
  302. # "ON 零件表.內部料號 = 規格表.內部料號 "
  303. # mycursor.execute(sql)
  304. # Component_id_name_salesnum = mycursor.fetchall()
  305. # # # 取得供應商編號/供應商名稱
  306. # # sql = "SELECT * FROM 供應商"
  307. # # mycursor.execute(sql)
  308. # # Company_id_name = mycursor.fetchall()
  309. # # # 取得所有請購單單號
  310. # # sql = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE `TABLE_NAME` LIKE '%0711%' ORDER BY TABLE_NAME"
  311. # # mycursor.execute(sql)
  312. # # PR_id = mycursor.fetchall()
  313. # # # 取得 `07110001T01` 現有資料
  314. # # try:
  315. # # sql = "SELECT " + PR_Form_number + ".內部料號, 零件表.名稱, 規格表.原廠料號, " + \
  316. # # "數量, 規格表.單位, 零件表.供應商, 單價, 關稅, 運費, 總金額, 付款狀況, " + PR_Form_number + ".備註 " + \
  317. # # "FROM ( " + PR_Form_number + " " + \
  318. # # "INNER JOIN 零件表 " + \
  319. # # "ON " + PR_Form_number + ".內部料號 = 零件表.內部料號 " + \
  320. # # ") " + \
  321. # # "INNER JOIN 規格表 " + \
  322. # # "ON " + PR_Form_number + ".內部料號 = 規格表.內部料號 "
  323. # # print("sql: ", sql)
  324. # # mycursor.execute(sql)
  325. # # PR_data = mycursor.fetchall()
  326. # # except pymysql.err.ProgrammingError:
  327. # # print("PR_user ERROR: pymysql.err.ProgrammingError")
  328. # # PR_data = ''
  329. # mydb.close()
  330. # return render_template('PR_new.html', title = '建立請購單', **locals())
  331. # 請購頁面
  332. @app.route('/PR_form_<PRx_Form_number>_<PRcompany>')
  333. def PR_form(PRx_Form_number, PRcompany):
  334. if True:
  335. # mydb = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='g53743001', database='erp', charset='utf8')
  336. mydb = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='Gold@53743001', database='erp', charset='utf8')
  337. mycursor = mydb.cursor()
  338. sql = "SELECT " + PRx_Form_number + ".內部料號, 零件表.名稱, 規格表.原廠料號, " + \
  339. "圖號, 數量, 連結, 供應商.公司" + \
  340. " FROM (" + \
  341. "(" + PRx_Form_number + " " + \
  342. "INNER JOIN 零件表 " + \
  343. "ON " + PRx_Form_number + ".內部料號 = 零件表.內部料號 " + \
  344. ") " + \
  345. "INNER JOIN 規格表 " + \
  346. "ON " + PRx_Form_number + ".內部料號 = 規格表.內部料號 " + \
  347. ") " + \
  348. "INNER JOIN 供應商 " + \
  349. "ON 零件表.供應商 = 供應商.供應商 " + \
  350. " WHERE `採購公司`='" + PRcompany + "'"
  351. mycursor.execute(sql)
  352. PR_datas = mycursor.fetchall()
  353. # 代入:請購、請購主管、採購、採購主管姓名
  354. sql = "SELECT `員工列表`.`員工英文名字` FROM `表單詳細資料` " + \
  355. "INNER JOIN `員工列表` ON `表單詳細資料`.`請購人員工編號` = `員工列表`.`員工編號` " + \
  356. "WHERE `請購草稿單號` = '" + PRx_Form_number + "'"
  357. mycursor.execute(sql)
  358. form_PR_user_datas = mycursor.fetchall()
  359. form_PR_user = form_PR_user_datas[0][0]
  360. sql = "SELECT `員工列表`.`員工英文名字` FROM `表單詳細資料` " + \
  361. "INNER JOIN `員工列表` ON `表單詳細資料`.`請購主管員工編號` = `員工列表`.`員工編號` " + \
  362. "WHERE `請購草稿單號` = '" + PRx_Form_number + "'"
  363. mycursor.execute(sql)
  364. form_PR_manager_datas = mycursor.fetchall()
  365. form_PR_manager = form_PR_manager_datas[0][0]
  366. sql = "SELECT `員工列表`.`員工英文名字` FROM `表單詳細資料` " + \
  367. "INNER JOIN `員工列表` ON `表單詳細資料`.`採購人員工編號` = `員工列表`.`員工編號` " + \
  368. "WHERE `請購草稿單號` = '" + PRx_Form_number + "'"
  369. mycursor.execute(sql)
  370. form_RFQ_user_datas = mycursor.fetchall()
  371. form_RFQ_user = form_RFQ_user_datas[0][0]
  372. sql = "SELECT `員工列表`.`員工英文名字` FROM `表單詳細資料` " + \
  373. "INNER JOIN `員工列表` ON `表單詳細資料`.`採購主管員工編號` = `員工列表`.`員工編號` " + \
  374. "WHERE `請購草稿單號` = '" + PRx_Form_number + "'"
  375. mycursor.execute(sql)
  376. form_RFQ_manager_datas = mycursor.fetchall()
  377. form_RFQ_manager = form_RFQ_manager_datas[0][0]
  378. # 取得請購單用途
  379. sql = "SELECT `用途` FROM `表單詳細資料` WHERE `請購草稿單號` = '" + PRx_Form_number + "'"
  380. mycursor.execute(sql)
  381. form_purpose_data = mycursor.fetchall()
  382. form_purpose = form_purpose_data[0][0]
  383. mydb.commit()
  384. mydb.close()
  385. PR_Form_number = PRx_Form_number[0:8] + PRcompany
  386. if PR_datas == ():
  387. return render_template('form_not_exist.html', title = '請購單 ' + PR_Form_number, **locals())
  388. else:
  389. return render_template('PR_form.html', title = '請購單 ' + PR_Form_number, **locals())
  390. else:
  391. return render_template('login.html', title = '請先登入')
  392. # 詢價頁面
  393. @app.route('/RFQ_form_<PR_Form_number>_<sup>')
  394. def RFQ_form(PR_Form_number, sup):
  395. if True:
  396. # mydb = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='g53743001', database='erp', charset='utf8')
  397. mydb = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='Gold@53743001', database='erp', charset='utf8')
  398. mycursor = mydb.cursor()
  399. sql = "SELECT `公司`, `聯絡人`, `地址`, `電話`, `傳真`, `email`, `統一編號`, `供應商`" + \
  400. " FROM `供應商` " + \
  401. " WHERE `供應商`='" + sup + "'"
  402. mycursor.execute(sql)
  403. sup_datas = mycursor.fetchall()
  404. sql = "SELECT " + PR_Form_number + ".內部料號, 零件表.名稱, 規格表.原廠料號, " + \
  405. "圖號, 數量, 單位" + \
  406. " FROM (" + PR_Form_number + " " + \
  407. "INNER JOIN 零件表 " + \
  408. "ON " + PR_Form_number + ".內部料號 = 零件表.內部料號 " + \
  409. ") " + \
  410. "INNER JOIN 規格表 " + \
  411. "ON " + PR_Form_number + ".內部料號 = 規格表.內部料號 " + \
  412. " WHERE `供應商`='" + sup + "'"
  413. mycursor.execute(sql)
  414. PR_datas = mycursor.fetchall()
  415. mydb.commit()
  416. mydb.close()
  417. if PR_datas == ():
  418. return render_template('form_not_exist.html', title = '無詢價單 ' + PR_Form_number + '_' + sup, **locals())
  419. else:
  420. return render_template('RFQ_form.html', title = '詢價單' + PR_Form_number + '_' + sup, **locals())
  421. else:
  422. return render_template('login.html', title = '請先登入')
  423. # 採購輸入頁面
  424. @app.route('/PO_user_<PO_Form_number>')
  425. def PO_user(PO_Form_number):
  426. if 'user_id' in session:
  427. # mydb = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='g53743001', database='erp', charset='utf8')
  428. mydb = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='Gold@53743001', database='erp', charset='utf8')
  429. mycursor = mydb.cursor()
  430. sql = "SELECT 零件表.內部料號, 零件表.名稱, 規格表.原廠料號, 規格表.圖號, 數量, 規格表.單位, 供應商.公司, 金額, 關稅, 含稅運費, 總金額, " + \
  431. "儲位, 儲位修改時間, 預計進貨日, 實際進貨日, 到貨數量, 剩餘數量, 發票號碼, 供應商出貨單, 檢測報告" + \
  432. " FROM (" + \
  433. " (採購單零件項目列表" + \
  434. " INNER JOIN 零件表" + \
  435. " ON 採購單零件項目列表.內部料號 = 零件表.內部料號" + \
  436. " )" + \
  437. " INNER JOIN 規格表" + \
  438. " ON 採購單零件項目列表.內部料號 = 規格表.內部料號" + \
  439. " )" + \
  440. " INNER JOIN 供應商" + \
  441. " ON 採購單零件項目列表.供應商 = 供應商.供應商" + \
  442. " WHERE 採購單零件項目列表.`採購單號`='" + PO_Form_number + "'"
  443. mycursor.execute(sql)
  444. PO_datas = mycursor.fetchall()
  445. # print("PO_datas: ", PO_datas)
  446. # print("PO_datas[0]: ", PO_datas[0])
  447. # print("PO_datas[0][0]: ", PO_datas[0][0])
  448. sql = "SELECT * FROM `採購單詳細資料列表` WHERE `採購單號`='" + PO_Form_number + "'"
  449. mycursor.execute(sql)
  450. PO_form_datas = mycursor.fetchall()
  451. PO_form_datas = PO_form_datas[0]
  452. # print("PO_form_datas: ", PO_form_datas)
  453. sql = "SELECT `儲位名稱` FROM `儲位列表`"
  454. mycursor.execute(sql)
  455. stock_datas = mycursor.fetchall()
  456. # print("stock_datas: ", stock_datas)
  457. mydb.commit()
  458. mydb.close()
  459. user_name = session['user_name']
  460. user_id = session['user_id']
  461. user_manager = session['user_manager']
  462. if PO_datas == ():
  463. return render_template('form_not_exist.html', title = '採購單 ' + PO_Form_number, **locals())
  464. else:
  465. return render_template('PO_user.html', title = '採購單 ' + PO_Form_number, **locals())
  466. else:
  467. return render_template('login.html', title = 'GET')
  468. # 採購頁面
  469. @app.route('/PO_form_<PO_Form_number>')
  470. def PO_form(PO_Form_number):
  471. if 'user_id' in session:
  472. # mydb = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='g53743001', database='erp', charset='utf8')
  473. mydb = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='Gold@53743001', database='erp', charset='utf8')
  474. mycursor = mydb.cursor()
  475. sql = "SELECT 零件表.名稱, 規格表.原廠料號, 規格表.圖號, 數量, 規格表.單位, 供應商.公司, 金額, 關稅, 含稅運費, 總金額" + \
  476. " FROM (" + \
  477. " (採購單零件項目列表" + \
  478. " INNER JOIN 零件表" + \
  479. " ON 採購單零件項目列表.內部料號 = 零件表.內部料號" + \
  480. " )" + \
  481. " INNER JOIN 規格表" + \
  482. " ON 採購單零件項目列表.內部料號 = 規格表.內部料號" + \
  483. " )" + \
  484. " INNER JOIN 供應商" + \
  485. " ON 採購單零件項目列表.供應商 = 供應商.供應商" + \
  486. " WHERE 採購單零件項目列表.`採購單號`='" + PO_Form_number + "'"
  487. mycursor.execute(sql)
  488. PO_datas = mycursor.fetchall()
  489. sql = "SELECT * FROM `採購單詳細資料列表` WHERE `採購單號`='" + PO_Form_number + "'"
  490. mycursor.execute(sql)
  491. PO_form_datas = mycursor.fetchall()
  492. PO_form_datas = PO_form_datas[0]
  493. mydb.commit()
  494. mydb.close()
  495. if PO_datas == ():
  496. return render_template('form_not_exist.html', title = '採購單 ' + PO_Form_number, **locals())
  497. else:
  498. return render_template('PO_form.html', title = '採購單 ' + PO_Form_number, **locals())
  499. else:
  500. return render_template('login.html', title = 'GET')
  501. # 採購頁面 未到貨 PO 區
  502. @app.route('/PO_backlog_stock')
  503. def PO_backlog_stock():
  504. if 'user_id' in session:
  505. # mydb = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='g53743001', database='erp', charset='utf8')
  506. mydb = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='Gold@53743001', database='erp', charset='utf8')
  507. mycursor = mydb.cursor()
  508. nowtime = time.strftime("%Y-%m-%d", time.localtime())
  509. print("nowtime: ", nowtime)
  510. sql = "SELECT `採購單號`, 零件表.名稱, 供應商.公司, `預計進貨日`, `實際進貨日`, `剩餘數量`, 規格表.單位, `發票號碼`" + \
  511. " FROM (" + \
  512. " (採購單零件項目列表" + \
  513. " INNER JOIN 零件表" + \
  514. " ON 採購單零件項目列表.內部料號 = 零件表.內部料號" + \
  515. " )" + \
  516. " INNER JOIN 規格表" + \
  517. " ON 採購單零件項目列表.內部料號 = 規格表.內部料號" + \
  518. " )" + \
  519. " INNER JOIN 供應商" + \
  520. " ON 採購單零件項目列表.供應商 = 供應商.供應商" + \
  521. " WHERE `剩餘數量` > 0"
  522. # " WHERE `預計進貨日` != '' AND `預計進貨日` <= '" + nowtime + "' AND `實際進貨日` = ''"
  523. mycursor.execute(sql)
  524. backlog_stocks = mycursor.fetchall()
  525. # print("backlog_stocks: ", backlog_stocks)
  526. mydb.commit()
  527. mydb.close()
  528. return render_template('PO_backlog_stock.html', title = '採購未到貨', **locals())
  529. else:
  530. return render_template('login.html', title = 'GET')
  531. # 檢驗報告介面
  532. @app.route('/IR_user_<IR_form_number>')
  533. def IR_user(IR_form_number):
  534. if 'user_id' in session:
  535. # mydb = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='g53743001', database='erp', charset='utf8')
  536. mydb = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='Gold@53743001', database='erp', charset='utf8')
  537. mycursor = mydb.cursor()
  538. # nowtime = time.strftime("%Y-%m-%d", time.localtime())
  539. # print("nowtime: ", nowtime)
  540. sql = "SELECT `檢驗單號`, `檢驗人員_管理部`, `檢驗人員_機構部`, `檢驗人員_研發部`, `檢驗人員_主管決議`, `日期`, " + \
  541. "零件表.名稱, `採購單號`, 規格表.圖號,`來貨批次`, `類型`, " + \
  542. "`管理部數量結果`, `管理部數量說明`, `管理部型號結果`, `管理部型號說明`, `管理部規格結果`, `管理部規格說明`, `管理部外觀完整性結果`, `管理部外觀完整性說明`, " + \
  543. "`管理部試組合結果`, `管理部試組合說明`, `管理部保固期限結果`, `管理部保固期限說明`, `管理部技術文件結果`, `管理部技術文件說明`, `管理部使用保養注意事項結果`, `管理部使用保養注意事項說明`, " + \
  544. "`研發部尺寸結果`, `研發部尺寸說明`, `研發部組合狀況結果`, `研發部組合狀況說明`, `研發部運作狀況結果`, `研發部運作狀況說明`, `研發部可靠度結果`, `研發部可靠度說明`, " + \
  545. "`主管決議說明`, `圖號連結`, `決議`" + \
  546. " FROM " + \
  547. " ( 檢驗報告列表" + \
  548. " INNER JOIN 零件表 " + \
  549. " ON 檢驗報告列表.內部料號 = 零件表.內部料號 " + \
  550. " ) " + \
  551. " INNER JOIN 規格表 " + \
  552. " ON 檢驗報告列表.內部料號 = 規格表.內部料號 " + \
  553. " WHERE `檢驗單號`='" + IR_form_number + "'"
  554. mycursor.execute(sql)
  555. IR_datas = mycursor.fetchall()[0]
  556. # print("IR_datas: ", IR_datas)
  557. user_name = session['user_name']
  558. mydb.commit()
  559. mydb.close()
  560. return render_template('IR_user.html', title = '檢驗報告 ' + IR_form_number, **locals())
  561. else:
  562. return render_template('login.html', title = 'GET')
  563. # PDF 測試
  564. @app.route('/PDF_create/<formtype>/<PR_Form_number>_<sup>')
  565. def PDF_create(formtype, PR_Form_number, sup):
  566. # 230 測試
  567. # config = pdfkit.configuration(wkhtmltopdf='/usr/local/bin/wkhtmltopdf')
  568. # 本機測試
  569. config = pdfkit.configuration(wkhtmltopdf='C:/Program Files/wkhtmltopdf/bin/wkhtmltopdf.exe')
  570. options = {
  571. 'encoding': 'UTF-8',
  572. 'cookie': [
  573. ('user_id', session['user_id']),
  574. ('user_name', session['user_name'])
  575. ],
  576. }
  577. RFQ_Form_number = '0712' + PR_Form_number[4:8]
  578. # pdfkit.from_url("https://www.google.com.tw/", "C:/Users/USER/Desktop/out.pdf", configuration=config, options=options)
  579. # pdfkit.from_url('http://192.168.50.65:5012/RFQ_form_07110001Txx_T17',
  580. # 'C:/Users/USER/Desktop/RFQ-' + PR_Form_number + '_' + sup + '.pdf', configuration=config)
  581. # 本機測試
  582. # if formtype == 'PR':
  583. # print("PDF_create PR")
  584. # pdfkit.from_url('http://192.168.50.65:5012/PR_form_' + PR_Form_number + '_' + sup,
  585. # 'C:/Users/USER/Rita/ERP_Rita_tt/PDF/' + PR_Form_number[0:8] + sup + '.pdf',
  586. # configuration=config)
  587. # file = PR_Form_number[0:8] + sup + '.pdf'
  588. # elif formtype == 'RFQ':
  589. # print("PDF_create RFQ")
  590. # pdfkit.from_url('http://192.168.50.65:5012/RFQ_form_' + PR_Form_number + '_' + sup,
  591. # 'C:/Users/USER/Rita/ERP_Rita_tt/PDF/' + RFQ_Form_number + sup + '.pdf',
  592. # configuration=config)
  593. # file = RFQ_Form_number + sup + '.pdf'
  594. # else:
  595. # print("PDF_create else RFQ")
  596. # pdfkit.from_url('http://192.168.50.65:5012/RFQ_form_' + PR_Form_number + '_' + sup,
  597. # 'C:/Users/USER/Rita/ERP_Rita_tt/PDF/' + RFQ_Form_number + sup + '.pdf',
  598. # configuration=config)
  599. # file = RFQ_Form_number + sup + '.pdf'
  600. # 230 測試
  601. # pdfkit.from_url('http://192.168.50.106:5012/RFQ_form_' + PR_Form_number + '_' + sup,
  602. # '/home/gs1801/Rita/ERP_Rita_tt/PDF/RFQ-' + PR_Form_number + '_' + sup + '.pdf',
  603. # configuration=config)
  604. if formtype == 'PR':
  605. print("PDF_create PR")
  606. pdfkit.from_url('192.168.50.106:5012/PR_form_' + PR_Form_number + '_' + sup,
  607. '/home/gs1801/Rita/ERP_Rita_tt/PDF/' + PR_Form_number[0:8] + sup + '.pdf',
  608. configuration=config)
  609. file = PR_Form_number[0:8] + sup + '.pdf'
  610. elif formtype == 'RFQ':
  611. print("PDF_create RFQ")
  612. pdfkit.from_url('192.168.50.106:5012/RFQ_form_' + PR_Form_number + '_' + sup,
  613. '/home/gs1801/Rita/ERP_Rita_tt/PDF/' + RFQ_Form_number + sup + '.pdf',
  614. configuration=config)
  615. file = RFQ_Form_number + sup + '.pdf'
  616. else:
  617. print("PDF_create else RFQ")
  618. pdfkit.from_url('192.168.50.106:5012/RFQ_form_' + PR_Form_number + '_' + sup,
  619. '/home/gs1801/Rita/ERP_Rita_tt/PDF/' + RFQ_Form_number + sup + '.pdf',
  620. configuration=config)
  621. file = RFQ_Form_number + sup + '.pdf'
  622. return jsonify({
  623. "response":"OK"
  624. })
  625. # 請購頁面:採購主管、請購主管、採購人員、請購人
  626. @app.route('/PR_user_<PR_Form_number>')
  627. def PR_user(PR_Form_number):
  628. if 'user_id' in session:
  629. mydb = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='Gold@53743001', database='erp', charset='utf8')
  630. # mydb = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='g53743001', database='erp', charset='utf8')
  631. mycursor = mydb.cursor()
  632. # 取得內部料號/零件名稱/原廠料號 新增內部料號時使用
  633. sql = "SELECT 零件表.內部料號, 零件表.名稱, 規格表.原廠料號 " + \
  634. "FROM 零件表 " + \
  635. "INNER JOIN 規格表 " + \
  636. "ON 零件表.內部料號 = 規格表.內部料號 "
  637. mycursor.execute(sql)
  638. Component_id_name_salesnum = mycursor.fetchall()
  639. # # 取得所有請購單單號
  640. # sql = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE `TABLE_NAME` LIKE '%0711%' ORDER BY TABLE_NAME"
  641. # mycursor.execute(sql)
  642. # PR_id = mycursor.fetchall()
  643. # # 取得 `07110001T01` 現有資料
  644. user_name = session['user_name']
  645. user_id = session['user_id']
  646. user_manager = session['user_manager']
  647. # # 取得部門主管英文名字, 送出請購單用
  648. # try:
  649. # sql = "SELECT `員工英文名字` FROM `員工列表` WHERE `員工編號`='" + user_manager + "'"
  650. # mycursor.execute(sql)
  651. # user_manager_data = mycursor.fetchall()
  652. # # print(user_manager_data[0][0])
  653. # user_manager_name = user_manager_data[0][0]
  654. # except:
  655. # user_manager_name = 'No_Manager'
  656. # 取得 PR_Form_number 表單狀態
  657. sql = "SELECT `表單狀態`,`採購人員工編號`,`請購人員工編號`,`請購主管員工編號` FROM `表單詳細資料` WHERE `請購草稿單號`='" + PR_Form_number + "'"
  658. mycursor.execute(sql)
  659. form_status_data = mycursor.fetchall()
  660. form_status = form_status_data[0][0]
  661. form_RFQ_user = form_status_data[0][1]
  662. form_PR_user = form_status_data[0][2]
  663. form_PR_manager = form_status_data[0][3]
  664. # 取得請購單用途
  665. sql = "SELECT `用途` FROM `表單詳細資料` WHERE `請購草稿單號` = '" + PR_Form_number + "'"
  666. mycursor.execute(sql)
  667. form_purpose_data = mycursor.fetchall()
  668. form_purpose = form_purpose_data[0][0]
  669. try:
  670. if session['user_is_PUmanager']:
  671. # print('You are PUmanager')
  672. # 請購單內容
  673. sql = "SELECT " + PR_Form_number + ".內部料號, 零件表.名稱, 規格表.原廠料號, " + \
  674. "數量, 規格表.單位, 規格表.圖片, 零件表.供應商, 連結, 請購人, " + \
  675. "幣別, 單價, 營業稅, 總金額, 交期, 詢價備註, 詢價單單號, 供應商報價單號, 採購主管核可, 採購公司, " + \
  676. PR_Form_number + ".請購備註 " + \
  677. "FROM ( " + PR_Form_number + " " + \
  678. "INNER JOIN 零件表 " + \
  679. "ON " + PR_Form_number + ".內部料號 = 零件表.內部料號 " + \
  680. ") " + \
  681. "INNER JOIN 規格表 " + \
  682. "ON " + PR_Form_number + ".內部料號 = 規格表.內部料號 "
  683. mycursor.execute(sql)
  684. PR_data = mycursor.fetchall()
  685. # 請購主管之下屬列表
  686. sql = "SELECT `員工編號`,`員工英文名字` FROM `員工列表` WHERE `直屬主管員工編號` = '" + user_id + "'"
  687. mycursor.execute(sql)
  688. staff_data = mycursor.fetchall()
  689. # staff_data = [g[0] for g in staff_data]
  690. print("staff_data: ", staff_data)
  691. mydb.close()
  692. return render_template('RFQ_manager.html', title = PR_Form_number + ' 詢價_主管', **locals())
  693. elif session['user_is_RDmanager'] or session['user_is_MEmanager']:
  694. # print('You are RD/MEmanager')
  695. sql = "SELECT " + PR_Form_number + ".內部料號, 零件表.名稱, 規格表.原廠料號, " + \
  696. "數量, 規格表.單位, 規格表.圖片, 零件表.供應商, 連結, 請購人, 請購核可, 請購備註, 採購主管核可 " + \
  697. "FROM ( " + PR_Form_number + " " + \
  698. "INNER JOIN 零件表 " + \
  699. "ON " + PR_Form_number + ".內部料號 = 零件表.內部料號 " + \
  700. ") " + \
  701. "INNER JOIN 規格表 " + \
  702. "ON " + PR_Form_number + ".內部料號 = 規格表.內部料號 "
  703. mycursor.execute(sql)
  704. PR_data = mycursor.fetchall()
  705. # 請購主管之下屬列表
  706. sql = "SELECT `員工英文名字` FROM `員工列表` WHERE `直屬主管員工編號` = '" + user_id + "'"
  707. mycursor.execute(sql)
  708. staff_data = mycursor.fetchall()
  709. staff_data = [g[0] for g in staff_data]
  710. print("staff_data: ", staff_data)
  711. # 請購單之請購人
  712. sql = "SELECT `請購人員工編號` FROM `表單詳細資料` WHERE `請購草稿單號`='" + PR_Form_number + "'"
  713. mycursor.execute(sql)
  714. PR_person_data = mycursor.fetchall()
  715. PR_person = PR_person_data[0][0]
  716. # 詢價主管
  717. RFQ_manager = "HRMA0001T01"
  718. mydb.close()
  719. return render_template('PR_manager.html', title = PR_Form_number + ' 請購_主管', **locals())
  720. elif 'HRPU' in session['user_id']:
  721. # print('You are PU')
  722. sql = "SELECT " + PR_Form_number + ".內部料號, 零件表.名稱, 規格表.原廠料號, " + \
  723. "數量, 規格表.單位, 規格表.圖片, 零件表.供應商, 連結, " + PR_Form_number + ".請購備註, 請購人, 請購核可, 採購公司, " + \
  724. "幣別, 單價, 營業稅, 總金額, 交期, 詢價備註, 詢價單單號, 供應商報價單號, 採購主管核可 " + \
  725. "FROM ( " + PR_Form_number + " " + \
  726. "INNER JOIN 零件表 " + \
  727. "ON " + PR_Form_number + ".內部料號 = 零件表.內部料號 " + \
  728. ") " + \
  729. "INNER JOIN 規格表 " + \
  730. "ON " + PR_Form_number + ".內部料號 = 規格表.內部料號 "
  731. mycursor.execute(sql)
  732. PR_data = mycursor.fetchall()
  733. # 獲取供應商欄位
  734. Supplier = "SELECT 供應商, 公司 FROM 供應商"
  735. mycursor.execute(Supplier)
  736. Supplier_data = mycursor.fetchall()
  737. # # 取得部門主管英文名字, 送出請購單用
  738. # sql = "SELECT `員工英文名字` FROM `員工列表` WHERE `員工編號`='" + user_manager + "'"
  739. # mycursor.execute(sql)
  740. # user_manager_data = mycursor.fetchall()
  741. # # print(user_manager_data[0][0])
  742. # user_manager_name = user_manager_data[0][0]
  743. # # 取得供應商名稱和供應商編號
  744. sql = "SELECT DISTINCT `零件表`.`供應商`, `供應商`.`公司` " + \
  745. "FROM ( `" + PR_Form_number + "` " + \
  746. "INNER JOIN `零件表` ON `" + PR_Form_number + "`.`內部料號` = `零件表`.`內部料號` ) " + \
  747. "INNER JOIN `供應商` ON `零件表`.`供應商` = `供應商`.`供應商` " + \
  748. "ORDER BY `零件表`.`供應商`"
  749. mycursor.execute(sql)
  750. RFQ_supplier_data = mycursor.fetchall()
  751. mydb.close()
  752. return render_template('RFQ_user.html', title = PR_Form_number + ' 詢價', **locals())
  753. elif 'HRRD' in session['user_id'] or 'HRME' in session['user_id']:
  754. # print('You are RD/ME')
  755. sql = "SELECT " + PR_Form_number + ".內部料號, 零件表.名稱, 規格表.原廠料號, " + \
  756. "數量, 規格表.單位, 規格表.圖片, 零件表.供應商, 連結, 請購人, 請購核可, 請購備註, 採購主管核可 " + \
  757. "FROM ( " + PR_Form_number + " " + \
  758. "INNER JOIN 零件表 " + \
  759. "ON " + PR_Form_number + ".內部料號 = 零件表.內部料號 " + \
  760. ") " + \
  761. "INNER JOIN 規格表 " + \
  762. "ON " + PR_Form_number + ".內部料號 = 規格表.內部料號 "
  763. mycursor.execute(sql)
  764. PR_data = mycursor.fetchall()
  765. # # 取得部門主管英文名字, 送出請購單用
  766. # sql = "SELECT `員工英文名字` FROM `員工列表` WHERE `員工編號`='" + user_manager + "'"
  767. # mycursor.execute(sql)
  768. # user_manager_data = mycursor.fetchall()
  769. # # print(user_manager_data[0][0])
  770. # user_manager_name = user_manager_data[0][0]
  771. mydb.close()
  772. return render_template('PR_user.html', title = PR_Form_number + ' 請購', **locals())
  773. except pymysql.err.ProgrammingError:
  774. print("PR_user ERROR: pymysql.err.ProgrammingError")
  775. PR_data = ''
  776. return redirect('/')
  777. else:
  778. return render_template('login.html', title = 'GET')
  779. # 查詢 內部料號 出現資料表 / 修改內部料號
  780. @app.route('/search_companyid', methods=['GET', 'POST'])
  781. def search_companyid():
  782. if 'user_id' in session:
  783. mydb = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='Gold@53743001', database='erp', charset='utf8')
  784. # 取得內部料號/名稱/原廠料號
  785. # mydb = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='g53743001', database='erp', charset='utf8')
  786. mycursor = mydb.cursor()
  787. sql = "SELECT 零件表.內部料號, 零件表.名稱, 規格表.原廠料號 " + \
  788. "FROM 零件表 " + \
  789. "INNER JOIN 規格表 " + \
  790. "ON 零件表.內部料號 = 規格表.內部料號 "
  791. mycursor.execute(sql)
  792. Component_id_name_salesnum = mycursor.fetchall()
  793. # 取得會計科目欄位
  794. AccountingSubjects = "SELECT * FROM 會計科目表"
  795. mycursor.execute(AccountingSubjects)
  796. AccountingSubjects_data = mycursor.fetchall()
  797. # print("AccountingSubjects_data", AccountingSubjects_data)
  798. # # (('01', '成品'), ('02', '系統成品'), ('03', '模組半成品'), ('04', '零件'), ('05', '工具'))
  799. # 取得類別欄位
  800. Category = "SELECT * FROM 類別表"
  801. mycursor.execute(Category)
  802. Category_data = mycursor.fetchall()
  803. # 取得供應商
  804. Supplier = "SELECT * FROM 供應商"
  805. mycursor.execute(Supplier)
  806. Supplier_data = mycursor.fetchall()
  807. return render_template('search_companyid.html', title='查詢內部料號', **locals())
  808. else:
  809. return render_template('login.html', title = 'GET')
  810. # 刪除 內部料號 出現資料表 零件
  811. @app.route('/delete_companyid_table_<search_company_id>', methods=['GET'])
  812. def delete_companyid_table(search_company_id):
  813. if 'user_id' in session:
  814. mydb = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='Gold@53743001', database='erp', charset='utf8')
  815. # mydb = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='g53743001', database='erp', charset='utf8')
  816. mycursor = mydb.cursor()
  817. sql = "SELECT `TABLE_NAME` " + \
  818. "FROM information_schema.columns " + \
  819. "WHERE `COLUMN_NAME`='內部料號' AND `TABLE_SCHEMA`='erp'"
  820. # print("sql: ", sql)
  821. mycursor.execute(sql)
  822. table_data = mycursor.fetchall()
  823. table_data = [g[0].replace('t', 'T') for g in table_data]
  824. print("table_data: ", table_data) # ['03040001T01', '03040002T01', '03040003T01', ... ]
  825. exist_BOM_list = []
  826. for i in range(0, len(table_data)):
  827. print("table_data[", i , "]: ", table_data[i])
  828. sql = "SELECT `內部料號` " + \
  829. "FROM `" + table_data[i] + "` " + \
  830. "WHERE `內部料號`='" + search_company_id + "'"
  831. print("[delete_companyid_table] sql: ", sql)
  832. mycursor.execute(sql)
  833. has_id_data = mycursor.fetchall()
  834. # print("has_id_data:", has_id_data)
  835. if len(has_id_data) > 0:
  836. exist_BOM_list.append(table_data[i])
  837. print("exist_BOM_list: ", exist_BOM_list)
  838. delete_sql = "DELETE FROM `" + table_data[i] + "` WHERE `內部料號` = '" + search_company_id + "'"
  839. print("delete_sql: " + delete_sql)
  840. mycursor.execute(delete_sql)
  841. delete_id_data = mycursor.fetchall()
  842. mydb.commit()
  843. mydb.close()
  844. return jsonify({"exist_BOM":exist_BOM_list})
  845. else:
  846. return render_template('login.html', title = 'GET')
  847. # 查詢 內部料號 出現資料表
  848. @app.route('/search_companyid_table_<search_company_id>', methods=['GET'])
  849. def search_companyid_table(search_company_id):
  850. mydb = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='Gold@53743001', database='erp', charset='utf8')
  851. # mydb = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='g53743001', database='erp', charset='utf8')
  852. mycursor = mydb.cursor()
  853. sql = "SELECT `TABLE_NAME` " + \
  854. "FROM information_schema.columns " + \
  855. "WHERE `COLUMN_NAME`='內部料號' AND `TABLE_SCHEMA`='erp'"
  856. # print("sql: ", sql)
  857. mycursor.execute(sql)
  858. table_data = mycursor.fetchall()
  859. table_data = [g[0].replace('t', 'T') for g in table_data]
  860. print("table_data: ", table_data) # ['03040001T01', '03040002T01', '03040003T01', ... ]
  861. sql_data = table_data
  862. has_id_table_list = []
  863. # 從 erp 資料庫內找到有符合內部料號的 table
  864. for i in range(0, len(table_data)):
  865. sql = "SELECT `內部料號` " + \
  866. "FROM " + table_data[i] + " " + \
  867. "WHERE `內部料號`='" + search_company_id + "'"
  868. mycursor.execute(sql)
  869. has_id_data = mycursor.fetchall()
  870. # print("has_id_data:", has_id_data)
  871. if len(has_id_data) > 0:
  872. has_id_table_list.append(table_data[i])
  873. print("has_id_table_list:", has_id_table_list)
  874. # 查詢是否有此 BOM 表 (資料表)
  875. has_table_list = []
  876. sql = "SELECT `TABLE_NAME` FROM information_schema.TABLES WHERE TABLE_NAME='" + search_company_id + "'"
  877. # sql = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE `TABLE_NAME` LIKE '%0711%' ORDER BY TABLE_NAME"
  878. print("has_table_list sql:", sql)
  879. mycursor.execute(sql)
  880. has_id_table_data = mycursor.fetchall()
  881. print("has_id_table_data:", has_id_table_data)
  882. if len(has_id_table_data) > 0:
  883. has_table_list.append(search_company_id)
  884. print("has_table_list:", has_table_list)
  885. mydb.commit()
  886. mydb.close()
  887. return jsonify({"sql_data":sql_data,
  888. "has_id_table_list":has_id_table_list,
  889. "has_table_list":has_table_list})
  890. # 更新 內部料號
  891. @app.route('/update_companyid_table_<old_company_id>_<new_company_id>', methods=['GET'])
  892. def update_companyid_table(old_company_id, new_company_id):
  893. mydb = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='Gold@53743001', database='erp', charset='utf8')
  894. # mydb = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='g53743001', database='erp', charset='utf8')
  895. mycursor = mydb.cursor()
  896. sql = "SELECT `TABLE_NAME` " + \
  897. "FROM information_schema.columns " + \
  898. "WHERE `COLUMN_NAME`='內部料號' AND `TABLE_SCHEMA`='erp'"
  899. # print("sql: ", sql)
  900. mycursor.execute(sql)
  901. table_data = mycursor.fetchall()
  902. table_data = [g[0].replace('t', 'T') for g in table_data]
  903. print("table_data: ", table_data) # ['03040001T01', '03040002T01', '03040003T01', ... ]
  904. has_id_table_list = []
  905. # 從 erp 資料庫內找到有符合內部料號的 table
  906. for i in range(0, len(table_data)):
  907. sql = "SELECT `內部料號` " + \
  908. "FROM " + table_data[i] + " " + \
  909. "WHERE `內部料號`='" + old_company_id + "'"
  910. mycursor.execute(sql)
  911. has_id_data = mycursor.fetchall()
  912. # print("has_id_data:", has_id_data)
  913. if len(has_id_data) > 0:
  914. has_id_table_list.append(table_data[i])
  915. print("has_id_table_list:", has_id_table_list)
  916. # # 更新資料表內之內部料號
  917. for j in range(0, len(has_id_table_list)):
  918. if has_id_table_list[j] == '零件表':
  919. sql = "UPDATE `零件表` SET `內部料號`='" + new_company_id + "',`會計科目`='" + new_company_id[0:2] + "',`類別`='" + new_company_id[2:4] + "',`流水號`='" + new_company_id[4:8] + "',`供應商`='" + new_company_id[8:11] + "'" + \
  920. " WHERE `內部料號`='" + old_company_id + "'"
  921. else :
  922. sql = "UPDATE `" + has_id_table_list[j] + "` SET `內部料號`='" + new_company_id + "'" + \
  923. " WHERE `內部料號`='" + old_company_id + "'"
  924. mycursor.execute(sql)
  925. has_id_data_update = mycursor.fetchall()
  926. print("UPDATE TABLE item: ", sql)
  927. print("UPDATE TABLE item SQL: ", has_id_data_update)
  928. # 查詢是否有此 BOM 表 (資料表)
  929. has_table_list = []
  930. sql = "SELECT `TABLE_NAME` FROM information_schema.TABLES WHERE TABLE_NAME='" + old_company_id + "' AND`TABLE_SCHEMA`='erp'"
  931. # sql = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE `TABLE_NAME` LIKE '%0711%' ORDER BY TABLE_NAME"
  932. print("has_table_list sql:", sql)
  933. mycursor.execute(sql)
  934. has_id_table_data = mycursor.fetchall()
  935. print("has_id_table_data:", has_id_table_data)
  936. if len(has_id_table_data) > 0:
  937. has_table_list.append(old_company_id)
  938. # # 更新 BOM 表名稱
  939. sql = "RENAME TABLE " + old_company_id + " TO " + new_company_id + ""
  940. mycursor.execute(sql)
  941. has_id_table_data_update = mycursor.fetchall()
  942. print("has_table_list SQL:", has_id_table_data_update)
  943. print("UPDATE TABLE name: ", sql)
  944. print("has_table_list:", has_table_list)
  945. print(new_company_id)
  946. print(has_id_table_list)
  947. print(has_table_list)
  948. mydb.commit()
  949. mydb.close()
  950. return jsonify({"new_company_id":new_company_id,
  951. "has_id_table_list":has_id_table_list,
  952. "has_table_list":has_table_list})
  953. # 建立 BOM 表
  954. @app.route('/create_BOM', methods=['GET', 'POST'])
  955. def create_BOM():
  956. if 'user_id' in session:
  957. # 紀錄使用者名稱 USERNAME
  958. USERNAME = session['user_name']
  959. # 開啟本機 erp 資料庫
  960. mydb = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='Gold@53743001', database='erp', charset='utf8')
  961. # mydb = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='g53743001', database='erp', charset='utf8')
  962. mycursor = mydb.cursor()
  963. # 獲取資料表內資料
  964. sql = '''SELECT 內部料號, 會計科目表.會計科目_名稱, 類別表.類別_名稱, 流水號, 供應商.公司, 名稱
  965. FROM (
  966. (零件表
  967. INNER JOIN 供應商
  968. ON 供應商.供應商 = 零件表.供應商
  969. )
  970. INNER JOIN 類別表
  971. ON 類別表.類別_編號 = 零件表.類別
  972. )
  973. INNER JOIN 會計科目表
  974. ON 會計科目表.會計科目_編號 = 零件表.會計科目
  975. '''
  976. mycursor.execute(sql)
  977. content = mycursor.fetchall()
  978. # 獲取欄位名稱
  979. sql = "SHOW FIELDS FROM 零件表"
  980. mycursor.execute(sql)
  981. labels = mycursor.fetchall()
  982. # print("labels: ", labels)
  983. # # labels: (('產品', 'varchar(4)', 'YES', '', None, ''), ('系統', 'varchar(5)', 'YES', '', None, ''),
  984. labels = [g[0] for g in labels]
  985. # print("labels: ", labels)
  986. # # labels: ['產品', '系統', '系統圖號', '狀態', '進貨狀態', '序號', '組序號',
  987. # 獲取會計科目欄位
  988. AccountingSubjects = "SELECT * FROM 會計科目表"
  989. mycursor.execute(AccountingSubjects)
  990. AccountingSubjects_data = mycursor.fetchall()
  991. # print("AccountingSubjects_data", AccountingSubjects_data)
  992. # # (('01', '成品'), ('02', '系統成品'), ('03', '模組半成品'), ('04', '零件'), ('05', '工具'))
  993. # 獲取類別欄位
  994. Category = "SELECT * FROM 類別表"
  995. mycursor.execute(Category)
  996. Category_data = mycursor.fetchall()
  997. # 獲取供應商欄位
  998. Supplier = "SELECT * FROM 供應商"
  999. mycursor.execute(Supplier)
  1000. Supplier_data = mycursor.fetchall()
  1001. # 獲取零件名稱
  1002. Component = "SELECT 名稱 FROM 零件表"
  1003. mycursor.execute(Component)
  1004. Component_data = mycursor.fetchall()
  1005. # # 獲取零件內部料號
  1006. # Company_ID = "SELECT 內部料號 FROM 零件表"
  1007. # mycursor.execute(Company_ID)
  1008. # Company_ID_data = mycursor.fetchall()
  1009. # 獲取零件名稱
  1010. Component_id_name = "SELECT 內部料號, 名稱 FROM 零件表"
  1011. mycursor.execute(Component_id_name)
  1012. Component_id_name = mycursor.fetchall()
  1013. # 取得內部料號/零件名稱/原廠料號 新增內部料號時使用
  1014. sql = "SELECT 零件表.內部料號, 零件表.名稱, 規格表.原廠料號 " + \
  1015. "FROM 零件表 " + \
  1016. "INNER JOIN 規格表 " + \
  1017. "ON 零件表.內部料號 = 規格表.內部料號 "
  1018. mycursor.execute(sql)
  1019. Component_id_name_salesnum = mycursor.fetchall()
  1020. mydb.close()
  1021. return render_template('create_BOM.html', title='建立 BOM 表', **locals())
  1022. else:
  1023. return render_template('login.html', title = 'GET')
  1024. # 新增零件清單項目
  1025. @app.route('/create_component_table_item', methods=['GET', 'POST'])
  1026. def create_component_table_item():
  1027. if 'user_id' in session:
  1028. # 開啟本機 erp 資料庫
  1029. mydb = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='Gold@53743001', database='erp', charset='utf8')
  1030. # mydb = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='g53743001', database='erp', charset='utf8')
  1031. mycursor = mydb.cursor()
  1032. # # --- 新增零件用 -----------------------------------------------------------------------------------------
  1033. # 獲取會計科目欄位
  1034. AccountingSubjects = "SELECT * FROM 會計科目表"
  1035. mycursor.execute(AccountingSubjects)
  1036. AccountingSubjects_data = mycursor.fetchall()
  1037. # print("AccountingSubjects_data", AccountingSubjects_data)
  1038. # # (('01', '成品'), ('02', '系統成品'), ('03', '模組半成品'), ('04', '零件'), ('05', '工具'))
  1039. # 獲取類別欄位
  1040. Category = "SELECT * FROM 類別表"
  1041. mycursor.execute(Category)
  1042. Category_data = mycursor.fetchall()
  1043. # 獲取供應商欄位
  1044. Supplier = "SELECT * FROM 供應商"
  1045. mycursor.execute(Supplier)
  1046. Supplier_data = mycursor.fetchall()
  1047. # 獲取零件名稱
  1048. Component = "SELECT 名稱 FROM 零件表"
  1049. mycursor.execute(Component)
  1050. Component_data = mycursor.fetchall()
  1051. # # --- 新增會計科目項目用 -----------------------------------------------------------------------------------------
  1052. accountingsubject_sql = 'SELECT 會計科目_編號 FROM 會計科目表 ORDER BY 會計科目_編號 DESC LIMIT 1'
  1053. # print("accountingsubject_sql: ", accountingsubject_sql)
  1054. mycursor.execute(accountingsubject_sql)
  1055. content = mycursor.fetchall()
  1056. try:
  1057. # print("content: ", content[0][0], type(content[0][0]))
  1058. new_as_sn = int(content[0][0])+1
  1059. new_as_sn = '{0:02d}'.format(new_as_sn)
  1060. except IndexError:
  1061. new_as_sn = '01'
  1062. # # --- 新增類別項目用 -----------------------------------------------------------------------------------------
  1063. category_sql = 'SELECT 類別_編號 FROM 類別表 ORDER BY 類別_編號 DESC LIMIT 1'
  1064. mycursor.execute(category_sql)
  1065. content = mycursor.fetchall()
  1066. try:
  1067. new_ca_sn = int(content[0][0])+1
  1068. new_ca_sn = '{0:02d}'.format(new_ca_sn)
  1069. except IndexError:
  1070. new_ca_sn = '01'
  1071. # --- 新增供應商項目用 -----------------------------------------------------------------------------------------
  1072. supplier_sql = 'SELECT `供應商` FROM `供應商` WHERE `供應商` LIKE "T%" ORDER BY `供應商` DESC LIMIT 1'
  1073. mycursor.execute(supplier_sql)
  1074. content = mycursor.fetchall()
  1075. try:
  1076. print("content[0][0]: ", content[0][0])
  1077. first_digit_number = content[0][0][0:1]
  1078. second_digit_number = content[0][0][1:2]
  1079. third_digit_number = content[0][0][2:]
  1080. # new_su_sn = int(content[0][0][1:])+1
  1081. # new_su_sn = content[0][0][:1] + '{0:02d}'.format(new_su_sn)
  1082. print("first_digit_number: ", first_digit_number)
  1083. print("second_digit_number: ", second_digit_number)
  1084. print("third_digit_number: ", third_digit_number)
  1085. second_digit_list = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
  1086. 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
  1087. if second_digit_number in second_digit_list and third_digit_number == '9':
  1088. l = int(second_digit_list.index(second_digit_number))
  1089. print(second_digit_list[l+1])
  1090. new_su_sn = second_digit_list[l+1] + '0'
  1091. elif second_digit_number in second_digit_list and third_digit_number != '9':
  1092. new_su_sn = second_digit_number + str(int(third_digit_number)+1)
  1093. except IndexError:
  1094. print("!!!!! IndexError")
  1095. new_su_sn = '01'
  1096. except ValueError:
  1097. print("!!!!! ValueError")
  1098. new_su_sn = 'ERROR'
  1099. mydb.commit()
  1100. mydb.close()
  1101. return render_template('create_component_table_item.html', title='新增零件', **locals())
  1102. else:
  1103. return render_template('login.html', title = 'GET')
  1104. # 找到 供應商編號 +1
  1105. @app.route('/sup_get/<first_digit_number>', methods=['GET', 'POST'])
  1106. def sup_get(first_digit_number):
  1107. # 開啟本機 erp 資料庫
  1108. mydb = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='Gold@53743001', database='erp', charset='utf8')
  1109. # mydb = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='g53743001', database='erp', charset='utf8')
  1110. mycursor = mydb.cursor()
  1111. supplier_sql = 'SELECT `供應商` FROM `供應商` WHERE `供應商` LIKE "' + first_digit_number + '%" ORDER BY `供應商` DESC LIMIT 1'
  1112. mycursor.execute(supplier_sql)
  1113. content = mycursor.fetchall()
  1114. try:
  1115. print("content[0][0]: ", content[0][0])
  1116. first_digit_number = content[0][0][0:1]
  1117. second_digit_number = content[0][0][1:2]
  1118. third_digit_number = content[0][0][2:]
  1119. print("first_digit_number: ", first_digit_number)
  1120. print("second_digit_number: ", second_digit_number)
  1121. print("third_digit_number: ", third_digit_number)
  1122. second_digit_list = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
  1123. 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
  1124. if second_digit_number in second_digit_list and third_digit_number == '9':
  1125. l = int(second_digit_list.index(second_digit_number))
  1126. new_su_sn = second_digit_list[l+1] + '0'
  1127. elif second_digit_number in second_digit_list and third_digit_number != '9':
  1128. new_su_sn = second_digit_number + str(int(third_digit_number)+1)
  1129. else:
  1130. new_su_sn = int(content[0][0][1:])+1
  1131. new_su_sn = '{0:02d}'.format(new_su_sn)
  1132. if new_su_sn == '100': new_su_sn = 'A0'
  1133. except IndexError:
  1134. print("!!!!! IndexError")
  1135. new_su_sn = '01'
  1136. # except ValueError:
  1137. # print("!!!!! ValueError")
  1138. # new_su_sn = 'ERROR'
  1139. mydb.commit()
  1140. mydb.close()
  1141. return jsonify({"sup_sn":new_su_sn})
  1142. # ERP 測試 四層下拉式選單關聯
  1143. @app.route('/drop_down_list', methods=['GET', 'POST'])
  1144. def drop_down_list():
  1145. conn = pymysql.connect(
  1146. host='52.69.200.169',
  1147. port=3306,
  1148. user='coffee',
  1149. password='skyeye',
  1150. database='Coffee',
  1151. charset='utf8'
  1152. )
  1153. cur = conn.cursor()
  1154. #獲取欄位資料
  1155. sql = "select * from product_info"
  1156. cur.execute(sql)
  1157. content = cur.fetchall()
  1158. #獲取欄位名稱
  1159. sql = "SHOW FIELDS FROM product_info"
  1160. cur.execute(sql)
  1161. labels = cur.fetchall()
  1162. # print("labels: ", labels) # labels: (('產品', 'varchar(4)', 'YES', '', None, ''), ('系統', 'varchar(5)', 'YES', '', None, ''),
  1163. labels = [g[0] for g in labels]
  1164. # print("labels: ", labels) # labels: ['產品', '系統', '系統圖號', '狀態', '進貨狀態', '序號', '組序號',
  1165. return render_template('drop_down_list.html', labels=labels, content=content)
  1166. # update_Picture
  1167. @app.route('/update_Picture', methods=['GET', 'POST'])
  1168. def update_Picture():
  1169. mydb = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='Gold@53743001', database='erp', charset='utf8')
  1170. # mydb = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='g53743001', database='erp', charset='utf8')
  1171. mycursor = mydb.cursor()
  1172. # 取得內部料號/零件名稱/原廠料號 新增內部料號時使用
  1173. sql = "SELECT 零件表.內部料號, 零件表.名稱, 規格表.原廠料號 " + \
  1174. "FROM 零件表 " + \
  1175. "INNER JOIN 規格表 " + \
  1176. "ON 零件表.內部料號 = 規格表.內部料號 "
  1177. mycursor.execute(sql)
  1178. Component_id_name_salesnum = mycursor.fetchall()
  1179. mydb.commit()
  1180. mydb.close()
  1181. return render_template('update_Picture.html', **locals())
  1182. # ERP 測試
  1183. @app.route('/search', methods=['GET', 'POST'])
  1184. def search():
  1185. if 'user_id' in session:
  1186. # 開啟本機 erp 資料庫
  1187. mydb = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='Gold@53743001', database='erp', charset='utf8')
  1188. # mydb = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='g53743001', database='erp', charset='utf8')
  1189. mycursor = mydb.cursor()
  1190. # 獲取欄位資料
  1191. # sql = '''SELECT * FROM table_component_name'''
  1192. sql = '''SELECT 內部料號, 會計科目表.會計科目_名稱, 類別表.類別_名稱, 流水號, 供應商.公司, 名稱
  1193. FROM (
  1194. (零件表
  1195. INNER JOIN 供應商
  1196. ON 供應商.供應商 = 零件表.供應商
  1197. )
  1198. INNER JOIN 類別表
  1199. ON 類別表.類別_編號 = 零件表.類別
  1200. )
  1201. INNER JOIN 會計科目表
  1202. ON 會計科目表.會計科目_編號 = 零件表.會計科目
  1203. '''
  1204. mycursor.execute(sql)
  1205. content = mycursor.fetchall()
  1206. #獲取欄位名稱
  1207. sql = "SHOW FIELDS FROM 零件表"
  1208. mycursor.execute(sql)
  1209. labels = mycursor.fetchall()
  1210. # print("labels: ", labels)
  1211. # # labels: (('產品', 'varchar(4)', 'YES', '', None, ''), ('系統', 'varchar(5)', 'YES', '', None, ''),
  1212. labels = [g[0] for g in labels]
  1213. # print("labels: ", labels)
  1214. # # labels: ['產品', '系統', '系統圖號', '狀態', '進貨狀態', '序號', '組序號',
  1215. # 獲取會計科目欄位
  1216. AccountingSubjects = "SELECT * FROM 會計科目表"
  1217. mycursor.execute(AccountingSubjects)
  1218. AccountingSubjects_data = mycursor.fetchall()
  1219. # print("AccountingSubjects_data", AccountingSubjects_data)
  1220. # # (('01', '成品'), ('02', '系統成品'), ('03', '模組半成品'), ('04', '零件'), ('05', '工具'))
  1221. # 獲取類別欄位
  1222. Category = "SELECT * FROM 類別表"
  1223. mycursor.execute(Category)
  1224. Category_data = mycursor.fetchall()
  1225. # 獲取供應商欄位
  1226. Supplier = "SELECT * FROM 供應商"
  1227. mycursor.execute(Supplier)
  1228. Supplier_data = mycursor.fetchall()
  1229. # 獲取零件名稱
  1230. Component = "SELECT 名稱 FROM 零件表"
  1231. mycursor.execute(Component)
  1232. Component_data = mycursor.fetchall()
  1233. mydb.close()
  1234. return render_template('search.html', title = 'ERP Search', **locals())
  1235. else:
  1236. return render_template('login.html', title = 'GET')
  1237. # 找到 serial number 最大值後 +1
  1238. @app.route('/sn_get/<AccountSubject>/<Category>', methods=['GET', 'POST'])
  1239. def sn_get(AccountSubject, Category):
  1240. # 開啟本機 erp 資料庫
  1241. mydb = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='Gold@53743001', database='erp', charset='utf8')
  1242. # mydb = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='g53743001', database='erp', charset='utf8')
  1243. mycursor = mydb.cursor()
  1244. # 取得流水號最大值
  1245. sql = 'SELECT 流水號 FROM 零件表 '
  1246. sql += 'WHERE 會計科目 = "' + AccountSubject + '" AND 類別 = "' + Category + '" ORDER BY 流水號 DESC LIMIT 1'
  1247. print("sql: ", sql)
  1248. mycursor.execute(sql)
  1249. content = mycursor.fetchall()
  1250. try:
  1251. print("content: ", content[0][0], type(content[0][0]))
  1252. new_sn = int(content[0][0])+1
  1253. new_sn = '{0:04d}'.format(new_sn)
  1254. except IndexError:
  1255. new_sn = '0001'
  1256. mydb.close()
  1257. return jsonify({"new_sn":new_sn})
  1258. # 找到 serial number 最大值後 +1
  1259. @app.route('/table_sn_get', methods=['GET', 'POST'])
  1260. def table_sn_get():
  1261. # 開啟本機 erp 資料庫
  1262. mydb = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='Gold@53743001', database='erp', charset='utf8')
  1263. # mydb = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='g53743001', database='erp', charset='utf8')
  1264. mycursor = mydb.cursor()
  1265. info = request.args.to_dict('sql')
  1266. sql = info['sql']
  1267. mycursor.execute(sql)
  1268. content = mycursor.fetchall()
  1269. try:
  1270. print("content: ", content[0][0], type(content[0][0]))
  1271. new_sn = int(content[0][0][4:8])+1
  1272. new_sn = '{0:04d}'.format(new_sn)
  1273. new_table_id = str(content[0][0][0:4]) + str(new_sn) + str(content[0][0][8:11]).upper()
  1274. except IndexError:
  1275. new_sn = '0001'
  1276. new_table_id = '07120001T01' # TODO 例外處理 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  1277. print("[table_sn_get] new_sn:", new_sn)
  1278. print("[table_sn_get] new_table_id:", new_table_id)
  1279. mydb.close()
  1280. return jsonify({"new_sn":new_sn,
  1281. "new_table_id":new_table_id})
  1282. # 從名稱找到內部料號
  1283. # @app.route('/sn_get/<AccountSubject>/<Category>', methods=['GET', 'POST'])
  1284. # def zn_get(AccountSubject, Category):
  1285. # # 開啟本機 erp 資料庫
  1286. # mydb = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='Gold@53743001', database='erp', charset='utf8')
  1287. # mydb = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='g53743001', database='erp', charset='utf8')
  1288. # mycursor = mydb.cursor()
  1289. # # 取得流水號最大值
  1290. # sql = 'SELECT 流水號 FROM 零件表 '
  1291. # sql += 'WHERE 會計科目 = "' + AccountSubject + '" AND 類別 = "' + Category + '" ORDER BY 流水號 DESC LIMIT 1'
  1292. # print("sql: ", sql)
  1293. # mycursor.execute(sql)
  1294. # content = mycursor.fetchall()
  1295. # try:
  1296. # print("content: ", content[0][0], type(content[0][0]))
  1297. # new_sn = int(content[0][0])+1
  1298. # new_sn = '{0:04d}'.format(new_sn)
  1299. # except IndexError:
  1300. # new_sn = '0001'
  1301. # mydb.close()
  1302. # return jsonify({"new_sn":new_sn})
  1303. # 匯出 Excel
  1304. @app.route('/export_excel/<BOM_company_id>', methods=['GET'])
  1305. def export_excel(BOM_company_id):
  1306. mydb = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='Gold@53743001', database='erp', charset='utf8')
  1307. # mydb = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='g53743001', database='erp', charset='utf8')
  1308. mycursor = mydb.cursor()
  1309. info = request.args.to_dict('sql')
  1310. sql = info['sql']
  1311. # print("sql: ", sql)
  1312. mycursor.execute(sql)
  1313. sql_data = mycursor.fetchall()
  1314. sql_data = [g for g in sql_data]
  1315. print("sql_data: ", sql_data)
  1316. sql_data_new = [] # 外面的 LIST
  1317. # BOM 表欄位名稱
  1318. sql_field = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '" + BOM_company_id + "'"
  1319. print("sql_field: ", sql_field)
  1320. mycursor.execute(sql_field)
  1321. sql_field = mycursor.fetchall()
  1322. sql_field = [g for g in sql_field]
  1323. F = []
  1324. for f in range(0, len(sql_field)):
  1325. F.append(sql_field[f][0])
  1326. # 增加輸出 BOM 表易讀性, 增加 零件表.名稱
  1327. F.insert(1, '名稱')
  1328. sql_data_new.append(F)
  1329. print("sql_data_new: ", sql_data_new)
  1330. # BOM 表內容
  1331. for i in range(0, len(sql_data)):
  1332. J = [] # 裡面的 LIST
  1333. for j in range(0, len(sql_data[i])):
  1334. J.append(sql_data[i][j])
  1335. sql_data_new.append(J)
  1336. print("sql_data_new: ", sql_data_new, type(sql_data_new))
  1337. CURRENT_PATH = os.path.dirname(__file__)
  1338. print("CURRENT_PATH: ", CURRENT_PATH)
  1339. # 匯出成 Excel
  1340. df = pd.DataFrame(sql_data_new)
  1341. # linux 下載 OK
  1342. # '_' + str(datetime.today().strftime("%m%d")) + '.xlsx'
  1343. # 230 測試
  1344. writer = pd.ExcelWriter(CURRENT_PATH + '/PDF/BOM_' + str(BOM_company_id) + '.xlsx', engine='xlsxwriter')
  1345. # 本機測試
  1346. # writer = pd.ExcelWriter('C:/Users/USER/Rita/ERP_Rita_tt/PDF/BOM_' + str(BOM_company_id) + '.xlsx', engine='xlsxwriter')
  1347. df.to_excel(writer, sheet_name=BOM_company_id, encoding='utf8', header=None, index=False)
  1348. writer.save()
  1349. mydb.commit()
  1350. mydb.close()
  1351. return jsonify({"file_location":'C:/Users/USER/Downloads/BOM_' + str(BOM_company_id) + '.xlsx',
  1352. "sql_data_new":sql_data_new})
  1353. # # 下載 server 內的 Excel BOM 表
  1354. # @app.route('/server_get_Excel', methods=['GET'])
  1355. # def server_get_Excel():
  1356. # filename = 'BOM_03030001T01_1228.xlsx'
  1357. # return send_from_directory('ExcelData', filename, as_attachment=True)
  1358. # 從資料表找出符合篩選條件之資料
  1359. @app.route('/sql_get', methods=['GET'])
  1360. def sql_get():
  1361. mydb = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='Gold@53743001', database='erp', charset='utf8')
  1362. # mydb = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='g53743001', database='erp', charset='utf8')
  1363. mycursor = mydb.cursor()
  1364. # 取得網頁傳回來的 SQL 指令
  1365. # sql = "SELECT * FROM 零件表 WHERE 會計科目 = 04"
  1366. info = request.args.to_dict('sql')
  1367. sql = info['sql']
  1368. print("[sql_get]sql: ", sql)
  1369. try:
  1370. mycursor.execute(sql)
  1371. sql_data = mycursor.fetchall()
  1372. except pymysql.err.IntegrityError:
  1373. sql_data = "請注意 ! 單據不可新增重複的零件"
  1374. # 獲取欄位名稱
  1375. # 拆解取得資料表資料
  1376. # print("sql_data: ", sql_data)
  1377. # # sql_data: (('04030001T01', '04', '03', '0001', 'T01', '電阻-18Ω'), ... )
  1378. # Rita 這裡在測試日期格式修改, 優化後調整
  1379. # if 'INNER JOIN 庫存表' in sql:
  1380. # labels = []
  1381. # for g in sql_data:
  1382. # # timeArray = time.strptime(a, "%Y-%m-%d %H:%M:%S")
  1383. # print("g[0]: ", g[0], type(g[0]), g[0].strftime("%Y-%m-%d %H:%M:%S"))
  1384. # timeString = g[0].strftime("%Y-%m-%d %H:%M:%S")
  1385. # print("timeString: ", timeString, type(timeString))
  1386. # labels.append(timeString)
  1387. # # labels.append(time.strftime("%Y-%m-%d %H:%M:%S"))
  1388. # labels.append(g[1])
  1389. # labels.append(g[2])
  1390. # else:
  1391. # labels = [g for g in sql_data]
  1392. labels = [g for g in sql_data]
  1393. # print("[sql_get]labels: ", labels)
  1394. # 取得欄位資料
  1395. if 'INNER JOIN 規格表' in sql:
  1396. sql_field = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '規格表'"
  1397. elif 'INNER JOIN 庫存表' in sql:
  1398. sql_field = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '庫存表' LIMIT 3"
  1399. elif 'INNER JOIN 銷貨表' in sql:
  1400. sql_field = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '銷貨表'"
  1401. elif 'INNER JOIN 進貨表' in sql:
  1402. sql_field = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '進貨表'"
  1403. # elif 'INNER JOIN 零件表' in sql:
  1404. # table_name = sql[sql.find('`')+1:]
  1405. # table_name = table_name[:table_name.find('`')]
  1406. # # sql_field = "SELECT 零件表.內部料號, 零件表.名稱, " + table_name + ".數量, " + table_name + ".腳位, COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS"
  1407. # sql_field = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '零件表'"
  1408. elif '`' in sql:
  1409. table_name = sql[sql.find('`')+1:]
  1410. table_name = table_name[:table_name.find('`')]
  1411. sql_field = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '" + table_name + "'"
  1412. else:
  1413. sql_field = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '零件表'"
  1414. print("[sql_get]sql_field: ", sql_field)
  1415. mycursor.execute(sql_field)
  1416. sql_field = mycursor.fetchall()
  1417. # print("[sql_get_finish]sql_field: ", sql_field)
  1418. print("[sql_get_finish]labels: ", labels)
  1419. mydb.commit()
  1420. mydb.close()
  1421. return jsonify({"sql_data":sql_data,
  1422. "labels":labels,
  1423. "sql_field":sql_field})
  1424. #自動關閉所有未使用、掛著的連接
  1425. @app.teardown_appcontext
  1426. def shutdown_session(exception=None):
  1427. # db.session.remove()
  1428. pass
  1429. if __name__ == '__main__':
  1430. # login 將 id / name 存入 Session 時使用, 不用會 RuntimeError
  1431. app.config['SECRET_KEY'] = 'ERPSessionKey'
  1432. app.run(host='0.0.0.0', port=5012, debug=True, threaded=True)