Synchronization.php 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. use Overtrue\Pinyin;
  6. /**
  7. * 中间表数据同步
  8. */
  9. class Synchronization extends Api
  10. {
  11. protected $noNeedLogin = ['*'];
  12. protected $noNeedRight = ['*'];
  13. /**
  14. * 工单资料数据同步
  15. */
  16. public function WorkOrderData()
  17. {
  18. if ($this->request->isGet() === false){
  19. $this->error('请求错误');
  20. }
  21. $db3 = Db::connect(config('database.db3'));
  22. $workOrderList = $db3->name('U8_06工单资料')
  23. ->where('MES接收时间',null)
  24. ->where('MES接收状态','0')
  25. ->where('U8插入类型','<>','关闭')
  26. ->select();
  27. if (empty($workOrderList)){
  28. $this->success('未获取新工单');
  29. }
  30. $j = $m = $n =0;
  31. foreach ($workOrderList as $key=>$value){
  32. //插入产品资料
  33. $productData = \db('产品_基本资料')
  34. ->where('产品编号',$value['成品编号'])
  35. ->count();
  36. if ($productData === 0){
  37. $clientCode = substr($value['成品编号'],0,5);
  38. $product = [
  39. '客户编号' => $clientCode,
  40. '客户名称' => $value['客户名称'],
  41. '产品编号' => $value['成品编号'],
  42. '产品名称' => $value['成品名称'],
  43. '计量单位' => '万张',
  44. '状态' => '',
  45. 'Mod_rq' => date('Y-m-d H:i:s',time()),
  46. 'UniqID' => \db('产品_基本资料')->order('UniqID desc')->value('UniqID')+1,
  47. ];
  48. if (strpos($value['成品名称'],'小盒') === false){
  49. $product['产品类别'] = '条盒';
  50. }else{
  51. $product['产品类别'] = '小盒';
  52. }
  53. $productSql = \db('产品_基本资料')->fetchSql(true)->insert($product);
  54. $productRes = \db()->query($productSql);
  55. if ($productRes === false){
  56. $n++;
  57. }
  58. }
  59. //插入工单资料
  60. if (preg_match("/[A-Za-z]/",$value['工单编号'])){
  61. $workcode = $value['工单编号'];
  62. }else{
  63. $workcode = 'Y'.$value['工单编号'];
  64. }
  65. $client = \db('产品_基本资料')->where('产品编号',$value['成品编号'])->field('rtrim(客户编号) as 客户编号,rtrim(客户名称) as 客户名称')->find();
  66. $data = [
  67. 'Gd_gdbh' => $workcode,
  68. '行号' => $value['行号'],
  69. 'Gd_客户代号' => $client['客户编号'],
  70. 'Gd_客户名称' => $client['客户名称'],
  71. 'Gd_khdh' => $value['客户编号'],
  72. 'Gd_khmc' => $value['客户名称'],
  73. '客户料号' => $value['客户料号'],
  74. '成品代号' => $value['成品编号'],
  75. '成品名称' => $value['成品名称'],
  76. 'Gd_cpdh' => $value['产品编号'],
  77. 'Gd_cpmc' => $value['产品名称'],
  78. '产品版本号' => $value['版本号'],
  79. '销售订单号' => $value['销售订单号'],
  80. '警语版面' => $value['警语版面'],
  81. '码源数量' => substr(str_replace('.','',$value['投料数量']),0,-2),
  82. 'Gd_desc' => $value['备注'],
  83. '接单日期' => $value['接单日期'],
  84. '开单日期' => $value['开单日期'],
  85. '交货日期' => $value['交货日期'],
  86. '订单数量' => $value['订单数量'],
  87. '实际投料' => $value['投料数量'],
  88. '产品单价' => $value['产品单价'],
  89. '计量单位' => '万张',
  90. '成本考核_胶印' => 1,
  91. '成本考核_凹印' => 1,
  92. '成本考核_丝印' => 1,
  93. '成本考核_模切' => 1,
  94. '成本考核_检验' => 1,
  95. 'gd_statu' => '3-计划中',
  96. 'Sys_id' => '[1012/开单员]',
  97. 'Sys_rq' => date('Y-m-d H:i:s',time()),
  98. 'Mod_rq' => date('Y-m-d H:i:s',time()),
  99. 'U8UID' => $value['U8_UID'],
  100. 'Uniqid' => $value['UniqId']
  101. ];
  102. $workOrderdata = \db('工单_基本资料')
  103. ->where('Uniqid',$value['UniqId'])
  104. ->count();
  105. if ($workOrderdata === 0){
  106. $addSql = \db('工单_基本资料')->fetchSql(true)->insert($data);
  107. $result = \db()->query($addSql);
  108. if ($result === false){
  109. $m++;
  110. }else{
  111. $sqlString = $db3->name('U8_06工单资料')
  112. ->where('UniqId', $value['UniqId'])
  113. ->fetchSql(true)
  114. ->update([
  115. 'MES接收时间' => date('Y-m-d H:i:s', time()),
  116. 'MES接收状态' => '1'
  117. ]);
  118. $db3->execute($sqlString);
  119. }
  120. }else{
  121. $updateSql = \db('工单_基本资料')->where('Uniqid',$value['UniqId'])->fetchSql(true)->update($data);
  122. $updateRes = \db()->query($updateSql);
  123. if ($updateRes === false){
  124. $j++;
  125. }else{
  126. $sqlString = $db3->name('U8_06工单资料')
  127. ->where('UniqId', $value['UniqId'])
  128. ->fetchSql(true)
  129. ->update([
  130. 'MES接收时间' => date('Y-m-d H:i:s', time()),
  131. 'MES接收状态' => '1'
  132. ]);
  133. $db3->execute($sqlString);
  134. }
  135. }
  136. }
  137. if ($j !== 0 || $m !==0 || $n !== 0){
  138. $this->error('工单资料同步失败');
  139. }else{
  140. $this->success('工单资料同步成功');
  141. }
  142. }
  143. /**
  144. * 工单bom资料同步
  145. */
  146. public function WorkOrderBomData()
  147. {
  148. if ($this->request->isGet() === false){
  149. $this->error('请求错误');
  150. }
  151. $db3 = Db::connect(config('database.db3'));
  152. $BomDataList = $db3->name('U8_09工单bom')
  153. ->where('MES接收时间',null)
  154. ->where('MES接收状态','0')
  155. ->order('U8传递时间 desc')
  156. ->select();
  157. if (empty($BomDataList)){
  158. $this->success('未找到新工单BOM');
  159. }
  160. $j = $m = 0;
  161. foreach ($BomDataList as $key=>$value){
  162. $res = \db('工单_bom资料')->where('UNIQID',$value['UNIQID'])->count();
  163. if (preg_match("/[A-Za-z]/",$value['BOM_工单编号'])){
  164. $workcode = $value['BOM_工单编号'];
  165. }else{
  166. $workcode = 'Y'.$value['BOM_工单编号'];
  167. }
  168. $data = [
  169. 'BOM_方案' => '工单评审定案',
  170. 'BOM_工单编号' => $workcode,
  171. 'BOM_版本' => $value['BOM_版本'],
  172. 'BOM_工单行号' => $value['BOM_工单行号'],
  173. 'BOM_行号' => $value['BOM_行号'],
  174. 'BOM_产品编号' => $value['BOM_产品编号'],
  175. 'BOM_物料编码' => $value['BOM_物料编码'],
  176. 'BOM_物料名称' => $value['BOM_物料名称'],
  177. 'BOM_投料单位' => $value['BOM_投料单位'],
  178. 'BOM_投入数' => $value['BOM_投入数'],
  179. 'BOM_产出数' => $value['BOM_产出数'],
  180. 'BOM_产出单位' => $value['BOM_产出单位'],
  181. 'BOM_标准用量' => $value['BOM_标准用量'],
  182. 'BOM_实际用量' => $value['BOM_实际用量'],
  183. 'BOM_计划用量' => $value['BOM_计划用量'],
  184. 'BOM_核算价格' => 0,
  185. 'Bom_领用工序' => '01-01',
  186. 'BOM_备注' => $value['BOM_备注'],
  187. 'Mod_rq' => date('Y-m-d H:i:s',time()),
  188. 'U8UID' => $value['U8_UID'],
  189. 'UNIQID' => $value['UNIQID']
  190. ];
  191. if ($res === 0){
  192. $addSql = \db('工单_bom资料')->fetchSql(true)->insert($data);
  193. $addRes = \db()->query($addSql);
  194. if ($addRes === false){
  195. $m++;
  196. }else{
  197. $sqlString = $db3->name('U8_09工单bom')
  198. ->where('UniqId', $value['UNIQID'])
  199. ->fetchSql(true)
  200. ->update([
  201. 'MES接收时间' => date('Y-m-d H:i:s', time()),
  202. 'MES接收状态' => '1'
  203. ]);
  204. $db3->execute($sqlString);
  205. }
  206. }else{
  207. $updateSql = \db('工单_bom资料')
  208. ->where('UNIQID',$value['UNIQID'])
  209. ->fetchSql(true)
  210. ->update($data);
  211. $updateRes = \db()->query($updateSql);
  212. if ($updateRes === false){
  213. $j++;
  214. }else{
  215. $sqlString = $db3->name('U8_09工单bom')
  216. ->where('UniqId', $value['UNIQID'])
  217. ->fetchSql(true)
  218. ->update([
  219. 'MES接收时间' => date('Y-m-d H:i:s', time()),
  220. 'MES接收状态' => '1'
  221. ]);
  222. $db3->execute($sqlString);
  223. }
  224. }
  225. }
  226. if ($j !== 0 || $m !== 0){
  227. $this->error('工单BOM资料同步失败');
  228. }else{
  229. $this->success('工单BOM资料同步成功');
  230. }
  231. }
  232. /**
  233. * 存货结构数据同步
  234. */
  235. public function MaterialRequisitionData()
  236. {
  237. if ($this->request->isGet() === false){
  238. $this->error('请求错误');
  239. }
  240. $db3 = \db()->connect(config('database.db3'));
  241. $MaterialDataList = $db3->name('U8_11存货结构')
  242. ->where('MES接收时间',null)
  243. ->where('MES接收状态','0')
  244. ->select();
  245. if (empty($MaterialDataList)){
  246. $this->success('未找到新的物料存货结构');
  247. }
  248. $i = 0;
  249. foreach ($MaterialDataList as $key=>$value){
  250. $data = [
  251. '编号' => $value['编号'],
  252. '名称' => $value['名称'],
  253. 'Sys_id' => '[272/超级用户]',
  254. 'Sys_rq' => date('Y-m-d H:i:s',time()),
  255. 'Mod_rq' => date('Y-m-d H:i:s',time()),
  256. 'U8UID' => $value['U8_UID'],
  257. 'UniqId' => $value['UniqId']
  258. ];
  259. if ($value['U8插入类型'] === '新增'){
  260. $sql = \db('物料_存货结构')->fetchSql(true)->insert($data);
  261. $res = \db()->query($sql);
  262. if ($res === false){
  263. $i++;
  264. }else{
  265. $sqlString = $db3->name('U8_11存货结构')
  266. ->where('UniqId', $value['UniqId'])
  267. ->fetchSql(true)
  268. ->update([
  269. 'MES接收时间' => date('Y-m-d H:i:s', time()),
  270. 'MES接收状态' => '1'
  271. ]);
  272. $db3->execute($sqlString);
  273. }
  274. }else{
  275. $sql = \db('物料_存货结构')
  276. ->fetchSql(true)
  277. ->where('UniqId',$value['UniqId'])
  278. ->update($data);
  279. $res = \db()->query($sql);
  280. if ($res === false){
  281. $i++;
  282. }else{
  283. $sqlString = $db3->name('U8_11存货结构')
  284. ->where('UniqId', $value['UniqId'])
  285. ->fetchSql(true)
  286. ->update([
  287. 'MES接收时间' => date('Y-m-d H:i:s', time()),
  288. 'MES接收状态' => '1'
  289. ]);
  290. $db3->execute($sqlString);
  291. }
  292. }
  293. }
  294. if ($i !== 0){
  295. $this->error('存货结构同步失败');
  296. }else{
  297. $this->success('存货结构同步成功');
  298. }
  299. }
  300. /**
  301. * 人事基本资料同步
  302. * @return void
  303. * @throws \think\Exception
  304. * @throws \think\db\exception\BindParamException
  305. * @throws \think\db\exception\DataNotFoundException
  306. * @throws \think\db\exception\ModelNotFoundException
  307. * @throws \think\exception\DbException
  308. * @throws \think\exception\PDOException
  309. */
  310. public function PersonnelData()
  311. {
  312. //拼音类
  313. $pinyin = new Pinyin\Pinyin();
  314. if ($this->request->isGet() === false){
  315. $this->error('请求错误');
  316. }
  317. $db3 = \db()->connect(config('database.db3'));
  318. $PersonnelDataList = $db3->name('U8_02人事资料')
  319. ->where('MES接收时间',null)
  320. ->where('MES接收状态','0')
  321. ->select();
  322. if (empty($PersonnelDataList)){
  323. $this->success('未找到新的人事资料');
  324. }
  325. $i = 0;
  326. foreach ($PersonnelDataList as $key=>$value){
  327. //获取姓名首字母
  328. $pycode = $pinyin->abbr($value['员工姓名']);
  329. $nameCode = strtoupper($pycode);
  330. $data = [
  331. '工卡编号'=>'',
  332. '卡钟设定'=>'',
  333. '打卡设置'=>'',
  334. '员工编号'=>$value['员工编号'],
  335. '员工姓名'=>$value['员工姓名'],
  336. '性别'=>$value['性别'],
  337. '聘用日期'=>$value['聘用日期'],
  338. '转正日期'=>$value['转正日期'],
  339. 'U8离职日期'=>$value['离职日期'],
  340. 'MES离职日期'=>$value['离职日期'],
  341. '扣减司龄'=>0,
  342. '部门编码'=>$value['部门编码'],
  343. '所在部门'=>$value['所在部门'],
  344. '人员类别'=>$value['人员类别'],
  345. '人员性质'=>$value['人员性质'],
  346. '班次类型'=>'',
  347. '标准工时制'=>'',
  348. '职称职务'=>$value['职称职务'],
  349. '薪资级别'=>$value['级别'],
  350. '工资表类别'=>'',
  351. '基本工资'=>'',
  352. '绩效工资1'=>'',
  353. '绩效工资2'=>'',
  354. '技能工资'=>'',
  355. '岗位津贴'=>'',
  356. '竞业补贴'=>'',
  357. '专业技术津贴'=>'',
  358. '技工技师津贴'=>'',
  359. '特殊工种津贴'=>'',
  360. '各类奖项津贴'=>'',
  361. '职危津贴'=>'',
  362. '夜班津贴'=>'',
  363. '全勤津贴'=>'',
  364. '住房津贴'=>'',
  365. '高温津贴'=>'',
  366. '用餐津贴'=>'',
  367. '司龄津贴'=>'',
  368. '联系电话'=>$value['联系电话'],
  369. '合同类型'=>'',
  370. '合同起始日期'=>'1900-01-01 00:00:00',
  371. '合同终止日期'=>'1900-01-01 00:00:00',
  372. '合同备注'=>'',
  373. '出生日期'=>$value['出生日期'],
  374. 'pycode'=>$nameCode,
  375. '籍贯'=>'',
  376. '民族'=>'',
  377. '身份证号'=>$value['身份证号'],
  378. '证件有效日期'=>'1900-01-01 00:00:00',
  379. '发证机关'=>'',
  380. '家庭住址'=>'',
  381. '学历'=>'',
  382. '婚姻状况'=>'',
  383. '社保开始日期'=>'1900-01-01 00:00:00',
  384. '开户银行'=>$value['开户银行'],
  385. '开户账号'=>$value['开户账号'],
  386. '存折办理日期'=>'1900-01-01 00:00:00',
  387. '紧急电话'=>'',
  388. '照片ID'=>'',
  389. '在职状态'=>$value['在职状态'],
  390. 'U8在职'=>$value['在职状态'],
  391. '薪酬核算分组'=>'',
  392. '考勤类型'=>'',
  393. '班组代号'=>'',
  394. 'sys_id'=>'[272/超级用户]',
  395. 'sys_rq'=>date('Y-m-d H:i:s',time()),
  396. 'mod_rq'=>'1900-01-01 00:00:00',
  397. 'U8UID'=>$value['U8_UID'],
  398. 'UniqID'=>$value['UniqId']
  399. ];
  400. $number = \db('人事_基本资料')
  401. ->where('Uniqid',$value['UniqId'])
  402. ->count();
  403. if ($number === 0){
  404. $sql = \db('人事_基本资料')->fetchSql(true)->insert($data);
  405. $res = \db()->query($sql);
  406. if ($res === false){
  407. $i++;
  408. }else{
  409. $sqlString = $db3->name('U8_02人事资料')
  410. ->where('UniqId', $value['UniqId'])
  411. ->fetchSql(true)
  412. ->update([
  413. 'MES接收时间' => date('Y-m-d H:i:s', time()),
  414. 'MES接收状态' => '1'
  415. ]);
  416. $db3->execute($sqlString);
  417. }
  418. }else{
  419. $sql = \db('人事_基本资料')
  420. ->fetchSql(true)
  421. ->where('UniqId',$value['UniqId'])
  422. ->update($data);
  423. $res = \db()->query($sql);
  424. if ($res === false){
  425. $i++;
  426. }else{
  427. $sqlString = $db3->name('U8_02人事资料')
  428. ->where('UniqId', $value['UniqId'])
  429. ->fetchSql(true)
  430. ->update([
  431. 'MES接收时间' => date('Y-m-d H:i:s', time()),
  432. 'MES接收状态' => '1'
  433. ]);
  434. $db3->execute($sqlString);
  435. }
  436. }
  437. }
  438. if ($i !== 0){
  439. $this->error('人事资料同步失败');
  440. }else{
  441. $this->success('人事资料同步成功');
  442. }
  443. }
  444. /**
  445. * 人事组织结构
  446. * @return void
  447. * @throws \think\Exception
  448. * @throws \think\db\exception\BindParamException
  449. * @throws \think\db\exception\DataNotFoundException
  450. * @throws \think\db\exception\ModelNotFoundException
  451. * @throws \think\exception\DbException
  452. * @throws \think\exception\PDOException
  453. */
  454. public function OrganizationalStructureData()
  455. {
  456. if ($this->request->isGet() === false){
  457. $this->error('请求错误');
  458. }
  459. $db3 = \db()->connect(config('database.db3'));
  460. $OrganizationalDataList = $db3->name('U8_01组织结构')
  461. ->where('MES接收时间',null)
  462. ->where('MES接收状态','0')
  463. ->select();
  464. if (empty($OrganizationalDataList)){
  465. $this->success('未找到新的组织结构');
  466. }
  467. $i = 0;
  468. foreach ($OrganizationalDataList as $key=>$value){
  469. $data = [
  470. '编号'=>$value['编号'],
  471. '名称'=>$value['名称'],
  472. '状态'=>$value['状态'],
  473. 'Sys_id'=>'[272/超级用户]',
  474. 'Sys_rq'=>date('Y-m-d H:i:s',time()),
  475. 'Mod_rq'=>'1900-01-01 00:00:00',
  476. 'U8UID'=>$value['U8_UID'],
  477. 'UNIQID'=>$value['UniqId'],
  478. ];
  479. $number = \db('人事_组织结构')
  480. ->where('Uniqid',$value['UniqId'])
  481. ->count();
  482. if ($number === 0){
  483. $sql = \db('人事_组织结构')->fetchSql(true)->insert($data);
  484. $res = \db()->query($sql);
  485. if ($res === false){
  486. $i++;
  487. }else{
  488. $sqlString = $db3->name('U8_01组织结构')
  489. ->where('UniqId', $value['UniqId'])
  490. ->fetchSql(true)
  491. ->update([
  492. 'MES接收时间' => date('Y-m-d H:i:s', time()),
  493. 'MES接收状态' => '1'
  494. ]);
  495. $db3->execute($sqlString);
  496. }
  497. }else{
  498. $sql = \db('人事_组织结构')
  499. ->fetchSql(true)
  500. ->where('UniqId',$value['UniqId'])
  501. ->update($data);
  502. $res = \db()->query($sql);
  503. if ($res === false){
  504. $i++;
  505. }else{
  506. $sqlString = $db3->name('U8_01组织结构')
  507. ->where('UniqId', $value['UniqId'])
  508. ->fetchSql(true)
  509. ->update([
  510. 'MES接收时间' => date('Y-m-d H:i:s', time()),
  511. 'MES接收状态' => '1'
  512. ]);
  513. $db3->execute($sqlString);
  514. }
  515. }
  516. }
  517. if ($i !== 0){
  518. $this->error('人事组织结构同步失败');
  519. }else{
  520. $this->success('人事组织结构同步成功');
  521. }
  522. }
  523. /**
  524. * 物料存货编码数据同步
  525. * @return void
  526. * @throws \think\Exception
  527. * @throws \think\db\exception\BindParamException
  528. * @throws \think\db\exception\DataNotFoundException
  529. * @throws \think\db\exception\ModelNotFoundException
  530. * @throws \think\exception\DbException
  531. * @throws \think\exception\PDOException
  532. */
  533. public function InventoryCodeData()
  534. {
  535. if ($this->request->isGet() === false){
  536. $this->error('请求错误');
  537. }
  538. $db3 = \db()->connect(config('database.db3'));
  539. $OrganizationalDataList = $db3->name('U8_04物料编码')
  540. ->where('MES接收时间',null)
  541. ->where('MES接收状态','0')
  542. ->select();
  543. if (empty($OrganizationalDataList)){
  544. $this->success('未找到新的物料编码');
  545. }
  546. $i = 0;
  547. foreach ($OrganizationalDataList as $key=>$value){
  548. $data = [
  549. '物料代码'=>$value['物料代码'],
  550. '物料名称'=>$value['物料名称'],
  551. '规格'=>$value['规格'],
  552. '采购单位'=>$value['采购单位'],
  553. '领用单位'=>$value['领用单位'],
  554. '单位换算率'=>$value['单位换算率'],
  555. '单价'=>$value['单价'],
  556. '币种'=>$value['币种'],
  557. '物料备注'=>$value['物料备注'],
  558. '状态'=>$value['状态'],
  559. 'Sys_id'=>'[272/超级用户]',
  560. 'Sys_rq'=>date('Y-m-d H:i:s',time()),
  561. 'Mod_rq'=>'1900-01-01 00:00:00',
  562. 'U8UID'=>$value['U8_UID'],
  563. 'UniqId'=>$value['UniqId'],
  564. ];
  565. $number = \db('物料_存货编码')
  566. ->where('Uniqid',$value['UniqId'])
  567. ->count();
  568. if ($number === 0){
  569. $sql = \db('物料_存货编码')->fetchSql(true)->insert($data);
  570. $res = \db()->query($sql);
  571. if ($res === false){
  572. $i++;
  573. }else{
  574. $sqlString = $db3->name('U8_04物料编码')
  575. ->where('UniqId', $value['UniqId'])
  576. ->fetchSql(true)
  577. ->update([
  578. 'MES接收时间' => date('Y-m-d H:i:s', time()),
  579. 'MES接收状态' => '1'
  580. ]);
  581. $db3->execute($sqlString);
  582. }
  583. }else{
  584. $sql = \db('物料_存货编码')
  585. ->fetchSql(true)
  586. ->where('UniqId',$value['UniqId'])
  587. ->update($data);
  588. $res = \db()->query($sql);
  589. if ($res === false){
  590. $i++;
  591. }else{
  592. $sqlString = $db3->name('U8_04物料编码')
  593. ->where('UniqId', $value['UniqId'])
  594. ->fetchSql(true)
  595. ->update([
  596. 'MES接收时间' => date('Y-m-d H:i:s', time()),
  597. 'MES接收状态' => '1'
  598. ]);
  599. $db3->execute($sqlString);
  600. }
  601. }
  602. }
  603. if ($i !== 0){
  604. $this->error('物料编码同步失败');
  605. }else{
  606. $this->success('物料编码同步成功');
  607. }
  608. }
  609. /**
  610. * 客户供应商数据同步
  611. * @return void
  612. * @throws \think\Exception
  613. * @throws \think\db\exception\BindParamException
  614. * @throws \think\db\exception\DataNotFoundException
  615. * @throws \think\db\exception\ModelNotFoundException
  616. * @throws \think\exception\DbException
  617. * @throws \think\exception\PDOException
  618. */
  619. public function CustomerSupplierData()
  620. {
  621. if ($this->request->isGet() === false){
  622. $this->error('请求错误');
  623. }
  624. $db3 = \db()->connect(config('database.db3'));
  625. $OrganizationalDataList = $db3->name('U8_03客户供应商')
  626. ->where('MES接收时间',null)
  627. ->where('MES接收状态','0')
  628. ->select();
  629. if (empty($OrganizationalDataList)){
  630. $this->success('未找到新的客户供应商');
  631. }
  632. $i = 0;
  633. foreach ($OrganizationalDataList as $key=>$value){
  634. $data = [
  635. '类型'=>$value['类型'],
  636. '编号'=>$value['编号'],
  637. '名称'=>$value['名称'],
  638. '简称'=>$value['简称'],
  639. '地址'=>$value['地址'],
  640. '对口部门'=>$value['对口部门'],
  641. '联系人'=>$value['联系人'],
  642. '电话'=>$value['电话'],
  643. '业务员'=>$value['业务员'],
  644. '币种'=>$value['币种'],
  645. 'Sys_id'=>'[272/超级用户]',
  646. 'Sys_rq'=>date('Y-m-d H:i:s',time()),
  647. 'Mod_rq'=>'1900-01-01 00:00:00',
  648. 'U8UID'=>$value['U8_UID'],
  649. 'UniqId'=>$value['UniqId'],
  650. ];
  651. $number = \db('erp_客户供应商')
  652. ->where('Uniqid',$value['UniqId'])
  653. ->count();
  654. if ($number === 0){
  655. $sql = \db('erp_客户供应商')->fetchSql(true)->insert($data);
  656. $res = \db()->query($sql);
  657. if ($res === false){
  658. $i++;
  659. }else{
  660. $sqlString = $db3->name('U8_03客户供应商')
  661. ->where('UniqId', $value['UniqId'])
  662. ->fetchSql(true)
  663. ->update([
  664. 'MES接收时间' => date('Y-m-d H:i:s', time()),
  665. 'MES接收状态' => '1'
  666. ]);
  667. $db3->execute($sqlString);
  668. }
  669. }else{
  670. $sql = \db('erp_客户供应商')
  671. ->fetchSql(true)
  672. ->where('UniqId',$value['UniqId'])
  673. ->update($data);
  674. $res = \db()->query($sql);
  675. if ($res === false){
  676. $i++;
  677. }else{
  678. $sqlString = $db3->name('U8_03客户供应商')
  679. ->where('UniqId', $value['UniqId'])
  680. ->fetchSql(true)
  681. ->update([
  682. 'MES接收时间' => date('Y-m-d H:i:s', time()),
  683. 'MES接收状态' => '1'
  684. ]);
  685. $db3->execute($sqlString);
  686. }
  687. }
  688. }
  689. if ($i !== 0){
  690. $this->error('客户供应商同步失败');
  691. }else{
  692. $this->success('客户供应商同步成功');
  693. }
  694. }
  695. /**
  696. * 物料领用记录数据同步
  697. * @return void
  698. * @throws \think\Exception
  699. * @throws \think\db\exception\BindParamException
  700. * @throws \think\db\exception\DataNotFoundException
  701. * @throws \think\db\exception\ModelNotFoundException
  702. * @throws \think\exception\DbException
  703. * @throws \think\exception\PDOException
  704. */
  705. public function ReceiptRecordData()
  706. {
  707. if ($this->request->isGet() === false){
  708. $this->error('请求错误');
  709. }
  710. $db3 = \db()->connect(config('database.db3'));
  711. $OrganizationalDataList = $db3->name('U8_07物料领用')
  712. ->where('MES接收时间',null)
  713. ->where('MES接收状态','0')
  714. ->select();
  715. if (empty($OrganizationalDataList)){
  716. $this->success('未找到新的物料领用记录');
  717. }
  718. $i = 0;
  719. foreach ($OrganizationalDataList as $key=>$value){
  720. if (preg_match("/[A-Za-z]/",$value['工单编号'])){
  721. $workcode = $value['工单编号'];
  722. }else{
  723. $workcode = 'Y'.$value['工单编号'];
  724. }
  725. $data = [
  726. 'st_rq'=>$value['日期'],
  727. 'st_jylb'=>$value['交易类别'],
  728. 'st_gdbh'=>$workcode,
  729. '采购单号'=>$value['采购单号'],
  730. '供方批次'=>$value['供方批次'],
  731. 'st_wlbh'=>$value['物料编号'],
  732. 'st_sl'=>$value['领用数量'],
  733. 'st_dw'=>$value['领用单位'],
  734. '领用单价'=>$value['领用单价'],
  735. 'st_desc'=>$value['备注'],
  736. '仓库编号'=>$value['仓库编号'],
  737. 'st_dpt'=>$value['采购单号'],
  738. 'st_jtbh'=>$value['机台编号'],
  739. 'sys_id'=>'[272/超级用户]',
  740. 'sys_rq'=>date('Y-m-d H:i:s',time()),
  741. 'mod_rq'=>'1900-01-01 00:00:00',
  742. 'U8UID'=>$value['U8_UID'],
  743. 'Uniqid'=>$value['UniqId'],
  744. ];
  745. $number = \db('物料_收发记录')
  746. ->where('Uniqid',$value['UniqId'])
  747. ->count();
  748. if ($number === 0){
  749. $sql = \db('物料_收发记录')->fetchSql(true)->insert($data);
  750. $res = \db()->query($sql);
  751. if ($res === false){
  752. $i++;
  753. }else{
  754. $sqlString = $db3->name('U8_07物料领用')
  755. ->where('UniqId', $value['UniqId'])
  756. ->fetchSql(true)
  757. ->update([
  758. 'MES接收时间' => date('Y-m-d H:i:s', time()),
  759. 'MES接收状态' => '1'
  760. ]);
  761. $db3->execute($sqlString);
  762. }
  763. }else{
  764. $sql = \db('物料_收发记录')
  765. ->fetchSql(true)
  766. ->where('UniqId',$value['UniqId'])
  767. ->update($data);
  768. $res = \db()->query($sql);
  769. if ($res === false){
  770. $i++;
  771. }else{
  772. $sqlString = $db3->name('U8_07物料领用')
  773. ->where('UniqId', $value['UniqId'])
  774. ->fetchSql(true)
  775. ->update([
  776. 'MES接收时间' => date('Y-m-d H:i:s', time()),
  777. 'MES接收状态' => '1'
  778. ]);
  779. $db3->execute($sqlString);
  780. }
  781. }
  782. }
  783. if ($i !== 0){
  784. $this->error('物料领用记录同步失败');
  785. }else{
  786. $this->success('物料领用记录同步成功');
  787. }
  788. }
  789. /**
  790. * 仓库信息数据同步
  791. * @return void
  792. * @throws \think\Exception
  793. * @throws \think\db\exception\BindParamException
  794. * @throws \think\db\exception\DataNotFoundException
  795. * @throws \think\db\exception\ModelNotFoundException
  796. * @throws \think\exception\DbException
  797. * @throws \think\exception\PDOException
  798. */
  799. public function WarehouseInformationData()
  800. {
  801. if ($this->request->isGet() === false){
  802. $this->error('请求错误');
  803. }
  804. $db3 = \db()->connect(config('database.db3'));
  805. $OrganizationalDataList = $db3->name('U8_12仓库信息')
  806. ->where('MES接收时间',null)
  807. ->where('MES接收状态','0')
  808. ->select();
  809. if (empty($OrganizationalDataList)){
  810. $this->success('未找到新的仓库信息');
  811. }
  812. $i = 0;
  813. foreach ($OrganizationalDataList as $key=>$value){
  814. $data = [
  815. '编号'=>$value['编号'],
  816. '名称'=>$value['名称'],
  817. 'Sys_id'=>'[272/超级用户]',
  818. 'Sys_rq'=>date('Y-m-d H:i:s',time()),
  819. 'Mod_rq'=>'1900-01-01 00:00:00',
  820. 'U8UID'=>$value['U8_UID'],
  821. 'UniqId'=>$value['UniqId'],
  822. ];
  823. $number = \db('物料_仓库信息')
  824. ->where('Uniqid',$value['UniqId'])
  825. ->count();
  826. if ($number === 0){
  827. $sql = \db('物料_仓库信息')->fetchSql(true)->insert($data);
  828. $res = \db()->query($sql);
  829. if ($res === false){
  830. $i++;
  831. }else{
  832. $sqlString = $db3->name('U8_12仓库信息')
  833. ->where('UniqId', $value['UniqId'])
  834. ->fetchSql(true)
  835. ->update([
  836. 'MES接收时间' => date('Y-m-d H:i:s', time()),
  837. 'MES接收状态' => '1'
  838. ]);
  839. $db3->execute($sqlString);
  840. }
  841. }else{
  842. $sql = \db('物料_仓库信息')
  843. ->fetchSql(true)
  844. ->where('UniqId',$value['UniqId'])
  845. ->update($data);
  846. $res = \db()->query($sql);
  847. if ($res === false){
  848. $i++;
  849. }else{
  850. $sqlString = $db3->name('U8_12仓库信息')
  851. ->where('UniqId', $value['UniqId'])
  852. ->fetchSql(true)
  853. ->update([
  854. 'MES接收时间' => date('Y-m-d H:i:s', time()),
  855. 'MES接收状态' => '1'
  856. ]);
  857. $db3->execute($sqlString);
  858. }
  859. }
  860. }
  861. if ($i !== 0){
  862. $this->error('仓库信息同步失败');
  863. }else{
  864. $this->success('仓库信息同步成功');
  865. }
  866. }
  867. /**
  868. * 职位编码数据同步
  869. * @return void
  870. * @throws \think\Exception
  871. * @throws \think\db\exception\BindParamException
  872. * @throws \think\db\exception\DataNotFoundException
  873. * @throws \think\db\exception\ModelNotFoundException
  874. * @throws \think\exception\DbException
  875. * @throws \think\exception\PDOException
  876. */
  877. public function PositionData()
  878. {
  879. if ($this->request->isGet() === false){
  880. $this->error('请求错误');
  881. }
  882. $db3 = \db()->connect(config('database.db3'));
  883. $OrganizationalDataList = $db3->name('U8_13职位编码')
  884. ->where('MES接收时间',null)
  885. ->where('MES接收状态','0')
  886. ->select();
  887. if (empty($OrganizationalDataList)){
  888. $this->success('未找到新的职位编码');
  889. }
  890. $i = 0;
  891. foreach ($OrganizationalDataList as $key=>$value){
  892. $data = [
  893. '职位编码'=>$value['职位编码'],
  894. '职位名称'=>$value['职位名称'],
  895. '定编人数'=>0,
  896. '备注说明'=>'',
  897. 'sys_id'=>'[272/超级用户]',
  898. 'sys_rq'=>date('Y-m-d H:i:s',time()),
  899. 'Mod_rq'=>'1900-01-01 00:00:00',
  900. 'U8UID'=>$value['U8_UID'],
  901. 'UniqID'=>$value['UniqID'],
  902. ];
  903. $number = \db('人事_职位编码')
  904. ->where('Uniqid',$value['UniqID'])
  905. ->count();
  906. if ($number === 0){
  907. $sql = \db('人事_职位编码')->fetchSql(true)->insert($data);
  908. $res = \db()->query($sql);
  909. if ($res === false){
  910. $i++;
  911. }else{
  912. $sqlString = $db3->name('U8_13职位编码')
  913. ->where('UniqID', $value['UniqID'])
  914. ->fetchSql(true)
  915. ->update([
  916. 'MES接收时间' => date('Y-m-d H:i:s', time()),
  917. 'MES接收状态' => '1'
  918. ]);
  919. $db3->execute($sqlString);
  920. }
  921. }else{
  922. $sql = \db('人事_职位编码')
  923. ->fetchSql(true)
  924. ->where('UniqID',$value['UniqID'])
  925. ->update($data);
  926. $res = \db()->query($sql);
  927. if ($res === false){
  928. $i++;
  929. }else{
  930. $sqlString = $db3->name('U8_13职位编码')
  931. ->where('UniqID', $value['UniqID'])
  932. ->fetchSql(true)
  933. ->update([
  934. 'MES接收时间' => date('Y-m-d H:i:s', time()),
  935. 'MES接收状态' => '1'
  936. ]);
  937. $db3->execute($sqlString);
  938. }
  939. }
  940. }
  941. if ($i !== 0){
  942. $this->error('职位编码同步失败');
  943. }else{
  944. $this->success('职位编码同步成功');
  945. }
  946. }
  947. /**
  948. * 设备产量计酬云中间表数据库同步
  949. * @return void
  950. * @throws \think\db\exception\BindParamException
  951. * @throws \think\db\exception\DataNotFoundException
  952. * @throws \think\db\exception\ModelNotFoundException
  953. * @throws \think\exception\DbException
  954. * @throws \think\exception\PDOException
  955. */
  956. public function ProductionData()
  957. {
  958. if ($this->request->isGet() === false){
  959. $this->error('请求错误');
  960. }
  961. if(is_dir(ROOT_PATH.'public/'.date('Y-m-d',time())) == null)
  962. {
  963. mkdir(ROOT_PATH.'public/'.date('Y-m-d',time()),0777,true);
  964. }
  965. // $lastID = \db('设备_产量计酬')->order('UniqId desc')->value('UniqId');
  966. // if (empty($lastID)){
  967. // $lastID = 0;
  968. // }
  969. $db5 = \db()->connect(config('database.db5'));
  970. $where = [
  971. // 'sczl_rq'=>date('Y-m-d',time()),
  972. // 'UniqId' =>['>',$lastID]
  973. 'sczl_rq' => ['>=','2024-04-01']
  974. ];
  975. $productData = $db5->name('设备_产量计酬')
  976. ->where($where)
  977. ->select();
  978. //获取机台信息
  979. $machineData = \db('设备_基本资料')
  980. ->where('mn_设备编号','<>','')
  981. ->field('rtrim(设备编号) as 设备编号,rtrim(mn_设备编号) as 美浓设备编号')
  982. ->select();
  983. //获取员工资料
  984. $employeeData = \db('人事_基本资料')
  985. ->where('mn_员工编号','<>','')
  986. ->field('rtrim(员工编号) as 员工编号,rtrim(mn_员工编号) as 美浓员工编号')
  987. ->select();
  988. //循环判断机台、员工编号
  989. $j = 0;
  990. $data = [];
  991. foreach ($productData as $key=>$value){
  992. //判断机台编号
  993. foreach ($machineData as $v){
  994. if ($value['sczl_jtbh'] === $v['美浓设备编号']){
  995. $productData[$key]['sczl_jtbh'] = $v['设备编号'];
  996. array_push($data,$productData[$key]);
  997. }
  998. }
  999. }
  1000. if (empty($data)){
  1001. $this->error('未找到新的生产数据');
  1002. }
  1003. foreach ($data as $key=>$value){
  1004. $name = [];
  1005. //判断员工编号,如果不存在写入日志文件
  1006. for($i=1;$i<=10;$i++){
  1007. foreach ($employeeData as $vv){
  1008. if ($value['sczl_bh'.$i] === $vv['美浓员工编号']){
  1009. $data[$key]['sczl_bh'.$i] = $vv['员工编号'];
  1010. }else{
  1011. array_push($name,$value['sczl_bh'.$i]);
  1012. }
  1013. }
  1014. }
  1015. $name = array_unique($name);
  1016. $filename = ROOT_PATH.'public/'.date('Y-m-d',time()).'/'.$value['sczl_jtbh'].'日志文件.txt';
  1017. $handle = fopen($filename, 'w');
  1018. foreach ($name as $v){
  1019. fwrite($handle, $v);
  1020. }
  1021. fclose($handle);
  1022. //修改工单编号
  1023. if (preg_match("/[A-Za-z]/",$value['sczl_gdbh'])){
  1024. $workcode = $value['sczl_gdbh'];
  1025. }else{
  1026. $workcode = 'Y'.$value['sczl_gdbh'];
  1027. }
  1028. $data[$key]['sczl_gdbh'] = $workcode;
  1029. }
  1030. //插入设备产量计酬数据
  1031. \db()->startTrans();
  1032. try {
  1033. \db('设备_产量计酬')->where('sczl_rq','>=','2024-04-01 00:00:00')->delete();
  1034. $sql = \db('设备_产量计酬')->fetchSql(true)->insertAll($data);
  1035. $res = \db()->query($sql);
  1036. }catch (\Exception $e){
  1037. \db()->rollback();
  1038. }
  1039. $this->success('同步成功');
  1040. }
  1041. }