FinishedProductWarehousing.php 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\api\controller\OrderSuperLoss;
  5. use think\db\exception\DataNotFoundException;
  6. use think\db\exception\ModelNotFoundException;
  7. use think\exception\DbException;
  8. use think\Db;
  9. /**
  10. * 成品入仓维护接口
  11. */
  12. class FinishedProductWarehousing extends Api
  13. {
  14. protected $noNeedLogin = ['*'];
  15. protected $noNeedRight = ['*'];
  16. /**
  17. * 首页
  18. *成品入仓测试
  19. */
  20. public function index()
  21. {
  22. $this->success('请求成功');
  23. }
  24. /**
  25. * 侧边栏
  26. * @ApiMethod (GET)
  27. */
  28. public function getTab()
  29. {
  30. //get请求
  31. if(!$this->request->isGet()){
  32. $this->error('请求方式错误');
  33. }
  34. $rows = db('成品入仓')
  35. ->field('LEFT(Sys_rq, 10) as date, COUNT(*) as counts')
  36. ->group('date')
  37. ->order('UniqId desc')
  38. ->limit(50)
  39. ->select();
  40. $num = db('成品入仓')
  41. ->where('Sys_rq','>=',$rows[count($rows)-1]['date'])
  42. ->count();
  43. $arr = db('成品入仓')
  44. ->field('LEFT(Sys_rq, 10) as date, rtrim(sys_id) as sys_id, COUNT(*) as count')
  45. ->where('Sys_rq','>=',$rows[count($rows)-1]['date'])
  46. ->group('date, sys_id')
  47. ->limit($num)
  48. ->select();
  49. foreach($rows as $key=>$value){
  50. $rows[$key]['sys'] = [];
  51. foreach($arr as $k=>$v){
  52. if($value['date'] == $v['date']){
  53. unset($v['date']);
  54. array_push($rows[$key]['sys'],$v);
  55. unset($arr[$k]);
  56. }
  57. }
  58. $rows[$key]['date'] = str_replace('-', '.', $rows[$key]['date']);
  59. }
  60. $this->success('成功',$rows);
  61. }
  62. /**
  63. * 侧边栏
  64. * @ApiMethod (GET)
  65. */
  66. public function getTabByGdbh()
  67. {
  68. //get请求
  69. if(!$this->request->isGet()){
  70. $this->error('请求方式错误');
  71. }
  72. $num = db('成品入仓')->count();
  73. $rows = db('成品入仓')
  74. ->field('jjcp_gdbh, rtrim(jjcp_cpdh) as jjcp_cpdh, LEFT(Sys_rq, 10) as Sys_rq')
  75. ->order('Sys_rq desc')
  76. ->limit($num)
  77. ->select();
  78. $num = db('工单_基本资料')->count();
  79. $arr = db('工单_基本资料')
  80. ->limit($num)
  81. ->column('CONCAT(Gd_gdbh, "-", rtrim(Gd_cpdh)) AS gdcp, CONCAT(rtrim(Gd_khdh), " 【", rtrim(Gd_khmc)) AS kh');
  82. $data = [];
  83. foreach ($rows as $value) {
  84. $key = $value['jjcp_gdbh'].'-'.$value['jjcp_cpdh'];
  85. if (isset($arr[$key]) && !isset($data[$arr[$key]])) {
  86. $data[$arr[$key]] = $value['Sys_rq'];
  87. }
  88. }
  89. // 对键进行升序排序
  90. uksort($data, function($a, $b) use ($data) {
  91. if ($data[$a] == $data[$b]) {
  92. return ($a > $b) ? 1 : -1; // 在值相同的情况下,根据键进行升序排列
  93. }
  94. return ($data[$a] < $data[$b]) ? 1 : -1;
  95. });
  96. $this->success('成功',$data);
  97. }
  98. /**
  99. * 列表
  100. * @ApiMethod (GET)
  101. * @param string $date 时间
  102. * @param string $sys_id 用户
  103. */
  104. public function getList()
  105. {
  106. // 验证请求方式
  107. if (!$this->request->isGet()) {
  108. $this->error('请求方式错误');
  109. }
  110. $req = $this->request->param();
  111. if (!isset($req['date'])) {
  112. $this->error('参数缺失');
  113. }
  114. // 设置分页参数,默认值
  115. $page = isset($req['page']) && !empty($req['page']) ? intval($req['page']) : 1;
  116. $limit = isset($req['limit']) && !empty($req['limit']) ? intval($req['limit']) : 20;
  117. // 构建查询条件
  118. if (strpos($req['date'], '-')) {
  119. $where = ['c.Sys_rq' => ['like', $req['date'] . '%']];
  120. $option = ['Sys_rq' => ['like', $req['date'] . '%']];
  121. } else {
  122. $where = [];
  123. $option = [];
  124. }
  125. if (isset($req['sys_id']) && !empty($req['sys_id'])) {
  126. $where['c.Sys_id'] = ['LIKE', $req['sys_id'] . '%'];
  127. $option['Sys_id'] = ['LIKE', $req['sys_id'] . '%'];
  128. }
  129. // 获取总数
  130. $total = db('成品入仓')->where($option)->count();
  131. // 查询数据(带分页)
  132. $rows = db('成品入仓')->alias('c')
  133. ->field('rtrim(c.入仓类型) as 入仓类型, rtrim(c.仓库编号) as 仓库编号, rtrim(c.仓库名称) as 仓库名称,
  134. rtrim(c.jjcp_num) as jjcp_num, c.jjcp_gdbh,
  135. c.jjcp_yjno, c.jjcp_sl, g.订单数量, rtrim(c.jjcp_dw) as jjcp_dw, LEFT(c.jjcp_sj, 10) as jjcp_sj,
  136. rtrim(c.jjcp_smb) as jjcp_smb, rtrim(c.jjcp_cpdh) as jjcp_cpdh, rtrim(c.jjcp_cpmc) as jjcp_cpmc,
  137. rtrim(c.成品编码) as 成品编码, rtrim(c.成品名称) as 成品名称,
  138. rtrim(c.客户料号) as 客户料号, rtrim(c.jjcp_desc) as jjcp_desc, LEFT(c.成品编码,'.config('product_code_digit').') as 客户编号, rtrim(g.Gd_客户名称) as Gd_客户名称,
  139. rtrim(c.机型备注) as 机型备注, rtrim(c.Sys_id) as Sys_id, c.Sys_rq, c.Mod_rq, c.UniqId')
  140. ->where($where)
  141. ->join('工单_基本资料 g', 'g.Gd_gdbh=c.jjcp_gdbh AND g.行号 = c.jjcp_yjno AND g.Gd_cpdh=c.jjcp_cpdh')
  142. ->order('c.UniqId desc')
  143. ->limit(($page - 1) * $limit, $limit)
  144. ->select();
  145. // 处理数据
  146. foreach ($rows as $key => $value) {
  147. $row = db('成品入仓')
  148. ->field('LEFT(Sys_rq, 10) as Sys_rq,jjcp_sl')
  149. ->where(['jjcp_gdbh' => $value['jjcp_gdbh'], 'jjcp_cpdh' => $value['jjcp_cpdh']])
  150. ->order('Sys_rq desc')
  151. ->select();
  152. $rows[$key]['jjcp_sls'] = 0;
  153. foreach ($row as $k => $v) {
  154. $rows[$key]['jjcp_sls'] += $v['jjcp_sl'];
  155. }
  156. if ($value['订单数量'] != 0) {
  157. $rows[$key]['完成率'] = number_format($rows[$key]['jjcp_sls'] / $value['订单数量'] / 100, 2);
  158. } else {
  159. $rows[$key]['完成率'] = '';
  160. }
  161. $rows[$key]['订单数量'] = floatval($value['订单数量'] * 10000);
  162. $rows[$key]['最近入仓日期'] = $row[0]['Sys_rq'];
  163. $rows[$key]['Mod_rq'] = $value['Mod_rq'] == '1900-01-01 00:00:00' ? '' : $value['Mod_rq'];
  164. }
  165. $data = [
  166. 'total' => $total,
  167. 'rows' => $rows,
  168. ];
  169. $this->success('成功', $data);
  170. }
  171. /**
  172. * 定位
  173. * @ApiMethod (GET)
  174. * @param string $gdbh 工单编号
  175. * @param string $cpmc 产品名称
  176. * @param string $page 页码
  177. * @param string $limit 数量
  178. */
  179. public function locate()
  180. {
  181. //get请求
  182. if(!$this->request->isGet()){
  183. $this->error('请求方式错误');
  184. }
  185. $req = $this->request->param();
  186. $page = 1;
  187. $limit = 15;
  188. if (isset($req['page']) && !empty($req['page'])) $page = $req['page'];
  189. if (isset($req['limit']) && !empty($req['limit'])) $limit = $req['limit'];
  190. if (isset($req['gdbh']) && !empty($req['gdbh'])){
  191. $where = ['c.jjcp_gdbh'=>[ 'like', '%' . $req['gdbh'] . '%']];
  192. $option = ['jjcp_gdbh'=>[ 'like', '%' . $req['gdbh'] . '%']];
  193. //客户编号
  194. $rows = db('成品入仓')->alias('c')
  195. ->field('rtrim(c.入仓类型) as 入仓类型, rtrim(c.仓库编号) as 仓库编号, rtrim(c.仓库名称) as 仓库名称,
  196. rtrim(c.jjcp_num) as jjcp_num, c.jjcp_gdbh,
  197. c.jjcp_yjno, c.jjcp_sl, g.订单数量, rtrim(c.jjcp_dw) as jjcp_dw, LEFT(c.jjcp_sj, 10) as jjcp_sj,
  198. rtrim(c.jjcp_smb) as jjcp_smb, rtrim(y.yj_Yjdh) as yj_Yjdh, rtrim(y.yj_yjmc) as yj_yjmc,
  199. rtrim(c.jjcp_cpdh) as jjcp_cpdh, rtrim(c.jjcp_cpmc) as jjcp_cpmc,
  200. rtrim(c.客户料号) as 客户料号, rtrim(c.jjcp_desc) as jjcp_desc, LEFT(c.成品编码,'.config('product_code_digit').') as 客户编号, rtrim(g.Gd_客户名称) as Gd_客户名称,
  201. rtrim(c.机型备注) as 机型备注, rtrim(c.Sys_id) as Sys_id, c.Sys_rq, c.Mod_rq, c.UniqId')
  202. ->where($where)
  203. ->where('y.yj_Yjno=c.jjcp_yjno')
  204. ->where('g.Gd_cpdh=c.jjcp_cpdh')
  205. ->join('工单_基本资料 g','g.Gd_gdbh=c.jjcp_gdbh')
  206. ->join('工单_印件资料 y','y.Yj_Gdbh=c.jjcp_gdbh')
  207. ->order('c.UniqId desc')
  208. ->page($page,$limit)
  209. ->select();
  210. $total = db('成品入仓')->where($option)->count();
  211. }else{
  212. if (isset($req['cpmc']) && !empty($req['cpmc'])){
  213. //查询工单表
  214. $gd = db('工单_基本资料')
  215. ->where('Gd_cpmc', 'LIKE', '%'.$req['cpmc'].'%')
  216. ->column('Gd_gdbh');
  217. $where = ['c.jjcp_gdbh'=>['in', $gd]];
  218. $option = ['jjcp_gdbh'=>['in', $gd]];
  219. //客户编号
  220. $rows = db('成品入仓')->alias('c')
  221. ->field('rtrim(c.入仓类型) as 入仓类型, rtrim(c.仓库编号) as 仓库编号, rtrim(c.仓库名称) as 仓库名称,
  222. rtrim(c.jjcp_num) as jjcp_num, c.jjcp_gdbh,
  223. c.jjcp_yjno, c.jjcp_sl, g.订单数量, rtrim(c.jjcp_dw) as jjcp_dw, LEFT(c.jjcp_sj, 10) as jjcp_sj,
  224. rtrim(c.jjcp_smb) as jjcp_smb, rtrim(y.yj_Yjdh) as yj_Yjdh, rtrim(y.yj_yjmc) as yj_yjmc,
  225. rtrim(c.jjcp_cpdh) as jjcp_cpdh, rtrim(c.jjcp_cpmc) as jjcp_cpmc,
  226. rtrim(c.客户料号) as 客户料号, rtrim(c.jjcp_desc) as jjcp_desc, LEFT(c.成品编码,'.config('product_code_digit').') as 客户编号, rtrim(g.Gd_客户名称) as Gd_客户名称,
  227. rtrim(c.机型备注) as 机型备注, rtrim(c.Sys_id) as Sys_id, c.Sys_rq, c.Mod_rq, c.UniqId')
  228. ->where($where)
  229. ->where('y.yj_Yjno=c.jjcp_yjno')
  230. ->where('g.Gd_cpdh=c.jjcp_cpdh')
  231. ->join('工单_基本资料 g','g.Gd_gdbh=c.jjcp_gdbh')
  232. ->join('工单_印件资料 y','y.Yj_Gdbh=c.jjcp_gdbh')
  233. ->order('c.UniqId desc')
  234. ->page($page,$limit)
  235. ->select();
  236. $total = db('成品入仓')->where($option)->count();
  237. }else{
  238. $this->error('参数错误');
  239. }
  240. }
  241. foreach ($rows as $key=>$value) {
  242. $row = db('成品入仓')
  243. ->field('LEFT(Sys_rq, 10) as Sys_rq,jjcp_sl')
  244. ->where(['jjcp_gdbh'=>$value['jjcp_gdbh'], 'jjcp_cpdh'=>$value['jjcp_cpdh']])
  245. ->order('Sys_rq desc')
  246. ->select();
  247. $rows[$key]['jjcp_sls'] = 0;
  248. foreach ($row as $k=>$v) {
  249. $rows[$key]['jjcp_sls'] += $v['jjcp_sl'];
  250. }
  251. if ($value['订单数量']!=0){
  252. $rows[$key]['完成率'] = number_format($rows[$key]['jjcp_sls']/$value['订单数量']/100,2);
  253. }else{
  254. $rows[$key]['完成率'] = '';
  255. }
  256. $rows[$key]['订单数量'] = floatval($value['订单数量']*10000);
  257. $rows[$key]['最近入仓日期'] = $row[0]['Sys_rq'];
  258. $rows[$key]['Mod_rq'] = $value['Mod_rq']=='1900-01-01 00:00:00' ? '' :$value['Mod_rq'];
  259. }
  260. $data = [
  261. 'total' => $total,
  262. 'rows' => $rows,
  263. ];
  264. $this->success('成功',$data);
  265. }
  266. /**
  267. * 获取信息
  268. * @ApiMethod (GET)
  269. * @param string $UniqId UniqId
  270. */
  271. public function getInfo()
  272. {
  273. //get请求
  274. if(!$this->request->isGet()){
  275. $this->error('请求方式错误');
  276. }
  277. $req = $this->request->param();
  278. if (isset($req['UniqId']) && !empty($req['UniqId'])){
  279. $UniqId = $req['UniqId'];
  280. }else{
  281. $this->error('参数错误');
  282. }
  283. //客户编号
  284. $rows = db('成品入仓')->alias('c')
  285. ->field('rtrim(c.入仓类型) as 入仓类型, rtrim(c.仓库编号) as 仓库编号, rtrim(c.仓库名称) as 仓库名称,
  286. rtrim(c.jjcp_num) as jjcp_num,
  287. c.jjcp_gdbh, c.jjcp_yjno, rtrim(c.订单编号) as 订单编号,
  288. rtrim(c.jjcp_cpdh) as jjcp_cpdh, rtrim(c.jjcp_cpmc) as jjcp_cpmc,
  289. rtrim(c.成品编码) as 成品编码, rtrim(c.成品名称) as 成品名称,
  290. rtrim(c.客户料号) as 客户料号,
  291. rtrim(c.jjcp_smb) as jjcp_smb, LEFT(c.jjcp_sj, 10) as jjcp_sj,
  292. c.jjcp_sl, rtrim(c.jjcp_dw) as jjcp_dw,
  293. g.订单数量*10000 as 订单数量, rtrim(c.机型备注) as 机型备注, rtrim(c.jjcp_desc) as jjcp_desc,
  294. rtrim(c.Sys_id) as Sys_id, c.Sys_rq, c.Mod_rq, c.UniqId')
  295. ->where('c.UniqId',$UniqId)
  296. ->where('g.Gd_cpdh=c.jjcp_cpdh')
  297. ->join('工单_基本资料 g','g.Gd_gdbh=c.jjcp_gdbh')
  298. ->find();
  299. $rows['jjcp_sls'] = db('成品入仓')
  300. ->where(['jjcp_gdbh'=>$rows['jjcp_gdbh'], 'jjcp_cpdh'=>$rows['jjcp_cpdh']])
  301. ->sum('jjcp_sl');
  302. $this->success('成功',$rows);
  303. }
  304. /**
  305. * 获取仓库信息
  306. * @ApiMethod (GET)
  307. * @param string keyword 关键词
  308. */
  309. public function getCk()
  310. {
  311. //get请求
  312. if(!$this->request->isGet()){
  313. $this->error('请求方式错误');
  314. }
  315. $req = $this->request->param();
  316. $key_word = '';
  317. if (isset($req['key_word']) && !empty($req['key_word'])){
  318. $key_word = $req['key_word'];
  319. }
  320. $rs = db('物料_仓库信息')
  321. ->field('rtrim(编号) as 编号, rtrim(名称) as 名称')
  322. ->where('编号|名称','like','%'.$key_word.'%')
  323. ->where('名称','in',['面材仓库','主要产成品'])
  324. ->select();
  325. $this->success('成功',$rs);
  326. }
  327. /**
  328. * 查询印件工序及产品名称
  329. * @ApiMethod (GET)
  330. * @param string $gdbh 工单编号
  331. */
  332. public function getGd()
  333. {
  334. //get请求
  335. if(!$this->request->isGet()){
  336. $this->error('请求方式错误');
  337. }
  338. $req = $this->request->param();
  339. if (isset($req['gdbh']) && !empty($req['gdbh'])){
  340. $gdbh = $req['gdbh'];
  341. }else{
  342. $this->error('参数错误');
  343. }
  344. $rows = db('工单_基本资料')->alias('g')
  345. ->field('g.Gd_gdbh, j.yj_yjno, rtrim(g.销售订单号) as 销售订单号, rtrim(j.yj_Yjdh) as yj_Yjdh, rtrim(j.yj_yjmc) as yj_yjmc, rtrim(g.成品代号) as 成品代号, rtrim(g.成品名称) as 成品名称, rtrim(g.客户料号) as 客户料号')
  346. ->where(['g.Gd_gdbh'=>$gdbh])
  347. ->join(['工单_印件资料'=>'j'],'g.Gd_cpdh=j.yj_yjdh and g.Gd_gdbh=j.Yj_Gdbh')
  348. ->group('j.yj_yjno')
  349. ->order('j.yj_yjno asc')
  350. ->select();
  351. foreach ($rows as $key=>$value){
  352. $rows[$key]['gdyj'] = $value['Gd_gdbh'].' -'.sprintf("%02d", $value['yj_yjno']).'-'.$value['yj_yjmc'];
  353. }
  354. $this->success('成功',$rows);
  355. }
  356. /**
  357. * 查询工单数量
  358. * @ApiMethod (GET)
  359. * @param string $gdbh 工单编号
  360. * @param string $cpdh 产品代号
  361. * @param string $rcsl 入仓数量
  362. */
  363. public function getGdsl()
  364. {
  365. //get请求
  366. if(!$this->request->isGet()){
  367. $this->error('请求方式错误');
  368. }
  369. $req = $this->request->param();
  370. if (isset($req['gdbh']) && !empty($req['gdbh'])){
  371. $gdbh = $req['gdbh'];
  372. }else{
  373. $this->error('参数错误');
  374. }
  375. if (isset($req['cpdh']) && !empty($req['cpdh'])){
  376. $cpdh = $req['cpdh'];
  377. }else{
  378. $this->error('参数错误');
  379. }
  380. $rows = db('工单_基本资料')->alias('g')
  381. ->field('订单数量*10000 as 订单数量, gd_statu')
  382. ->where(['Gd_gdbh'=>$gdbh, 'Gd_cpdh'=>$cpdh])
  383. ->find();
  384. if (!$rows) $this->error('未查询到记录');
  385. $rows['jjcp_sls'] = db('成品入仓')
  386. ->where(['jjcp_gdbh'=>$gdbh, 'jjcp_cpdh'=>$cpdh])
  387. ->sum('jjcp_sl');
  388. $this->success('成功',$rows);
  389. }
  390. /**
  391. * 新增
  392. * @ApiMethod (POST)
  393. */
  394. public function add()
  395. {
  396. if (!$this->request->isPost()) {
  397. $this->error('请求方式错误');
  398. }
  399. $req = $this->request->param();
  400. $data = $this->buildAddData($req);
  401. $this->validateProductName($data['jjcp_gdbh'], $data['jjcp_yjno'], $data['jjcp_cpmc']);
  402. $existingEndBoard = $this->findExistingEndBoard($data['jjcp_gdbh'], $data['jjcp_yjno']);
  403. if ($existingEndBoard) {
  404. $this->error(
  405. $existingEndBoard['jjcp_gdbh'] . '工单 -- 印件' . $existingEndBoard['jjcp_yjno'] . '已经进入末版,无法继续操作'
  406. );
  407. }
  408. $this->validateMaterialReceived($data['jjcp_gdbh'], $data['jjcp_yjno']);
  409. $data['UniqId'] = $this->generateFinishStockUniqId();
  410. $mongodb = Db::connect(config('database.mongodb'));
  411. $this->saveFinishStock($data, $mongodb);
  412. $warnings = [];
  413. // 1. 末板超节损(与同步独立,失败不阻断后续)
  414. if ($this->isEndBoard($data['jjcp_smb'])) {
  415. try {
  416. $this->handleEndBoardSuperLoss($data['jjcp_gdbh'], $data['jjcp_yjno']);
  417. } catch (\Exception $e) {
  418. $warnings[] = '超节损计算失败:' . $e->getMessage();
  419. }
  420. }
  421. // 2. 中间表同步(与超节损独立,失败不阻断)
  422. if ($this->shouldSyncFinishStockMiddle($data['jjcp_gdbh'])) {
  423. try {
  424. $synchronization = new \app\api\controller\Synchronization();
  425. $synchronization->syncFinishStockMiddle($data['UniqId'], '新增');
  426. } catch (\Exception $e) {
  427. $warnings[] = $e->getMessage();
  428. }
  429. }
  430. $result = ['UniqId' => $data['UniqId']];
  431. if (!empty($warnings)) {
  432. $result['warnings'] = $warnings;
  433. }
  434. $this->success(empty($warnings) ? '成功' : '成品入仓已保存,但部分后续处理失败', $result);
  435. }
  436. /**
  437. * 查询成品入仓同步操作日志
  438. * @ApiMethod (GET)
  439. * @param int|string $UniqId 成品入仓 UniqId
  440. */
  441. public function getSyncLog()
  442. {
  443. if (!$this->request->isGet()) {
  444. $this->error('请求方式错误');
  445. }
  446. $uniqId = $this->request->param('UniqId');
  447. if ($uniqId === null || $uniqId === '') {
  448. $this->error('参数错误', 'UniqId');
  449. }
  450. $list = db('工单_成品入仓同步操作日志')
  451. ->where('UniqId', (int)$uniqId)
  452. ->field('id,Gdbh,wlbm,yjmc,Yjdh,Yjno,sys_id,sys_rq,type,status,status_msg,UniqId')
  453. ->order('id desc')
  454. ->select();
  455. $this->success('成功', $list ?: []);
  456. }
  457. /**
  458. * 修改
  459. * 前端传:UniqId、数量(jjcp_sl)、smb(jjcp_smb)、sys_id(Sys_id)、时间(jjcp_sj)
  460. * @ApiMethod (POST)
  461. */
  462. public function edit()
  463. {
  464. if (!$this->request->isPost()) {
  465. $this->error('请求方式错误');
  466. }
  467. $req = $this->request->param();
  468. $uniqId = $req['UniqId'] ?? ($req['UniaId'] ?? null);
  469. if ($uniqId === null || trim((string)$uniqId) === '') {
  470. $this->error('参数错误', 'UniqId', 100);
  471. }
  472. $uniqId = (int)$uniqId;
  473. $old = db('成品入仓')->where('UniqId', $uniqId)->find();
  474. if (empty($old)) {
  475. $this->error('未找到成品入仓记录');
  476. }
  477. $hasQty = array_key_exists('jjcp_sl', $req) || array_key_exists('数量', $req);
  478. $hasSmb = array_key_exists('jjcp_smb', $req) || array_key_exists('smb', $req);
  479. $hasSj = array_key_exists('jjcp_sj', $req);
  480. $sysId = $req['Sys_id'] ?? ($req['sys_id'] ?? null);
  481. if (!$hasQty && !$hasSmb && !$hasSj && ($sysId === null || $sysId === '')) {
  482. $this->error('参数错误', '', 111);
  483. }
  484. $data = ['Mod_rq' => date('Y-m-d H:i:s')];
  485. if ($sysId !== null && $sysId !== '') {
  486. $data['Sys_id'] = $sysId;
  487. }
  488. if ($hasSj) {
  489. $data['jjcp_sj'] = $req['jjcp_sj'];
  490. }
  491. $oldQty = (float)$old['jjcp_sl'];
  492. $newQty = $oldQty;
  493. $qtyChanged = false;
  494. if ($hasQty) {
  495. $newQty = (float)($req['jjcp_sl'] ?? $req['数量']);
  496. $data['jjcp_sl'] = $newQty;
  497. $qtyChanged = abs($newQty - $oldQty) > 0.00001;
  498. }
  499. $oldSmb = (string)$old['jjcp_smb'];
  500. $newSmb = $oldSmb;
  501. $smbChanged = false;
  502. if ($hasSmb) {
  503. $newSmb = (string)($req['jjcp_smb'] ?? $req['smb']);
  504. $data['jjcp_smb'] = $newSmb;
  505. $smbChanged = trim($newSmb) !== trim($oldSmb);
  506. }
  507. $sjChanged = false;
  508. if ($hasSj) {
  509. $sjChanged = trim((string)$req['jjcp_sj']) !== trim((string)$old['jjcp_sj']);
  510. }
  511. // 改为末板时,校验同工单同印件是否已有其他末板记录
  512. if ($smbChanged && $this->isEndBoard($newSmb)) {
  513. $existingEndBoard = $this->findExistingEndBoard($old['jjcp_gdbh'], $old['jjcp_yjno']);
  514. if ($existingEndBoard && (int)$existingEndBoard['UniqId'] !== $uniqId) {
  515. $this->error(
  516. $existingEndBoard['jjcp_gdbh'] . '工单 -- 印件' . $existingEndBoard['jjcp_yjno'] . '已经进入末版,无法继续操作'
  517. );
  518. }
  519. }
  520. $mongodb = Db::connect(config('database.mongodb'));
  521. db()->startTrans();
  522. try {
  523. $sql = db('成品入仓')->where('UniqId', $uniqId)->fetchSql(true)->update($data);
  524. $bool = db()->query($sql);
  525. $mongodb->name('finished_products')
  526. ->where('UniqId', $uniqId)
  527. ->update($data);
  528. db()->commit();
  529. } catch (\Exception $e) {
  530. db()->rollback();
  531. $this->error($e->getMessage());
  532. }
  533. if ($bool === false) {
  534. $this->error('失败');
  535. }
  536. $warnings = [];
  537. // 1. 改为末板时先做超节损(与同步独立)
  538. if ($smbChanged && $this->isEndBoard($newSmb)) {
  539. try {
  540. $this->handleEndBoardSuperLoss($old['jjcp_gdbh'], $old['jjcp_yjno']);
  541. } catch (\Exception $e) {
  542. $warnings[] = '超节损计算失败:' . $e->getMessage();
  543. }
  544. }
  545. // 2. 同步中间表(与超节损独立)
  546. // - 数量变更:插入差值为新同步数据(增加为正、减少为负)
  547. // - 仅修改 smb / 时间:正常同步完整数量
  548. // - 数量与其他同时改:只同步差值
  549. if ($this->shouldSyncFinishStockMiddle($old['jjcp_gdbh']) && ($qtyChanged || $smbChanged || $sjChanged)) {
  550. try {
  551. $synchronization = new \app\api\controller\Synchronization();
  552. if ($qtyChanged) {
  553. $qtyDiff = $newQty - $oldQty; // 增加为正,减少为负
  554. $synchronization->syncFinishStockMiddle($uniqId, '修改', $qtyDiff, $sysId);
  555. } else {
  556. $synchronization->syncFinishStockMiddle($uniqId, '修改', null, $sysId);
  557. }
  558. } catch (\Exception $e) {
  559. $warnings[] = $e->getMessage();
  560. }
  561. }
  562. if (!empty($warnings)) {
  563. $this->success('成品入仓已保存,但部分后续处理失败', [
  564. 'UniqId' => $uniqId,
  565. 'warnings' => $warnings,
  566. ]);
  567. }
  568. $this->success('成功');
  569. }
  570. /**
  571. * 删除
  572. * 前端传:UniqId、sys_id(Sys_id)
  573. * @ApiMethod (POST)
  574. */
  575. public function del()
  576. {
  577. if (!$this->request->isPost()) {
  578. $this->error('请求方式错误');
  579. }
  580. $req = $this->request->param();
  581. if (!(isset($req['UniqId']) && trim($req['UniqId']) != '')) {
  582. $this->error('参数错误', 'UniqId', 100);
  583. }
  584. $sysId = $req['Sys_id'] ?? ($req['sys_id'] ?? null);
  585. if ($sysId === null || trim((string)$sysId) === '') {
  586. $this->error('参数错误', 'sys_id', 101);
  587. }
  588. $uniqId = (int)$req['UniqId'];
  589. $old = db('成品入仓')->where('UniqId', $uniqId)->find();
  590. if (empty($old)) {
  591. $this->error('未找到成品入仓记录');
  592. }
  593. // 满足同步条件才回冲:MES插入类型=新增,数量取负;不满足则正常删除
  594. if ($this->shouldSyncFinishStockMiddle($old['jjcp_gdbh'])) {
  595. try {
  596. $synchronization = new \app\api\controller\Synchronization();
  597. $synchronization->syncFinishStockMiddle(
  598. $uniqId,
  599. '新增',
  600. -1 * (float)$old['jjcp_sl'],
  601. $sysId
  602. );
  603. } catch (\Exception $e) {
  604. $this->error('同步中间表失败,未删除:' . $e->getMessage(), ['UniqId' => $uniqId]);
  605. }
  606. }
  607. $mongodb = Db::connect(config('database.mongodb'));
  608. db()->startTrans();
  609. try {
  610. $sql = db('成品入仓')->where('UniqId', $uniqId)->fetchSql(true)->delete();
  611. $bool = db()->query($sql);
  612. $mongodb->name('finished_products')
  613. ->where('UniqId', $uniqId)
  614. ->delete();
  615. db()->commit();
  616. } catch (\Exception $e) {
  617. db()->rollback();
  618. $this->error($e->getMessage());
  619. }
  620. if ($bool === false) {
  621. $this->error('失败');
  622. }
  623. $this->success('成功');
  624. }
  625. /**
  626. * excel导出
  627. * @ApiMethod (POST)
  628. * @param string date
  629. * @param string sys_id
  630. * @param array fields
  631. */
  632. public function export(){
  633. if(!$this->request->isGet()){
  634. $this->error('请求方式错误');
  635. }
  636. $req = $this->request->param();
  637. if (!isset($req['date'])) $this->error('参数缺失');
  638. if (!(isset($req['fields']) && count($req['fields'])!=0)){
  639. $this->error('参数错误','fields',100);
  640. }
  641. if(strpos($req['date'],'-')){
  642. $where = ['c.Sys_rq'=>['like',$req['date'].'%']];
  643. }else if(empty($req['date'])){
  644. $where = [];
  645. }else{
  646. $where = ['c.jjcp_gdbh'=>$req['date']];
  647. }
  648. if (isset($req['sys_id']) && !empty($req['sys_id'])){
  649. $where['c.Sys_id'] = ['LIKE',$req['sys_id'].'%'];
  650. }
  651. $arr = [
  652. '入仓类型' =>['入仓类型'],
  653. '仓库编号' =>['仓库编号'],
  654. '仓库名称' =>['仓库名称'],
  655. 'jjcp_num' =>['入仓单号'],
  656. 'jjcp_gdbh' =>['工单编号'],
  657. 'jjcp_yjno' =>['印件号'],
  658. 'jjcp_sl' =>['入仓数量'],
  659. 'jjcp_sls' =>['累计入仓数量'],
  660. '订单数量' =>['订单数量'],
  661. '完成率' =>['完成率'],
  662. 'jjcp_dw' =>['单位'],
  663. 'jjcp_sj' =>['入仓时间'],
  664. 'jjcp_smb' =>['首末板'],
  665. 'jjcp_cpdh' =>['印件代号'],
  666. 'jjcp_cpmc' =>['印件名称'],
  667. '成品编码' =>['产品编号'],
  668. '成品名称' =>['产品名称'],
  669. '客户料号' =>['客户料号'],
  670. 'jjcp_desc' =>['备注'],
  671. '客户编号' =>['客户编号'],
  672. 'Gd_客户名称' =>['客户名称'],
  673. '最近入仓日期' =>['最近入仓日期'],
  674. '机型备注' =>['机型备注'],
  675. ];
  676. $data[0] = [];
  677. foreach ($req['fields'] as $k=>$v){
  678. if(array_key_exists($v,$arr)){
  679. array_push($data[0],$arr[$v][0]);
  680. }
  681. }
  682. $num = db('成品入仓')->alias('c')
  683. ->where($where)
  684. ->where('g.Gd_cpdh=c.jjcp_cpdh')
  685. ->join('工单_基本资料 g','g.Gd_gdbh=c.jjcp_gdbh')
  686. ->count();
  687. $rows = [];
  688. for($i=1;$i<=ceil($num/10000);$i++){
  689. $p = $i == ceil($num/10000) ? $num%10000 : 10000;
  690. $r = db('成品入仓')->alias('c')
  691. ->field('rtrim(c.入仓类型) as 入仓类型, rtrim(c.仓库编号) as 仓库编号, rtrim(c.仓库名称) as 仓库名称,
  692. rtrim(c.jjcp_num) as jjcp_num, c.jjcp_gdbh,
  693. c.jjcp_yjno, c.jjcp_sl, g.订单数量, rtrim(c.jjcp_dw) as jjcp_dw, LEFT(c.jjcp_sj, 10) as jjcp_sj,
  694. rtrim(c.jjcp_smb) as jjcp_smb, rtrim(c.jjcp_cpdh) as jjcp_cpdh, rtrim(c.jjcp_cpmc) as jjcp_cpmc,
  695. rtrim(c.成品编码) as 成品编码, rtrim(c.成品名称) as 成品名称,
  696. rtrim(c.客户料号) as 客户料号, rtrim(c.jjcp_desc) as jjcp_desc, LEFT(c.成品编码,'.config('product_code_digit').') as 客户编号, rtrim(g.Gd_客户名称) as Gd_客户名称,
  697. rtrim(c.机型备注) as 机型备注')
  698. ->where($where)
  699. ->where('g.Gd_cpdh=c.jjcp_cpdh')
  700. ->join('工单_基本资料 g','g.Gd_gdbh=c.jjcp_gdbh')
  701. ->order('c.UniqId desc')
  702. ->limit($i,$p)
  703. ->select();
  704. $rows = array_merge($rows,$r);
  705. }
  706. foreach ($rows as $key=>$value) {
  707. $row = db('成品入仓')
  708. ->field('LEFT(Sys_rq, 10) as Sys_rq,jjcp_sl')
  709. ->where(['jjcp_gdbh'=>$value['jjcp_gdbh'], 'jjcp_cpdh'=>$value['jjcp_cpdh']])
  710. ->order('Sys_rq desc')
  711. ->select();
  712. $subArray = [];
  713. foreach ($arr as $k=>$v){
  714. if($k=='jjcp_sls'){
  715. $subArray[$k] = 0;
  716. foreach ($row as $n) {
  717. $subArray[$k] += $n['jjcp_sl'];
  718. }
  719. }else if($k=='完成率'){
  720. if ($value['订单数量']!=0){
  721. $subArray[$k] = 0;
  722. foreach ($row as $n) {
  723. $subArray[$k] += $n['jjcp_sl'];
  724. }
  725. $subArray[$k] = number_format($subArray[$k]/$value['订单数量']/100,2);
  726. }else{
  727. $subArray[$k] = '';
  728. }
  729. }else if($k=='最近入仓日期'){
  730. $subArray[$k] = $row[0]['Sys_rq'];
  731. }else{
  732. $subArray[$k] = $rows[$key][$k];
  733. }
  734. }
  735. array_push($data,$subArray);
  736. }
  737. $this->success('成功',['file_name'=>$req['file_name'],'data'=>$data]);
  738. }
  739. /**
  740. * 月度A
  741. * @ApiMethod (POST)
  742. * @param string start_time
  743. * @param string end_time
  744. * @param string file_name
  745. * @param array fields
  746. */
  747. public function monthExportA(){
  748. if(!$this->request->isGet()){
  749. $this->error('请求方式错误');
  750. }
  751. $req = $this->request->param();
  752. $arr = [
  753. 'start_time', 'end_time', 'file_name'
  754. ];
  755. foreach ($arr as $key => $value){
  756. if (!isset($req[$value])){
  757. $this->error('参数错误',$value,$key+1);
  758. }
  759. }
  760. if (!(isset($req['fields']) && count($req['fields'])!=0)){
  761. $this->error('参数错误','fields',100);
  762. }
  763. $arr = [
  764. '入仓类型' =>['入仓类型'],
  765. '仓库编号' =>['仓库编号'],
  766. '仓库名称' =>['仓库名称'],
  767. 'jjcp_gdbh' =>['工单编号'],
  768. 'jjcp_cpdh' =>['入仓存货编号'],
  769. 'jjcp_cpmc' =>['入仓存货名称'],
  770. 'jjcp_sl' =>['入仓数量'],
  771. 'jjcp_dw' =>['单位'],
  772. 'jjcp_sj' =>['入仓时间'],
  773. 'jjcp_smb' =>['首末板'],
  774. 'jjcp_num' =>['单据编号'],
  775. '订单编号' =>['订单编号'],
  776. ];
  777. $data[0] = [];
  778. foreach ($req['fields'] as $k=>$v){
  779. if(array_key_exists($v,$arr)){
  780. array_push($data[0],$arr[$v][0]);
  781. }
  782. }
  783. //客户编号
  784. $rows = db('成品入仓')->alias('c')
  785. ->field('rtrim(c.入仓类型) as 入仓类型, rtrim(c.仓库编号) as 仓库编号, rtrim(c.仓库名称) as 仓库名称,
  786. c.jjcp_gdbh, rtrim(c.jjcp_cpdh) as jjcp_cpdh, rtrim(c.jjcp_cpmc) as jjcp_cpmc,
  787. c.jjcp_sl, rtrim(c.jjcp_dw) as jjcp_dw, c.jjcp_sj,
  788. rtrim(c.jjcp_smb) as jjcp_smb,
  789. rtrim(c.jjcp_num) as jjcp_num, rtrim(c.订单编号) as 订单编号
  790. ')
  791. ->where('c.jjcp_sj','between time',[$req['start_time'], $req['end_time']])
  792. ->order('c.jjcp_sj asc')
  793. ->join('工单_基本资料 g','g.Gd_gdbh=c.jjcp_gdbh')
  794. ->where('g.Gd_cpdh=c.jjcp_cpdh')
  795. ->select();
  796. foreach ($rows as $key=>$value) {
  797. $subArray = [];
  798. foreach ($arr as $k=>$v){
  799. $subArray[$k] = $rows[$key][$k];
  800. }
  801. array_push($data,$subArray);
  802. }
  803. $this->success('成功',['file_name'=>$req['file_name'],'data'=>$data]);
  804. }
  805. /**
  806. * 月度B
  807. * @ApiMethod (POST)
  808. * @param string start_time
  809. * @param string end_time
  810. * @param string file_name
  811. * @param array fields
  812. */
  813. public function monthExportB(){
  814. if(!$this->request->isGet()){
  815. $this->error('请求方式错误');
  816. }
  817. $req = $this->request->param();
  818. $arr = [
  819. 'start_time', 'end_time', 'file_name'
  820. ];
  821. foreach ($arr as $key => $value){
  822. if (!isset($req[$value])){
  823. $this->error('参数错误',$value,$key+1);
  824. }
  825. }
  826. if (!(isset($req['fields']) && count($req['fields'])!=0)){
  827. $this->error('参数错误','fields',100);
  828. }
  829. $arr = [
  830. '入仓类型' =>['入仓类型'],
  831. '客户编号' =>['客户编号'],
  832. '客户名称' =>['客户名称'],
  833. '产品类别' =>['产品类别'],
  834. 'jjcp_gdbh' =>['工单编号'],
  835. '成品编码' =>['成品编码'],
  836. '成品名称' =>['成品名称'],
  837. 'jjcp_sls' =>['入仓数量'],
  838. '大箱产量' =>['大箱产量'],
  839. '产品单价' =>['万张单价'],
  840. '产值万元' =>['产值万元'],
  841. ];
  842. $data[0] = [];
  843. foreach ($req['fields'] as $k=>$v){
  844. if(array_key_exists($v,$arr)){
  845. array_push($data[0],$arr[$v][0]);
  846. }
  847. }
  848. $rows1 = db('成品入仓')->alias('c')
  849. ->field('rtrim(c.入仓类型) as 入仓类型, LEFT(c.成品编码,'.config('product_code_digit').') as 客户编号, rtrim(b.客户名称) as 客户名称,
  850. rtrim(b.产品类别) as 产品类别, c.jjcp_gdbh,
  851. rtrim(c.成品编码) as 成品编码, rtrim(c.成品名称) as 成品名称
  852. ')
  853. ->where('c.jjcp_sj','between time',[$req['start_time'], $req['end_time']])
  854. ->order('客户编号 asc,b.产品类别 desc,c.成品编码 asc,c.jjcp_gdbh asc')
  855. ->join('产品_基本资料 b','b.产品编号=c.jjcp_cpdh')
  856. ->group('c.jjcp_gdbh')
  857. ->select();
  858. $rows2 = db('成品入仓')
  859. ->field('rtrim(入仓类型) as 入仓类型, rtrim(成品编码) as 客户编号,
  860. rtrim(成品编码) as 客户名称, rtrim(成品编码) as 产品类别,
  861. jjcp_gdbh,rtrim(成品编码) as 成品编码, rtrim(成品名称) as 成品名称
  862. ')
  863. ->where('jjcp_sj','between time',[$req['start_time'], $req['end_time']])
  864. ->where('成品编码','')
  865. ->order('jjcp_gdbh asc')
  866. ->group('jjcp_gdbh')
  867. ->select();
  868. $rows = array_merge($rows2,$rows1);
  869. $num = db('工单_基本资料')->count();
  870. $arr1 = db('工单_基本资料')->limit($num)
  871. ->column('CONCAT(Gd_gdbh, "-", rtrim(Gd_cpdh)) AS gdcp, 产品单价 as 万张单价');
  872. $brr = array_column($rows,'jjcp_gdbh');
  873. $brr = db('成品入仓')
  874. ->where('jjcp_gdbh', 'in', $brr)
  875. ->group('jjcp_gdbh')
  876. ->column('jjcp_gdbh, sum(jjcp_sl)');
  877. foreach ($rows as $key=>$value) {
  878. $subArray = [];
  879. foreach ($arr as $k=>$v){
  880. if($k=='jjcp_sls'){
  881. $subArray[$k] = $brr[$value['jjcp_gdbh']];
  882. }else if ($k=='大箱产量'){
  883. if($value['产品类别']=='条盒'){
  884. $subArray[$k] = $brr[$value['jjcp_gdbh']]/250;
  885. }else if($value['产品类别']=='小盒'){
  886. $subArray[$k] = $brr[$value['jjcp_gdbh']]/2500;
  887. }else{
  888. $subArray[$k] = 0;
  889. }
  890. }else if ($k=='产品单价'){
  891. if($value['产品类别']!=''){
  892. if (array_key_exists($value['jjcp_gdbh'].'-'.$value['成品编码'],$arr1)){
  893. $subArray[$k] = floatval(number_format($arr1[$value['jjcp_gdbh'].'-'.$value['成品编码']],4));
  894. }
  895. }else{
  896. $subArray[$k] = 0;
  897. }
  898. }else if ($k=='产值万元'){
  899. if($value['产品类别']!=''){
  900. if (array_key_exists($value['jjcp_gdbh'].'-'.$value['成品编码'],$arr1)){
  901. $subArray[$k] = floatval(number_format($arr1[$value['jjcp_gdbh'].'-'.$value['成品编码']]*$brr[$value['jjcp_gdbh']]/100000000,2));
  902. }
  903. }else{
  904. $subArray[$k] = 0;
  905. }
  906. }else{
  907. $subArray[$k] = $rows[$key][$k];
  908. }
  909. }
  910. array_push($data,$subArray);
  911. }
  912. $this->success('成功',['file_name'=>$req['file_name'],'data'=>$data]);
  913. }
  914. /**
  915. * 各日统计列表
  916. * @ApiMethod (GET)
  917. * @param string $date 时间
  918. * @param string $sys_id 用户
  919. */
  920. public function getDayList()
  921. {
  922. //get请求
  923. if(!$this->request->isGet()){
  924. $this->error('请求方式错误');
  925. }
  926. $req = $this->request->param();
  927. $page = 1;
  928. $limit = 15;
  929. if (isset($req['page']) && !empty($req['page'])) $page = $req['page'];
  930. if (isset($req['limit']) && !empty($req['limit'])) $limit = $req['limit'];
  931. $where = [
  932. 'c.jjcp_smb'=>['<>',''],
  933. ];
  934. if (isset($req['key_word']) && !empty($req['key_word'])){
  935. $where['c.jjcp_sj | c.jjcp_gdbh | c.jjcp_cpmc | c.jjcp_cpdh | g.实际投料'] = ['like','%'.$req['key_word'].'%'];
  936. }
  937. //客户编号
  938. $rows = db('成品入仓')->alias('c')
  939. ->field('LEFT(c.jjcp_sj, 10) as jjcp_sj, c.jjcp_gdbh, rtrim(c.jjcp_cpmc) as jjcp_cpmc,
  940. rtrim(c.jjcp_cpdh) as jjcp_cpdh, g.实际投料')
  941. ->where($where)
  942. ->where('g.Gd_cpdh=c.jjcp_cpdh')
  943. ->join('工单_基本资料 g','g.Gd_gdbh=c.jjcp_gdbh')
  944. ->order('c.jjcp_sj desc')
  945. ->group('c.jjcp_gdbh, c.jjcp_cpdh')
  946. ->page($page,$limit)
  947. ->select();
  948. $total = db('成品入仓')->alias('c')
  949. ->where($where)
  950. ->where('g.Gd_cpdh=c.jjcp_cpdh')
  951. ->join('工单_基本资料 g','g.Gd_gdbh=c.jjcp_gdbh')
  952. ->group('c.jjcp_gdbh, c.jjcp_cpdh')
  953. ->count();
  954. $brr = db('成品入仓')
  955. ->group('jjcp_gdbh,jjcp_cpdh')
  956. ->column('CONCAT(jjcp_gdbh, "-", rtrim(jjcp_cpdh)), sum(jjcp_sl)');
  957. foreach ($rows as $key=>$value) {
  958. $rows[$key]['实际投料'] = (string)floatval($value['实际投料']*10000);
  959. if(array_key_exists($value['jjcp_gdbh'].'-'.$value['jjcp_cpdh'],$brr)){
  960. $rows[$key]['入仓数量'] = $brr[$value['jjcp_gdbh'].'-'.$value['jjcp_cpdh']];
  961. if($rows[$key]['实际投料']!=0){
  962. $rows[$key]['投入产出率'] = number_format($rows[$key]['入仓数量']/$rows[$key]['实际投料']*100,2);
  963. }else{
  964. $rows[$key]['投入产出率'] = '';
  965. }
  966. }else{
  967. $rows[$key]['入仓数量'] = '';
  968. $rows[$key]['投入产出率'] = '';
  969. }
  970. }
  971. $data = [
  972. 'total' => $total,
  973. 'rows' => $rows,
  974. ];
  975. $this->success('成功',$data);
  976. }
  977. /**
  978. * 各日统计列表excel导出
  979. * @ApiMethod (POST)
  980. * @param string file_name
  981. * @param array fields
  982. */
  983. public function dayExport()
  984. {
  985. //get请求
  986. if(!$this->request->isPost()){
  987. $this->error('请求方式错误');
  988. }
  989. $req = $this->request->param();
  990. if (!isset($req['file_name'])){
  991. $this->error('参数错误','file_name');
  992. }
  993. if (!(isset($req['fields']) && count($req['fields'])!=0)){
  994. $this->error('参数错误','fields',100);
  995. }
  996. if (isset($req['key_word']) && !empty($req['key_word'])){
  997. $where['c.jjcp_sj | c.jjcp_gdbh | c.jjcp_cpmc | c.jjcp_cpdh | g.实际投料'] = ['like','%'.$req['key_word'].'%'];
  998. }
  999. $arr = [
  1000. 'jjcp_gdbh' =>['jjcp_gdbh'],
  1001. 'jjcp_yjno' =>['jjcp_yjno'],
  1002. 'jjcp_cpmc' =>['印件名称'],
  1003. '成品编码' =>['成品编码'],
  1004. '成品名称' =>['成品名称'],
  1005. '实际投料' =>['实际投料'],
  1006. 'jjcp_sls' =>['入仓数量'],
  1007. 'jjcp_sj' =>['完工日期'],
  1008. ];
  1009. $data[0] = [];
  1010. foreach ($req['fields'] as $k=>$v){
  1011. if(array_key_exists($v,$arr)){
  1012. array_push($data[0],$arr[$v][0]);
  1013. }
  1014. }
  1015. $where = [
  1016. 'c.jjcp_smb'=>['<>','']
  1017. ];
  1018. //客户编号
  1019. $rows = db('成品入仓')->alias('c')
  1020. ->field('c.jjcp_gdbh, c.jjcp_yjno, rtrim(c.jjcp_cpmc) as jjcp_cpmc, rtrim(c.jjcp_cpdh) as jjcp_cpdh,
  1021. rtrim(c.成品编码) as 成品编码, rtrim(c.成品名称) as 成品名称, sum(jjcp_sl) as jjcp_sls,
  1022. g.实际投料, LEFT(c.jjcp_sj, 10) as jjcp_sj
  1023. ')
  1024. ->where($where)
  1025. ->where('g.Gd_cpdh=c.jjcp_cpdh')
  1026. ->join('工单_基本资料 g','g.Gd_gdbh=c.jjcp_gdbh')
  1027. ->order('c.jjcp_sj desc')
  1028. ->group('c.jjcp_gdbh, c.jjcp_cpdh')
  1029. ->select();
  1030. foreach ($rows as $key=>$value) {
  1031. $subArray = [];
  1032. foreach ($arr as $k=>$v){
  1033. if($k=='实际投料'){
  1034. $subArray[$k] = (string)floatval($value['实际投料']*10000);
  1035. }else{
  1036. $subArray[$k] = $rows[$key][$k];
  1037. }
  1038. }
  1039. array_push($data,$subArray);
  1040. }
  1041. $this->success('成功',['file_name'=>$req['file_name'],'data'=>$data]);
  1042. }
  1043. /**
  1044. * 入仓末板数据统计列表
  1045. * @ApiMethod (GET)
  1046. * @param string $date 时间
  1047. * @param string $sys_id 用户
  1048. */
  1049. public function mbList()
  1050. {
  1051. //get请求
  1052. if(!$this->request->isGet()){
  1053. $this->error('请求方式错误');
  1054. }
  1055. $req = $this->request->param();
  1056. // $page = 1;
  1057. // $limit = 15;
  1058. // if (isset($req['page']) && !empty($req['page'])) $page = $req['page'];
  1059. // if (isset($req['limit']) && !empty($req['limit'])) $limit = $req['limit'];
  1060. $date = date('Y-m-d',strtotime('-3 month'));
  1061. $arr1 = db('db_包装产量预报')
  1062. ->field('sczl_gdbh1, sczl_gdbh2, sczl_gdbh3, sczl_gdbh4, sczl_gdbh5, sczl_gdbh6,
  1063. cast(substr(sczl_yjGx1,1,2) as signed) as sczl_yjGx1, cast(substr(sczl_yjGx2,1,2) as signed) as sczl_yjGx2,
  1064. cast(substr(sczl_yjGx3,1,2) as signed) as sczl_yjGx3, cast(substr(sczl_yjGx4,1,2) as signed) as sczl_yjGx4,
  1065. cast(substr(sczl_yjGx5,1,2) as signed) as sczl_yjGx5, cast(substr(sczl_yjGx6,1,2) as signed) as sczl_yjGx6,
  1066. (sczl_cl1*sczl_PgCl1+sczl_clAdd1) as sczl_cls1,
  1067. (sczl_cl2*sczl_PgCl2+sczl_clAdd2) as sczl_cls2,
  1068. (sczl_cl3*sczl_PgCl3+sczl_clAdd3) as sczl_cls3,
  1069. (sczl_cl4*sczl_PgCl4+sczl_clAdd4) as sczl_cls4,
  1070. (sczl_cl5*sczl_PgCl5+sczl_clAdd5) as sczl_cls5,
  1071. (sczl_cl6*sczl_PgCl6+sczl_clAdd6) as sczl_cls6
  1072. ')
  1073. ->where('sczl_rq','>=',$date)
  1074. ->select();
  1075. $data1 = [];
  1076. foreach ($arr1 as $k=>$v){
  1077. for ($i=1;$i<=6;$i++){
  1078. if (!empty($v['sczl_gdbh'.$i])){
  1079. $v['sczl_gdbh'.$i] = strtoupper($v['sczl_gdbh'.$i]);
  1080. if(array_key_exists($v['sczl_gdbh'.$i].'-'.$v['sczl_yjGx'.$i],$data1)){
  1081. $data1[$v['sczl_gdbh'.$i].'-'.$v['sczl_yjGx'.$i]] += $v['sczl_cls'.$i];
  1082. }else{
  1083. $data1[$v['sczl_gdbh'.$i].'-'.$v['sczl_yjGx'.$i]] = $v['sczl_cls'.$i];
  1084. }
  1085. }
  1086. }
  1087. }
  1088. krsort($data1);
  1089. //成品入仓中jjcp_smb不为空的工单
  1090. $rows2 = db('成品入仓')
  1091. ->where('jjcp_sj','>=',$date)
  1092. ->group('jjcp_gdbh, jjcp_cpdh')
  1093. ->column("CONCAT( jjcp_gdbh, '-', jjcp_yjno) AS gdyj, max(jjcp_smb) as jjcp_smb,
  1094. sum(jjcp_sl) as jjcp_sls, left(max(jjcp_sj),10) as jjcp_sj,UniqId");
  1095. $data = [];
  1096. foreach($data1 as $key=>$value){
  1097. $subArray = [];
  1098. $gdbh = strtoupper(explode('-',$key)[0]);
  1099. $yjno = explode('-',$key)[1];
  1100. $res = db('工单_印件资料')
  1101. ->where('Yj_Gdbh',$gdbh)
  1102. ->where('yj_Yjno',$yjno)
  1103. ->field('Yj_Gdbh, yj_Yjno, yj_yjmc, yj_成品数量, (yj_平张投料 * yj_ks * yj_ls) as 投料')
  1104. ->find();
  1105. if(!empty($res)){
  1106. $subArray['Yj_Gdbh'] = $res['Yj_Gdbh'];
  1107. $subArray['yj_Yjno'] = $res['yj_Yjno'];
  1108. $subArray['yj_yjmc'] = trim($res['yj_yjmc']);
  1109. $subArray['yj_成品数量'] = (string)floatval($res['yj_成品数量']*10000);
  1110. $subArray['yj_实际投料'] = (string)floatval($res['投料']);
  1111. }else{
  1112. $subArray['Yj_Gdbh'] = '';
  1113. $subArray['yj_Yjno'] = '';
  1114. $subArray['yj_yjmc'] = '';
  1115. $subArray['yj_成品数量'] = '';
  1116. $subArray['yj_实际投料'] = '';
  1117. }
  1118. if(array_key_exists($key,$rows2)){
  1119. $subArray['jjcp_smb'] = $rows2[$key]['jjcp_smb'];
  1120. $subArray['jjcp_sls'] = $rows2[$key]['jjcp_sls'];
  1121. $subArray['jjcp_sj'] = $rows2[$key]['jjcp_sj'];
  1122. }else{
  1123. $subArray['jjcp_smb'] = '';
  1124. $subArray['jjcp_sls'] = '';
  1125. $subArray['jjcp_sj'] = '';
  1126. }
  1127. $subArray['sczl_cls'] = (string)floatval($value);
  1128. array_push($data,$subArray);
  1129. }
  1130. $data = [
  1131. 'total' => count($data),
  1132. 'rows' => $data,
  1133. ];
  1134. $this->success('成功',$data);
  1135. }
  1136. /**
  1137. * 入仓末板数据统计列表excel导出
  1138. * @ApiMethod (POST)
  1139. * @param string file_name
  1140. * @param array fields
  1141. */
  1142. public function mbExport()
  1143. {
  1144. //get请求
  1145. if(!$this->request->isPost()){
  1146. $this->error('请求方式错误');
  1147. }
  1148. $req = $this->request->param();
  1149. if (!isset($req['file_name'])){
  1150. $this->error('参数错误','file_name');
  1151. }
  1152. if (!(isset($req['fields']) && count($req['fields'])!=0)){
  1153. $this->error('参数错误','fields',100);
  1154. }
  1155. $arr = [
  1156. 'Yj_Gdbh' =>['工单编号'],
  1157. 'yj_Yjno' =>['印件号'],
  1158. 'yj_yjmc' =>['印件名称'],
  1159. 'yj_成品数量' =>['计划交货数量'],
  1160. 'yj_实际投料' =>['印件投料'],
  1161. 'sczl_cls' =>['包装产量'],
  1162. 'jjcp_sj' =>['最近入仓时间'],
  1163. 'jjcp_sls' =>['入仓产量'],
  1164. 'status' =>['完工'],
  1165. '缺数' =>['缺数'],
  1166. ];
  1167. $data[0] = [];
  1168. foreach ($req['fields'] as $k=>$v){
  1169. if(array_key_exists($v,$arr)){
  1170. array_push($data[0],$arr[$v][0]);
  1171. }
  1172. }
  1173. $date = date('Y-m-d',strtotime('-3 month'));
  1174. $arr1 = db('db_包装产量预报')
  1175. ->field('sczl_gdbh1, sczl_gdbh2, sczl_gdbh3, sczl_gdbh4, sczl_gdbh5, sczl_gdbh6,
  1176. cast(substr(sczl_yjGx1,1,2) as signed) as sczl_yjGx1, cast(substr(sczl_yjGx2,1,2) as signed) as sczl_yjGx2,
  1177. cast(substr(sczl_yjGx3,1,2) as signed) as sczl_yjGx3, cast(substr(sczl_yjGx4,1,2) as signed) as sczl_yjGx4,
  1178. cast(substr(sczl_yjGx5,1,2) as signed) as sczl_yjGx5, cast(substr(sczl_yjGx6,1,2) as signed) as sczl_yjGx6,
  1179. (sczl_cl1*sczl_PgCl1+sczl_clAdd1) as sczl_cls1,
  1180. (sczl_cl2*sczl_PgCl2+sczl_clAdd2) as sczl_cls2,
  1181. (sczl_cl3*sczl_PgCl3+sczl_clAdd3) as sczl_cls3,
  1182. (sczl_cl4*sczl_PgCl4+sczl_clAdd4) as sczl_cls4,
  1183. (sczl_cl5*sczl_PgCl5+sczl_clAdd5) as sczl_cls5,
  1184. (sczl_cl6*sczl_PgCl6+sczl_clAdd6) as sczl_cls6
  1185. ')
  1186. ->where('sczl_rq','>=',$date)
  1187. ->select();
  1188. $data1 = [];
  1189. foreach ($arr1 as $k=>$v){
  1190. for ($i=1;$i<=6;$i++){
  1191. if(array_key_exists($v['sczl_gdbh'.$i].'-'.$v['sczl_yjGx'.$i],$data1)){
  1192. $data1[$v['sczl_gdbh'.$i].'-'.$v['sczl_yjGx'.$i]] += $v['sczl_cls'.$i];
  1193. }else{
  1194. $data1[$v['sczl_gdbh'.$i].'-'.$v['sczl_yjGx'.$i]] = $v['sczl_cls'.$i];
  1195. }
  1196. }
  1197. }
  1198. krsort($data1);
  1199. //成品入仓中jjcp_smb不为空的工单
  1200. $rows1 = db('成品入仓')
  1201. ->where('jjcp_smb','<>','')
  1202. ->order('jjcp_gdbh desc')
  1203. ->column("CONCAT(jjcp_gdbh, '-', jjcp_yjno) AS gdyj, UniqId");
  1204. $rows2 = db('成品入仓')
  1205. ->where('jjcp_sj','>=',$date)
  1206. ->group('jjcp_gdbh, jjcp_cpdh')
  1207. ->column("CONCAT( jjcp_gdbh, '-', jjcp_yjno) AS gdyj, jjcp_smb,
  1208. sum(jjcp_sl) as jjcp_sls, left(max(jjcp_sj),10) as jjcp_sj");
  1209. $rows3 = db('工单_印件资料')->cache(true,86400)
  1210. ->group('Yj_Gdbh, yj_Yjno')
  1211. ->column("CONCAT( Yj_Gdbh, '-', yj_Yjno) AS gdyj,
  1212. Yj_Gdbh, yj_Yjno, yj_yjmc, yj_成品数量, yj_实际投料");
  1213. foreach($data1 as $key=>$value){
  1214. if(!array_key_exists($key,$rows1)){
  1215. $subArray = [];
  1216. foreach ($arr as $k=>$v){
  1217. if($k=='yj_成品数量' || $k=='yj_实际投料'){
  1218. if(array_key_exists($key,$rows3)){
  1219. $subArray[$k] = (string)floatval($rows3[$key][$k]*10000);
  1220. }else{
  1221. $subArray[$k] = '';
  1222. }
  1223. }else if($k=='Yj_Gdbh' || $k=='yj_Yjno'){
  1224. if(array_key_exists($key,$rows3)){
  1225. $subArray[$k] = $rows3[$key][$k];
  1226. }else{
  1227. $subArray[$k] = '';
  1228. }
  1229. }else if($k=='yj_yjmc'){
  1230. if(array_key_exists($key,$rows3)){
  1231. $subArray[$k] = trim($rows3[$key][$k]);
  1232. }else{
  1233. $subArray[$k] = '';
  1234. }
  1235. }else if($k=='sczl_cls'){
  1236. $subArray[$k] = (string)floatval($value);
  1237. }else if($k=='status'){
  1238. if(array_key_exists($key,$rows2) && $rows2[$key]['jjcp_smb'] !=''){
  1239. $subArray[$k] = '完工';
  1240. }else{
  1241. $subArray[$k] = '';
  1242. }
  1243. }else if($k=='缺数'){
  1244. if(array_key_exists($key,$rows2) && $rows2[$key]['jjcp_smb'] !=''){
  1245. $subArray[$k]['缺数'] = (int)$rows3[$key]['yj_成品数量']-(int)$value;
  1246. }else{
  1247. $subArray[$k] = '';
  1248. }
  1249. }else{
  1250. if(array_key_exists($key,$rows2)){
  1251. $subArray[$k] = $rows2[$key][$k];
  1252. $subArray[$k] = $rows2[$key][$k];
  1253. $subArray[$k] = $rows2[$key][$k];
  1254. }else{
  1255. $subArray[$k] = '';
  1256. $subArray[$k] = '';
  1257. $subArray[$k] = '';
  1258. }
  1259. }
  1260. }
  1261. array_push($data,$subArray);
  1262. }
  1263. }
  1264. $this->success('成功',['file_name'=>$req['file_name'],'data'=>$data]);
  1265. }
  1266. /**
  1267. * 构建新增入仓数据
  1268. * @param array $req
  1269. * @return array
  1270. */
  1271. protected function buildAddData(array $req)
  1272. {
  1273. $fields = [
  1274. 'Sys_id',
  1275. '入仓类型', '仓库编号', '仓库名称',
  1276. 'jjcp_num',
  1277. 'jjcp_gdbh', 'jjcp_yjno', '订单编号',
  1278. 'jjcp_cpdh', 'jjcp_cpmc',
  1279. '成品编码', '成品名称',
  1280. '客户料号',
  1281. 'jjcp_smb', 'jjcp_sj',
  1282. 'jjcp_sl', 'jjcp_dw',
  1283. '机型备注',
  1284. 'jjcp_desc',
  1285. ];
  1286. $data = [];
  1287. foreach ($fields as $index => $field) {
  1288. if (!isset($req[$field])) {
  1289. $this->error('参数错误', $field, $index + 1);
  1290. }
  1291. $data[$field] = $req[$field];
  1292. }
  1293. $data['Sys_rq'] = date('Y-m-d H:i:s');
  1294. return $data;
  1295. }
  1296. /**
  1297. * 校验印件产品名称
  1298. * @param string $gdbh
  1299. * @param string $yjno
  1300. * @param string $cpmc
  1301. */
  1302. protected function validateProductName($gdbh, $yjno, $cpmc)
  1303. {
  1304. $name = db('工单_印件资料')
  1305. ->where('Yj_Gdbh', $gdbh)
  1306. ->where('yj_Yjno', $yjno)
  1307. ->value('yj_yjmc');
  1308. if ($name !== $cpmc) {
  1309. $this->error('产品名称错误');
  1310. }
  1311. }
  1312. /**
  1313. * 查询工单印件是否已存在末版记录
  1314. * @param string $gdbh
  1315. * @param string $yjno
  1316. * @return array|null
  1317. */
  1318. protected function findExistingEndBoard($gdbh, $yjno)
  1319. {
  1320. return db('成品入仓')
  1321. ->where('jjcp_gdbh', $gdbh)
  1322. ->where('jjcp_yjno', $yjno)
  1323. ->where('jjcp_smb', '<>', '')
  1324. ->find();
  1325. }
  1326. /**
  1327. * 校验工单印件是否已完全领料
  1328. * @param string $gdbh
  1329. * @param string $yjno
  1330. */
  1331. protected function validateMaterialReceived($gdbh, $yjno)
  1332. {
  1333. $materialRecord = db('工单_印件资料')
  1334. ->alias('a')
  1335. ->join('物料_收发记录 b', 'a.Yj_Gdbh=b.st_gdbh and a.yj_cpdh=b.cpdh')
  1336. ->where('a.Yj_Gdbh', $gdbh)
  1337. ->where('a.yj_Yjno', $yjno)
  1338. ->group('b.st_wlbh')
  1339. ->order('b.st_wlbh asc')
  1340. ->column('b.st_wlbh');
  1341. $bomRecord = db('工单_印件资料')
  1342. ->alias('a')
  1343. ->join('工单_bom资料 b', 'a.Yj_Gdbh=b.BOM_工单编号 and a.yj_Yjno=b.BOM_工单行号 and a.yj_cpdh=b.BOM_产品编号')
  1344. ->where('a.Yj_Gdbh', $gdbh)
  1345. ->where('a.yj_Yjno', $yjno)
  1346. ->where('b.BOM_实际用量', '<>', 0)
  1347. ->group('b.BOM_物料编码')
  1348. ->order('b.BOM_物料编码 asc')
  1349. ->column('b.BOM_物料编码');
  1350. foreach ($bomRecord as $materialCode) {
  1351. if (!in_array($materialCode, $materialRecord)) {
  1352. $this->error('工单' . $gdbh . '印件' . $yjno . '物料' . $materialCode . '未领料');
  1353. }
  1354. }
  1355. }
  1356. /**
  1357. * 生成成品入仓 UniqId
  1358. * @return int
  1359. */
  1360. protected function generateFinishStockUniqId()
  1361. {
  1362. $uniqId = db('成品入仓')->max('UniqId');
  1363. return $uniqId < 10000000 ? 10000000 : $uniqId + 1;
  1364. }
  1365. /**
  1366. * 保存成品入仓数据到 MySQL 和 MongoDB
  1367. * @param array $data
  1368. * @param mixed $mongodb
  1369. */
  1370. protected function saveFinishStock(array $data, $mongodb)
  1371. {
  1372. db()->startTrans();
  1373. try {
  1374. $sql = db('成品入仓')->fetchSql(true)->insert($data);
  1375. $inserted = db()->query($sql);
  1376. if ($inserted === false) {
  1377. throw new \Exception('MySQL 保存失败');
  1378. }
  1379. $mongodb->name('finished_products')->insert($data);
  1380. db()->commit();
  1381. } catch (\Exception $e) {
  1382. db()->rollback();
  1383. $this->error($e->getMessage());
  1384. }
  1385. }
  1386. /**
  1387. * 判断是否为末版(仅判断首字符是否为「末」)
  1388. * @param string $smb
  1389. * @return bool
  1390. */
  1391. protected function isEndBoard($smb)
  1392. {
  1393. $smb = preg_replace('/^[\s\x{3000}]+|[\s\x{3000}]+$/u', '', (string)$smb);
  1394. if ($smb === '') {
  1395. return false;
  1396. }
  1397. return mb_substr($smb, 0, 1, 'UTF-8') === '末';
  1398. }
  1399. /**
  1400. * 是否需要同步成品入仓中间表
  1401. * 规则:
  1402. * 1. 工单编号以 Y 开头,且 >= Y2608001
  1403. * 2. 工单_基本资料.接单日期 >= 2026-08-01
  1404. * 3. 中间库 U8_06工单资料 中存在该工单编号
  1405. * @param string $gdbh
  1406. * @return bool
  1407. */
  1408. protected function shouldSyncFinishStockMiddle($gdbh)
  1409. {
  1410. $gdbh = strtoupper(trim((string)$gdbh));
  1411. if ($gdbh === '' || $gdbh[0] !== 'Y') {
  1412. return false;
  1413. }
  1414. if ((int)substr($gdbh, 1) < 2608001) {
  1415. return false;
  1416. }
  1417. $orderDate = db('工单_基本资料')->where('Gd_gdbh', $gdbh)->value('接单日期');
  1418. if (empty($orderDate)) {
  1419. return false;
  1420. }
  1421. if (strtotime(trim((string)$orderDate)) < strtotime('2026-08-01')) {
  1422. return false;
  1423. }
  1424. // db3:正式中间库,仅查询 U8_06工单资料是否存在该工单
  1425. // 实际写入走 syncFinishStockMiddle 中的 database.db8(本地 MySQL 测试库)
  1426. $middleDb = Db::connect(config('database.db3'));
  1427. $exists = $middleDb->name('U8_06工单资料')->where('工单编号', $gdbh)->count();
  1428. return $exists > 0;
  1429. }
  1430. /**
  1431. * 末版入仓后进入超节损
  1432. * @param string $gdbh
  1433. * @param string $yjno
  1434. */
  1435. protected function handleEndBoardSuperLoss($gdbh, $yjno)
  1436. {
  1437. $orderLoss = new OrderSuperLoss();
  1438. $result = [];
  1439. try {
  1440. $result = $orderLoss->OneOrderSuperLoss($gdbh, $yjno);
  1441. } catch (DataNotFoundException $e) {
  1442. } catch (ModelNotFoundException $e) {
  1443. } catch (DbException $e) {
  1444. }
  1445. if (empty($result)) {
  1446. return;
  1447. }
  1448. db('工单_质量考核汇总')
  1449. ->where('Gy0_gdbh', $gdbh)
  1450. ->where('印件及工序', $yjno)
  1451. ->delete();
  1452. $sql = db('工单_质量考核汇总')->fetchSql(true)->insertAll($result);
  1453. $inserted = db()->query($sql);
  1454. if ($inserted === false) {
  1455. throw new \Exception('超节损数据保存失败');
  1456. }
  1457. }
  1458. }