FinishedProductWarehousing.php 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625
  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. // 接单日期在 2026-08-01 及以后,且中间库 U8_06工单资料存在该工单时才同步
  413. if ($this->shouldSyncFinishStockMiddle($data['jjcp_gdbh'])) {
  414. try {
  415. $synchronization = new \app\api\controller\Synchronization();
  416. $synchronization->syncFinishStockMiddle($data['UniqId'], '新增');
  417. } catch (\Exception $e) {
  418. $this->error('成品入仓已保存,但' . $e->getMessage(), ['UniqId' => $data['UniqId']]);
  419. }
  420. }
  421. if ($this->isEndBoard($data['jjcp_smb'])) {
  422. $this->handleEndBoardSuperLoss($data['jjcp_gdbh'], $data['jjcp_yjno']);
  423. }
  424. $this->success('成功', ['UniqId' => $data['UniqId']]);
  425. }
  426. /**
  427. * 查询成品入仓同步操作日志
  428. * @ApiMethod (GET)
  429. * @param int|string $UniqId 成品入仓 UniqId
  430. */
  431. public function getSyncLog()
  432. {
  433. if (!$this->request->isGet()) {
  434. $this->error('请求方式错误');
  435. }
  436. $uniqId = $this->request->param('UniqId');
  437. if ($uniqId === null || $uniqId === '') {
  438. $this->error('参数错误', 'UniqId');
  439. }
  440. $list = db('工单_成品入仓同步操作日志')
  441. ->where('UniqId', (int)$uniqId)
  442. ->field('id,Gdbh,wlbm,yjmc,Yjdh,Yjno,sys_id,sys_rq,type,status,status_msg,UniqId')
  443. ->order('id desc')
  444. ->select();
  445. $this->success('成功', $list ?: []);
  446. }
  447. /**
  448. * 修改
  449. * 前端仅传:UniqId、数量(jjcp_sl)、smb(jjcp_smb)、sys_id(Sys_id)
  450. * @ApiMethod (POST)
  451. */
  452. public function edit()
  453. {
  454. if (!$this->request->isPost()) {
  455. $this->error('请求方式错误');
  456. }
  457. $req = $this->request->param();
  458. $uniqId = $req['UniqId'] ?? ($req['UniaId'] ?? null);
  459. if ($uniqId === null || trim((string)$uniqId) === '') {
  460. $this->error('参数错误', 'UniqId', 100);
  461. }
  462. $uniqId = (int)$uniqId;
  463. $old = db('成品入仓')->where('UniqId', $uniqId)->find();
  464. if (empty($old)) {
  465. $this->error('未找到成品入仓记录');
  466. }
  467. $hasQty = array_key_exists('jjcp_sl', $req) || array_key_exists('数量', $req);
  468. $hasSmb = array_key_exists('jjcp_smb', $req) || array_key_exists('smb', $req);
  469. $sysId = $req['Sys_id'] ?? ($req['sys_id'] ?? null);
  470. if (!$hasQty && !$hasSmb && ($sysId === null || $sysId === '')) {
  471. $this->error('参数错误', '', 111);
  472. }
  473. $data = ['Mod_rq' => date('Y-m-d H:i:s')];
  474. if ($sysId !== null && $sysId !== '') {
  475. $data['Sys_id'] = $sysId;
  476. }
  477. $oldQty = (float)$old['jjcp_sl'];
  478. $newQty = $oldQty;
  479. $qtyChanged = false;
  480. if ($hasQty) {
  481. $newQty = (float)($req['jjcp_sl'] ?? $req['数量']);
  482. $data['jjcp_sl'] = $newQty;
  483. $qtyChanged = abs($newQty - $oldQty) > 0.00001;
  484. }
  485. $oldSmb = (string)$old['jjcp_smb'];
  486. $newSmb = $oldSmb;
  487. $smbChanged = false;
  488. if ($hasSmb) {
  489. $newSmb = (string)($req['jjcp_smb'] ?? $req['smb']);
  490. $data['jjcp_smb'] = $newSmb;
  491. $smbChanged = trim($newSmb) !== trim($oldSmb);
  492. }
  493. // 改为末板时,校验同工单同印件是否已有其他末板记录
  494. if ($smbChanged && $this->isEndBoard($newSmb)) {
  495. $existingEndBoard = $this->findExistingEndBoard($old['jjcp_gdbh'], $old['jjcp_yjno']);
  496. if ($existingEndBoard && (int)$existingEndBoard['UniqId'] !== $uniqId) {
  497. $this->error(
  498. $existingEndBoard['jjcp_gdbh'] . '工单 -- 印件' . $existingEndBoard['jjcp_yjno'] . '已经进入末版,无法继续操作'
  499. );
  500. }
  501. }
  502. $mongodb = Db::connect(config('database.mongodb'));
  503. db()->startTrans();
  504. try {
  505. $sql = db('成品入仓')->where('UniqId', $uniqId)->fetchSql(true)->update($data);
  506. $bool = db()->query($sql);
  507. $mongodb->name('finished_products')
  508. ->where('UniqId', $uniqId)
  509. ->update($data);
  510. db()->commit();
  511. } catch (\Exception $e) {
  512. db()->rollback();
  513. $this->error($e->getMessage());
  514. }
  515. if ($bool === false) {
  516. $this->error('失败');
  517. }
  518. // 同步中间表:
  519. // - 数量变更:插入差值为新同步数据(增加为正、减少为负)
  520. // - 仅修改 smb:正常同步完整数量
  521. // - 二者同时改:只同步差值(此时记录已是新 smb,中间表会带上末板标记)
  522. if ($this->shouldSyncFinishStockMiddle($old['jjcp_gdbh']) && ($qtyChanged || $smbChanged)) {
  523. try {
  524. $synchronization = new \app\api\controller\Synchronization();
  525. if ($qtyChanged) {
  526. $qtyDiff = $newQty - $oldQty; // 增加为正,减少为负
  527. $synchronization->syncFinishStockMiddle($uniqId, '修改', $qtyDiff);
  528. } else {
  529. $synchronization->syncFinishStockMiddle($uniqId, '修改');
  530. }
  531. } catch (\Exception $e) {
  532. $this->error('成品入仓已保存,但' . $e->getMessage(), ['UniqId' => $uniqId]);
  533. }
  534. }
  535. if ($smbChanged && $this->isEndBoard($newSmb)) {
  536. $this->handleEndBoardSuperLoss($old['jjcp_gdbh'], $old['jjcp_yjno']);
  537. }
  538. $this->success('成功');
  539. }
  540. /**
  541. * 删除
  542. * 前端传:UniqId、sys_id(Sys_id)
  543. * @ApiMethod (POST)
  544. */
  545. public function del()
  546. {
  547. if (!$this->request->isPost()) {
  548. $this->error('请求方式错误');
  549. }
  550. $req = $this->request->param();
  551. if (!(isset($req['UniqId']) && trim($req['UniqId']) != '')) {
  552. $this->error('参数错误', 'UniqId', 100);
  553. }
  554. $sysId = $req['Sys_id'] ?? ($req['sys_id'] ?? null);
  555. if ($sysId === null || trim((string)$sysId) === '') {
  556. $this->error('参数错误', 'sys_id', 101);
  557. }
  558. $uniqId = (int)$req['UniqId'];
  559. $old = db('成品入仓')->where('UniqId', $uniqId)->find();
  560. if (empty($old)) {
  561. $this->error('未找到成品入仓记录');
  562. }
  563. // 删除前同步中间表:MES插入类型=新增,数量取负;同步失败则不删除
  564. if ($this->shouldSyncFinishStockOnDelete($old['jjcp_gdbh'], $uniqId)) {
  565. try {
  566. $synchronization = new \app\api\controller\Synchronization();
  567. $synchronization->syncFinishStockMiddle(
  568. $uniqId,
  569. '新增',
  570. -1 * (float)$old['jjcp_sl'],
  571. $sysId
  572. );
  573. } catch (\Exception $e) {
  574. $this->error('同步中间表失败,未删除:' . $e->getMessage(), ['UniqId' => $uniqId]);
  575. }
  576. }
  577. $mongodb = Db::connect(config('database.mongodb'));
  578. db()->startTrans();
  579. try {
  580. $sql = db('成品入仓')->where('UniqId', $uniqId)->fetchSql(true)->delete();
  581. $bool = db()->query($sql);
  582. $mongodb->name('finished_products')
  583. ->where('UniqId', $uniqId)
  584. ->delete();
  585. db()->commit();
  586. } catch (\Exception $e) {
  587. db()->rollback();
  588. $this->error($e->getMessage());
  589. }
  590. if ($bool === false) {
  591. $this->error('失败');
  592. }
  593. $this->success('成功');
  594. }
  595. /**
  596. * excel导出
  597. * @ApiMethod (POST)
  598. * @param string date
  599. * @param string sys_id
  600. * @param array fields
  601. */
  602. public function export(){
  603. if(!$this->request->isGet()){
  604. $this->error('请求方式错误');
  605. }
  606. $req = $this->request->param();
  607. if (!isset($req['date'])) $this->error('参数缺失');
  608. if (!(isset($req['fields']) && count($req['fields'])!=0)){
  609. $this->error('参数错误','fields',100);
  610. }
  611. if(strpos($req['date'],'-')){
  612. $where = ['c.Sys_rq'=>['like',$req['date'].'%']];
  613. }else if(empty($req['date'])){
  614. $where = [];
  615. }else{
  616. $where = ['c.jjcp_gdbh'=>$req['date']];
  617. }
  618. if (isset($req['sys_id']) && !empty($req['sys_id'])){
  619. $where['c.Sys_id'] = ['LIKE',$req['sys_id'].'%'];
  620. }
  621. $arr = [
  622. '入仓类型' =>['入仓类型'],
  623. '仓库编号' =>['仓库编号'],
  624. '仓库名称' =>['仓库名称'],
  625. 'jjcp_num' =>['入仓单号'],
  626. 'jjcp_gdbh' =>['工单编号'],
  627. 'jjcp_yjno' =>['印件号'],
  628. 'jjcp_sl' =>['入仓数量'],
  629. 'jjcp_sls' =>['累计入仓数量'],
  630. '订单数量' =>['订单数量'],
  631. '完成率' =>['完成率'],
  632. 'jjcp_dw' =>['单位'],
  633. 'jjcp_sj' =>['入仓时间'],
  634. 'jjcp_smb' =>['首末板'],
  635. 'jjcp_cpdh' =>['印件代号'],
  636. 'jjcp_cpmc' =>['印件名称'],
  637. '成品编码' =>['产品编号'],
  638. '成品名称' =>['产品名称'],
  639. '客户料号' =>['客户料号'],
  640. 'jjcp_desc' =>['备注'],
  641. '客户编号' =>['客户编号'],
  642. 'Gd_客户名称' =>['客户名称'],
  643. '最近入仓日期' =>['最近入仓日期'],
  644. '机型备注' =>['机型备注'],
  645. ];
  646. $data[0] = [];
  647. foreach ($req['fields'] as $k=>$v){
  648. if(array_key_exists($v,$arr)){
  649. array_push($data[0],$arr[$v][0]);
  650. }
  651. }
  652. $num = db('成品入仓')->alias('c')
  653. ->where($where)
  654. ->where('g.Gd_cpdh=c.jjcp_cpdh')
  655. ->join('工单_基本资料 g','g.Gd_gdbh=c.jjcp_gdbh')
  656. ->count();
  657. $rows = [];
  658. for($i=1;$i<=ceil($num/10000);$i++){
  659. $p = $i == ceil($num/10000) ? $num%10000 : 10000;
  660. $r = db('成品入仓')->alias('c')
  661. ->field('rtrim(c.入仓类型) as 入仓类型, rtrim(c.仓库编号) as 仓库编号, rtrim(c.仓库名称) as 仓库名称,
  662. rtrim(c.jjcp_num) as jjcp_num, c.jjcp_gdbh,
  663. c.jjcp_yjno, c.jjcp_sl, g.订单数量, rtrim(c.jjcp_dw) as jjcp_dw, LEFT(c.jjcp_sj, 10) as jjcp_sj,
  664. rtrim(c.jjcp_smb) as jjcp_smb, rtrim(c.jjcp_cpdh) as jjcp_cpdh, rtrim(c.jjcp_cpmc) as jjcp_cpmc,
  665. rtrim(c.成品编码) as 成品编码, rtrim(c.成品名称) as 成品名称,
  666. rtrim(c.客户料号) as 客户料号, rtrim(c.jjcp_desc) as jjcp_desc, LEFT(c.成品编码,'.config('product_code_digit').') as 客户编号, rtrim(g.Gd_客户名称) as Gd_客户名称,
  667. rtrim(c.机型备注) as 机型备注')
  668. ->where($where)
  669. ->where('g.Gd_cpdh=c.jjcp_cpdh')
  670. ->join('工单_基本资料 g','g.Gd_gdbh=c.jjcp_gdbh')
  671. ->order('c.UniqId desc')
  672. ->limit($i,$p)
  673. ->select();
  674. $rows = array_merge($rows,$r);
  675. }
  676. foreach ($rows as $key=>$value) {
  677. $row = db('成品入仓')
  678. ->field('LEFT(Sys_rq, 10) as Sys_rq,jjcp_sl')
  679. ->where(['jjcp_gdbh'=>$value['jjcp_gdbh'], 'jjcp_cpdh'=>$value['jjcp_cpdh']])
  680. ->order('Sys_rq desc')
  681. ->select();
  682. $subArray = [];
  683. foreach ($arr as $k=>$v){
  684. if($k=='jjcp_sls'){
  685. $subArray[$k] = 0;
  686. foreach ($row as $n) {
  687. $subArray[$k] += $n['jjcp_sl'];
  688. }
  689. }else if($k=='完成率'){
  690. if ($value['订单数量']!=0){
  691. $subArray[$k] = 0;
  692. foreach ($row as $n) {
  693. $subArray[$k] += $n['jjcp_sl'];
  694. }
  695. $subArray[$k] = number_format($subArray[$k]/$value['订单数量']/100,2);
  696. }else{
  697. $subArray[$k] = '';
  698. }
  699. }else if($k=='最近入仓日期'){
  700. $subArray[$k] = $row[0]['Sys_rq'];
  701. }else{
  702. $subArray[$k] = $rows[$key][$k];
  703. }
  704. }
  705. array_push($data,$subArray);
  706. }
  707. $this->success('成功',['file_name'=>$req['file_name'],'data'=>$data]);
  708. }
  709. /**
  710. * 月度A
  711. * @ApiMethod (POST)
  712. * @param string start_time
  713. * @param string end_time
  714. * @param string file_name
  715. * @param array fields
  716. */
  717. public function monthExportA(){
  718. if(!$this->request->isGet()){
  719. $this->error('请求方式错误');
  720. }
  721. $req = $this->request->param();
  722. $arr = [
  723. 'start_time', 'end_time', 'file_name'
  724. ];
  725. foreach ($arr as $key => $value){
  726. if (!isset($req[$value])){
  727. $this->error('参数错误',$value,$key+1);
  728. }
  729. }
  730. if (!(isset($req['fields']) && count($req['fields'])!=0)){
  731. $this->error('参数错误','fields',100);
  732. }
  733. $arr = [
  734. '入仓类型' =>['入仓类型'],
  735. '仓库编号' =>['仓库编号'],
  736. '仓库名称' =>['仓库名称'],
  737. 'jjcp_gdbh' =>['工单编号'],
  738. 'jjcp_cpdh' =>['入仓存货编号'],
  739. 'jjcp_cpmc' =>['入仓存货名称'],
  740. 'jjcp_sl' =>['入仓数量'],
  741. 'jjcp_dw' =>['单位'],
  742. 'jjcp_sj' =>['入仓时间'],
  743. 'jjcp_smb' =>['首末板'],
  744. 'jjcp_num' =>['单据编号'],
  745. '订单编号' =>['订单编号'],
  746. ];
  747. $data[0] = [];
  748. foreach ($req['fields'] as $k=>$v){
  749. if(array_key_exists($v,$arr)){
  750. array_push($data[0],$arr[$v][0]);
  751. }
  752. }
  753. //客户编号
  754. $rows = db('成品入仓')->alias('c')
  755. ->field('rtrim(c.入仓类型) as 入仓类型, rtrim(c.仓库编号) as 仓库编号, rtrim(c.仓库名称) as 仓库名称,
  756. c.jjcp_gdbh, rtrim(c.jjcp_cpdh) as jjcp_cpdh, rtrim(c.jjcp_cpmc) as jjcp_cpmc,
  757. c.jjcp_sl, rtrim(c.jjcp_dw) as jjcp_dw, c.jjcp_sj,
  758. rtrim(c.jjcp_smb) as jjcp_smb,
  759. rtrim(c.jjcp_num) as jjcp_num, rtrim(c.订单编号) as 订单编号
  760. ')
  761. ->where('c.jjcp_sj','between time',[$req['start_time'], $req['end_time']])
  762. ->order('c.jjcp_sj asc')
  763. ->join('工单_基本资料 g','g.Gd_gdbh=c.jjcp_gdbh')
  764. ->where('g.Gd_cpdh=c.jjcp_cpdh')
  765. ->select();
  766. foreach ($rows as $key=>$value) {
  767. $subArray = [];
  768. foreach ($arr as $k=>$v){
  769. $subArray[$k] = $rows[$key][$k];
  770. }
  771. array_push($data,$subArray);
  772. }
  773. $this->success('成功',['file_name'=>$req['file_name'],'data'=>$data]);
  774. }
  775. /**
  776. * 月度B
  777. * @ApiMethod (POST)
  778. * @param string start_time
  779. * @param string end_time
  780. * @param string file_name
  781. * @param array fields
  782. */
  783. public function monthExportB(){
  784. if(!$this->request->isGet()){
  785. $this->error('请求方式错误');
  786. }
  787. $req = $this->request->param();
  788. $arr = [
  789. 'start_time', 'end_time', 'file_name'
  790. ];
  791. foreach ($arr as $key => $value){
  792. if (!isset($req[$value])){
  793. $this->error('参数错误',$value,$key+1);
  794. }
  795. }
  796. if (!(isset($req['fields']) && count($req['fields'])!=0)){
  797. $this->error('参数错误','fields',100);
  798. }
  799. $arr = [
  800. '入仓类型' =>['入仓类型'],
  801. '客户编号' =>['客户编号'],
  802. '客户名称' =>['客户名称'],
  803. '产品类别' =>['产品类别'],
  804. 'jjcp_gdbh' =>['工单编号'],
  805. '成品编码' =>['成品编码'],
  806. '成品名称' =>['成品名称'],
  807. 'jjcp_sls' =>['入仓数量'],
  808. '大箱产量' =>['大箱产量'],
  809. '产品单价' =>['万张单价'],
  810. '产值万元' =>['产值万元'],
  811. ];
  812. $data[0] = [];
  813. foreach ($req['fields'] as $k=>$v){
  814. if(array_key_exists($v,$arr)){
  815. array_push($data[0],$arr[$v][0]);
  816. }
  817. }
  818. $rows1 = db('成品入仓')->alias('c')
  819. ->field('rtrim(c.入仓类型) as 入仓类型, LEFT(c.成品编码,'.config('product_code_digit').') as 客户编号, rtrim(b.客户名称) as 客户名称,
  820. rtrim(b.产品类别) as 产品类别, c.jjcp_gdbh,
  821. rtrim(c.成品编码) as 成品编码, rtrim(c.成品名称) as 成品名称
  822. ')
  823. ->where('c.jjcp_sj','between time',[$req['start_time'], $req['end_time']])
  824. ->order('客户编号 asc,b.产品类别 desc,c.成品编码 asc,c.jjcp_gdbh asc')
  825. ->join('产品_基本资料 b','b.产品编号=c.jjcp_cpdh')
  826. ->group('c.jjcp_gdbh')
  827. ->select();
  828. $rows2 = db('成品入仓')
  829. ->field('rtrim(入仓类型) as 入仓类型, rtrim(成品编码) as 客户编号,
  830. rtrim(成品编码) as 客户名称, rtrim(成品编码) as 产品类别,
  831. jjcp_gdbh,rtrim(成品编码) as 成品编码, rtrim(成品名称) as 成品名称
  832. ')
  833. ->where('jjcp_sj','between time',[$req['start_time'], $req['end_time']])
  834. ->where('成品编码','')
  835. ->order('jjcp_gdbh asc')
  836. ->group('jjcp_gdbh')
  837. ->select();
  838. $rows = array_merge($rows2,$rows1);
  839. $num = db('工单_基本资料')->count();
  840. $arr1 = db('工单_基本资料')->limit($num)
  841. ->column('CONCAT(Gd_gdbh, "-", rtrim(Gd_cpdh)) AS gdcp, 产品单价 as 万张单价');
  842. $brr = array_column($rows,'jjcp_gdbh');
  843. $brr = db('成品入仓')
  844. ->where('jjcp_gdbh', 'in', $brr)
  845. ->group('jjcp_gdbh')
  846. ->column('jjcp_gdbh, sum(jjcp_sl)');
  847. foreach ($rows as $key=>$value) {
  848. $subArray = [];
  849. foreach ($arr as $k=>$v){
  850. if($k=='jjcp_sls'){
  851. $subArray[$k] = $brr[$value['jjcp_gdbh']];
  852. }else if ($k=='大箱产量'){
  853. if($value['产品类别']=='条盒'){
  854. $subArray[$k] = $brr[$value['jjcp_gdbh']]/250;
  855. }else if($value['产品类别']=='小盒'){
  856. $subArray[$k] = $brr[$value['jjcp_gdbh']]/2500;
  857. }else{
  858. $subArray[$k] = 0;
  859. }
  860. }else if ($k=='产品单价'){
  861. if($value['产品类别']!=''){
  862. if (array_key_exists($value['jjcp_gdbh'].'-'.$value['成品编码'],$arr1)){
  863. $subArray[$k] = floatval(number_format($arr1[$value['jjcp_gdbh'].'-'.$value['成品编码']],4));
  864. }
  865. }else{
  866. $subArray[$k] = 0;
  867. }
  868. }else if ($k=='产值万元'){
  869. if($value['产品类别']!=''){
  870. if (array_key_exists($value['jjcp_gdbh'].'-'.$value['成品编码'],$arr1)){
  871. $subArray[$k] = floatval(number_format($arr1[$value['jjcp_gdbh'].'-'.$value['成品编码']]*$brr[$value['jjcp_gdbh']]/100000000,2));
  872. }
  873. }else{
  874. $subArray[$k] = 0;
  875. }
  876. }else{
  877. $subArray[$k] = $rows[$key][$k];
  878. }
  879. }
  880. array_push($data,$subArray);
  881. }
  882. $this->success('成功',['file_name'=>$req['file_name'],'data'=>$data]);
  883. }
  884. /**
  885. * 各日统计列表
  886. * @ApiMethod (GET)
  887. * @param string $date 时间
  888. * @param string $sys_id 用户
  889. */
  890. public function getDayList()
  891. {
  892. //get请求
  893. if(!$this->request->isGet()){
  894. $this->error('请求方式错误');
  895. }
  896. $req = $this->request->param();
  897. $page = 1;
  898. $limit = 15;
  899. if (isset($req['page']) && !empty($req['page'])) $page = $req['page'];
  900. if (isset($req['limit']) && !empty($req['limit'])) $limit = $req['limit'];
  901. $where = [
  902. 'c.jjcp_smb'=>['<>',''],
  903. ];
  904. if (isset($req['key_word']) && !empty($req['key_word'])){
  905. $where['c.jjcp_sj | c.jjcp_gdbh | c.jjcp_cpmc | c.jjcp_cpdh | g.实际投料'] = ['like','%'.$req['key_word'].'%'];
  906. }
  907. //客户编号
  908. $rows = db('成品入仓')->alias('c')
  909. ->field('LEFT(c.jjcp_sj, 10) as jjcp_sj, c.jjcp_gdbh, rtrim(c.jjcp_cpmc) as jjcp_cpmc,
  910. rtrim(c.jjcp_cpdh) as jjcp_cpdh, g.实际投料')
  911. ->where($where)
  912. ->where('g.Gd_cpdh=c.jjcp_cpdh')
  913. ->join('工单_基本资料 g','g.Gd_gdbh=c.jjcp_gdbh')
  914. ->order('c.jjcp_sj desc')
  915. ->group('c.jjcp_gdbh, c.jjcp_cpdh')
  916. ->page($page,$limit)
  917. ->select();
  918. $total = db('成品入仓')->alias('c')
  919. ->where($where)
  920. ->where('g.Gd_cpdh=c.jjcp_cpdh')
  921. ->join('工单_基本资料 g','g.Gd_gdbh=c.jjcp_gdbh')
  922. ->group('c.jjcp_gdbh, c.jjcp_cpdh')
  923. ->count();
  924. $brr = db('成品入仓')
  925. ->group('jjcp_gdbh,jjcp_cpdh')
  926. ->column('CONCAT(jjcp_gdbh, "-", rtrim(jjcp_cpdh)), sum(jjcp_sl)');
  927. foreach ($rows as $key=>$value) {
  928. $rows[$key]['实际投料'] = (string)floatval($value['实际投料']*10000);
  929. if(array_key_exists($value['jjcp_gdbh'].'-'.$value['jjcp_cpdh'],$brr)){
  930. $rows[$key]['入仓数量'] = $brr[$value['jjcp_gdbh'].'-'.$value['jjcp_cpdh']];
  931. if($rows[$key]['实际投料']!=0){
  932. $rows[$key]['投入产出率'] = number_format($rows[$key]['入仓数量']/$rows[$key]['实际投料']*100,2);
  933. }else{
  934. $rows[$key]['投入产出率'] = '';
  935. }
  936. }else{
  937. $rows[$key]['入仓数量'] = '';
  938. $rows[$key]['投入产出率'] = '';
  939. }
  940. }
  941. $data = [
  942. 'total' => $total,
  943. 'rows' => $rows,
  944. ];
  945. $this->success('成功',$data);
  946. }
  947. /**
  948. * 各日统计列表excel导出
  949. * @ApiMethod (POST)
  950. * @param string file_name
  951. * @param array fields
  952. */
  953. public function dayExport()
  954. {
  955. //get请求
  956. if(!$this->request->isPost()){
  957. $this->error('请求方式错误');
  958. }
  959. $req = $this->request->param();
  960. if (!isset($req['file_name'])){
  961. $this->error('参数错误','file_name');
  962. }
  963. if (!(isset($req['fields']) && count($req['fields'])!=0)){
  964. $this->error('参数错误','fields',100);
  965. }
  966. if (isset($req['key_word']) && !empty($req['key_word'])){
  967. $where['c.jjcp_sj | c.jjcp_gdbh | c.jjcp_cpmc | c.jjcp_cpdh | g.实际投料'] = ['like','%'.$req['key_word'].'%'];
  968. }
  969. $arr = [
  970. 'jjcp_gdbh' =>['jjcp_gdbh'],
  971. 'jjcp_yjno' =>['jjcp_yjno'],
  972. 'jjcp_cpmc' =>['印件名称'],
  973. '成品编码' =>['成品编码'],
  974. '成品名称' =>['成品名称'],
  975. '实际投料' =>['实际投料'],
  976. 'jjcp_sls' =>['入仓数量'],
  977. 'jjcp_sj' =>['完工日期'],
  978. ];
  979. $data[0] = [];
  980. foreach ($req['fields'] as $k=>$v){
  981. if(array_key_exists($v,$arr)){
  982. array_push($data[0],$arr[$v][0]);
  983. }
  984. }
  985. $where = [
  986. 'c.jjcp_smb'=>['<>','']
  987. ];
  988. //客户编号
  989. $rows = db('成品入仓')->alias('c')
  990. ->field('c.jjcp_gdbh, c.jjcp_yjno, rtrim(c.jjcp_cpmc) as jjcp_cpmc, rtrim(c.jjcp_cpdh) as jjcp_cpdh,
  991. rtrim(c.成品编码) as 成品编码, rtrim(c.成品名称) as 成品名称, sum(jjcp_sl) as jjcp_sls,
  992. g.实际投料, LEFT(c.jjcp_sj, 10) as jjcp_sj
  993. ')
  994. ->where($where)
  995. ->where('g.Gd_cpdh=c.jjcp_cpdh')
  996. ->join('工单_基本资料 g','g.Gd_gdbh=c.jjcp_gdbh')
  997. ->order('c.jjcp_sj desc')
  998. ->group('c.jjcp_gdbh, c.jjcp_cpdh')
  999. ->select();
  1000. foreach ($rows as $key=>$value) {
  1001. $subArray = [];
  1002. foreach ($arr as $k=>$v){
  1003. if($k=='实际投料'){
  1004. $subArray[$k] = (string)floatval($value['实际投料']*10000);
  1005. }else{
  1006. $subArray[$k] = $rows[$key][$k];
  1007. }
  1008. }
  1009. array_push($data,$subArray);
  1010. }
  1011. $this->success('成功',['file_name'=>$req['file_name'],'data'=>$data]);
  1012. }
  1013. /**
  1014. * 入仓末板数据统计列表
  1015. * @ApiMethod (GET)
  1016. * @param string $date 时间
  1017. * @param string $sys_id 用户
  1018. */
  1019. public function mbList()
  1020. {
  1021. //get请求
  1022. if(!$this->request->isGet()){
  1023. $this->error('请求方式错误');
  1024. }
  1025. $req = $this->request->param();
  1026. // $page = 1;
  1027. // $limit = 15;
  1028. // if (isset($req['page']) && !empty($req['page'])) $page = $req['page'];
  1029. // if (isset($req['limit']) && !empty($req['limit'])) $limit = $req['limit'];
  1030. $date = date('Y-m-d',strtotime('-3 month'));
  1031. $arr1 = db('db_包装产量预报')
  1032. ->field('sczl_gdbh1, sczl_gdbh2, sczl_gdbh3, sczl_gdbh4, sczl_gdbh5, sczl_gdbh6,
  1033. cast(substr(sczl_yjGx1,1,2) as signed) as sczl_yjGx1, cast(substr(sczl_yjGx2,1,2) as signed) as sczl_yjGx2,
  1034. cast(substr(sczl_yjGx3,1,2) as signed) as sczl_yjGx3, cast(substr(sczl_yjGx4,1,2) as signed) as sczl_yjGx4,
  1035. cast(substr(sczl_yjGx5,1,2) as signed) as sczl_yjGx5, cast(substr(sczl_yjGx6,1,2) as signed) as sczl_yjGx6,
  1036. (sczl_cl1*sczl_PgCl1+sczl_clAdd1) as sczl_cls1,
  1037. (sczl_cl2*sczl_PgCl2+sczl_clAdd2) as sczl_cls2,
  1038. (sczl_cl3*sczl_PgCl3+sczl_clAdd3) as sczl_cls3,
  1039. (sczl_cl4*sczl_PgCl4+sczl_clAdd4) as sczl_cls4,
  1040. (sczl_cl5*sczl_PgCl5+sczl_clAdd5) as sczl_cls5,
  1041. (sczl_cl6*sczl_PgCl6+sczl_clAdd6) as sczl_cls6
  1042. ')
  1043. ->where('sczl_rq','>=',$date)
  1044. ->select();
  1045. $data1 = [];
  1046. foreach ($arr1 as $k=>$v){
  1047. for ($i=1;$i<=6;$i++){
  1048. if (!empty($v['sczl_gdbh'.$i])){
  1049. $v['sczl_gdbh'.$i] = strtoupper($v['sczl_gdbh'.$i]);
  1050. if(array_key_exists($v['sczl_gdbh'.$i].'-'.$v['sczl_yjGx'.$i],$data1)){
  1051. $data1[$v['sczl_gdbh'.$i].'-'.$v['sczl_yjGx'.$i]] += $v['sczl_cls'.$i];
  1052. }else{
  1053. $data1[$v['sczl_gdbh'.$i].'-'.$v['sczl_yjGx'.$i]] = $v['sczl_cls'.$i];
  1054. }
  1055. }
  1056. }
  1057. }
  1058. krsort($data1);
  1059. //成品入仓中jjcp_smb不为空的工单
  1060. $rows2 = db('成品入仓')
  1061. ->where('jjcp_sj','>=',$date)
  1062. ->group('jjcp_gdbh, jjcp_cpdh')
  1063. ->column("CONCAT( jjcp_gdbh, '-', jjcp_yjno) AS gdyj, max(jjcp_smb) as jjcp_smb,
  1064. sum(jjcp_sl) as jjcp_sls, left(max(jjcp_sj),10) as jjcp_sj,UniqId");
  1065. $data = [];
  1066. foreach($data1 as $key=>$value){
  1067. $subArray = [];
  1068. $gdbh = strtoupper(explode('-',$key)[0]);
  1069. $yjno = explode('-',$key)[1];
  1070. $res = db('工单_印件资料')
  1071. ->where('Yj_Gdbh',$gdbh)
  1072. ->where('yj_Yjno',$yjno)
  1073. ->field('Yj_Gdbh, yj_Yjno, yj_yjmc, yj_成品数量, (yj_平张投料 * yj_ks * yj_ls) as 投料')
  1074. ->find();
  1075. if(!empty($res)){
  1076. $subArray['Yj_Gdbh'] = $res['Yj_Gdbh'];
  1077. $subArray['yj_Yjno'] = $res['yj_Yjno'];
  1078. $subArray['yj_yjmc'] = trim($res['yj_yjmc']);
  1079. $subArray['yj_成品数量'] = (string)floatval($res['yj_成品数量']*10000);
  1080. $subArray['yj_实际投料'] = (string)floatval($res['投料']);
  1081. }else{
  1082. $subArray['Yj_Gdbh'] = '';
  1083. $subArray['yj_Yjno'] = '';
  1084. $subArray['yj_yjmc'] = '';
  1085. $subArray['yj_成品数量'] = '';
  1086. $subArray['yj_实际投料'] = '';
  1087. }
  1088. if(array_key_exists($key,$rows2)){
  1089. $subArray['jjcp_smb'] = $rows2[$key]['jjcp_smb'];
  1090. $subArray['jjcp_sls'] = $rows2[$key]['jjcp_sls'];
  1091. $subArray['jjcp_sj'] = $rows2[$key]['jjcp_sj'];
  1092. }else{
  1093. $subArray['jjcp_smb'] = '';
  1094. $subArray['jjcp_sls'] = '';
  1095. $subArray['jjcp_sj'] = '';
  1096. }
  1097. $subArray['sczl_cls'] = (string)floatval($value);
  1098. array_push($data,$subArray);
  1099. }
  1100. $data = [
  1101. 'total' => count($data),
  1102. 'rows' => $data,
  1103. ];
  1104. $this->success('成功',$data);
  1105. }
  1106. /**
  1107. * 入仓末板数据统计列表excel导出
  1108. * @ApiMethod (POST)
  1109. * @param string file_name
  1110. * @param array fields
  1111. */
  1112. public function mbExport()
  1113. {
  1114. //get请求
  1115. if(!$this->request->isPost()){
  1116. $this->error('请求方式错误');
  1117. }
  1118. $req = $this->request->param();
  1119. if (!isset($req['file_name'])){
  1120. $this->error('参数错误','file_name');
  1121. }
  1122. if (!(isset($req['fields']) && count($req['fields'])!=0)){
  1123. $this->error('参数错误','fields',100);
  1124. }
  1125. $arr = [
  1126. 'Yj_Gdbh' =>['工单编号'],
  1127. 'yj_Yjno' =>['印件号'],
  1128. 'yj_yjmc' =>['印件名称'],
  1129. 'yj_成品数量' =>['计划交货数量'],
  1130. 'yj_实际投料' =>['印件投料'],
  1131. 'sczl_cls' =>['包装产量'],
  1132. 'jjcp_sj' =>['最近入仓时间'],
  1133. 'jjcp_sls' =>['入仓产量'],
  1134. 'status' =>['完工'],
  1135. '缺数' =>['缺数'],
  1136. ];
  1137. $data[0] = [];
  1138. foreach ($req['fields'] as $k=>$v){
  1139. if(array_key_exists($v,$arr)){
  1140. array_push($data[0],$arr[$v][0]);
  1141. }
  1142. }
  1143. $date = date('Y-m-d',strtotime('-3 month'));
  1144. $arr1 = db('db_包装产量预报')
  1145. ->field('sczl_gdbh1, sczl_gdbh2, sczl_gdbh3, sczl_gdbh4, sczl_gdbh5, sczl_gdbh6,
  1146. cast(substr(sczl_yjGx1,1,2) as signed) as sczl_yjGx1, cast(substr(sczl_yjGx2,1,2) as signed) as sczl_yjGx2,
  1147. cast(substr(sczl_yjGx3,1,2) as signed) as sczl_yjGx3, cast(substr(sczl_yjGx4,1,2) as signed) as sczl_yjGx4,
  1148. cast(substr(sczl_yjGx5,1,2) as signed) as sczl_yjGx5, cast(substr(sczl_yjGx6,1,2) as signed) as sczl_yjGx6,
  1149. (sczl_cl1*sczl_PgCl1+sczl_clAdd1) as sczl_cls1,
  1150. (sczl_cl2*sczl_PgCl2+sczl_clAdd2) as sczl_cls2,
  1151. (sczl_cl3*sczl_PgCl3+sczl_clAdd3) as sczl_cls3,
  1152. (sczl_cl4*sczl_PgCl4+sczl_clAdd4) as sczl_cls4,
  1153. (sczl_cl5*sczl_PgCl5+sczl_clAdd5) as sczl_cls5,
  1154. (sczl_cl6*sczl_PgCl6+sczl_clAdd6) as sczl_cls6
  1155. ')
  1156. ->where('sczl_rq','>=',$date)
  1157. ->select();
  1158. $data1 = [];
  1159. foreach ($arr1 as $k=>$v){
  1160. for ($i=1;$i<=6;$i++){
  1161. if(array_key_exists($v['sczl_gdbh'.$i].'-'.$v['sczl_yjGx'.$i],$data1)){
  1162. $data1[$v['sczl_gdbh'.$i].'-'.$v['sczl_yjGx'.$i]] += $v['sczl_cls'.$i];
  1163. }else{
  1164. $data1[$v['sczl_gdbh'.$i].'-'.$v['sczl_yjGx'.$i]] = $v['sczl_cls'.$i];
  1165. }
  1166. }
  1167. }
  1168. krsort($data1);
  1169. //成品入仓中jjcp_smb不为空的工单
  1170. $rows1 = db('成品入仓')
  1171. ->where('jjcp_smb','<>','')
  1172. ->order('jjcp_gdbh desc')
  1173. ->column("CONCAT(jjcp_gdbh, '-', jjcp_yjno) AS gdyj, UniqId");
  1174. $rows2 = db('成品入仓')
  1175. ->where('jjcp_sj','>=',$date)
  1176. ->group('jjcp_gdbh, jjcp_cpdh')
  1177. ->column("CONCAT( jjcp_gdbh, '-', jjcp_yjno) AS gdyj, jjcp_smb,
  1178. sum(jjcp_sl) as jjcp_sls, left(max(jjcp_sj),10) as jjcp_sj");
  1179. $rows3 = db('工单_印件资料')->cache(true,86400)
  1180. ->group('Yj_Gdbh, yj_Yjno')
  1181. ->column("CONCAT( Yj_Gdbh, '-', yj_Yjno) AS gdyj,
  1182. Yj_Gdbh, yj_Yjno, yj_yjmc, yj_成品数量, yj_实际投料");
  1183. foreach($data1 as $key=>$value){
  1184. if(!array_key_exists($key,$rows1)){
  1185. $subArray = [];
  1186. foreach ($arr as $k=>$v){
  1187. if($k=='yj_成品数量' || $k=='yj_实际投料'){
  1188. if(array_key_exists($key,$rows3)){
  1189. $subArray[$k] = (string)floatval($rows3[$key][$k]*10000);
  1190. }else{
  1191. $subArray[$k] = '';
  1192. }
  1193. }else if($k=='Yj_Gdbh' || $k=='yj_Yjno'){
  1194. if(array_key_exists($key,$rows3)){
  1195. $subArray[$k] = $rows3[$key][$k];
  1196. }else{
  1197. $subArray[$k] = '';
  1198. }
  1199. }else if($k=='yj_yjmc'){
  1200. if(array_key_exists($key,$rows3)){
  1201. $subArray[$k] = trim($rows3[$key][$k]);
  1202. }else{
  1203. $subArray[$k] = '';
  1204. }
  1205. }else if($k=='sczl_cls'){
  1206. $subArray[$k] = (string)floatval($value);
  1207. }else if($k=='status'){
  1208. if(array_key_exists($key,$rows2) && $rows2[$key]['jjcp_smb'] !=''){
  1209. $subArray[$k] = '完工';
  1210. }else{
  1211. $subArray[$k] = '';
  1212. }
  1213. }else if($k=='缺数'){
  1214. if(array_key_exists($key,$rows2) && $rows2[$key]['jjcp_smb'] !=''){
  1215. $subArray[$k]['缺数'] = (int)$rows3[$key]['yj_成品数量']-(int)$value;
  1216. }else{
  1217. $subArray[$k] = '';
  1218. }
  1219. }else{
  1220. if(array_key_exists($key,$rows2)){
  1221. $subArray[$k] = $rows2[$key][$k];
  1222. $subArray[$k] = $rows2[$key][$k];
  1223. $subArray[$k] = $rows2[$key][$k];
  1224. }else{
  1225. $subArray[$k] = '';
  1226. $subArray[$k] = '';
  1227. $subArray[$k] = '';
  1228. }
  1229. }
  1230. }
  1231. array_push($data,$subArray);
  1232. }
  1233. }
  1234. $this->success('成功',['file_name'=>$req['file_name'],'data'=>$data]);
  1235. }
  1236. /**
  1237. * 构建新增入仓数据
  1238. * @param array $req
  1239. * @return array
  1240. */
  1241. protected function buildAddData(array $req)
  1242. {
  1243. $fields = [
  1244. 'Sys_id',
  1245. '入仓类型', '仓库编号', '仓库名称',
  1246. 'jjcp_num',
  1247. 'jjcp_gdbh', 'jjcp_yjno', '订单编号',
  1248. 'jjcp_cpdh', 'jjcp_cpmc',
  1249. '成品编码', '成品名称',
  1250. '客户料号',
  1251. 'jjcp_smb', 'jjcp_sj',
  1252. 'jjcp_sl', 'jjcp_dw',
  1253. '机型备注',
  1254. 'jjcp_desc',
  1255. ];
  1256. $data = [];
  1257. foreach ($fields as $index => $field) {
  1258. if (!isset($req[$field])) {
  1259. $this->error('参数错误', $field, $index + 1);
  1260. }
  1261. $data[$field] = $req[$field];
  1262. }
  1263. $data['Sys_rq'] = date('Y-m-d H:i:s');
  1264. return $data;
  1265. }
  1266. /**
  1267. * 校验印件产品名称
  1268. * @param string $gdbh
  1269. * @param string $yjno
  1270. * @param string $cpmc
  1271. */
  1272. protected function validateProductName($gdbh, $yjno, $cpmc)
  1273. {
  1274. $name = db('工单_印件资料')
  1275. ->where('Yj_Gdbh', $gdbh)
  1276. ->where('yj_Yjno', $yjno)
  1277. ->value('yj_yjmc');
  1278. if ($name !== $cpmc) {
  1279. $this->error('产品名称错误');
  1280. }
  1281. }
  1282. /**
  1283. * 查询工单印件是否已存在末版记录
  1284. * @param string $gdbh
  1285. * @param string $yjno
  1286. * @return array|null
  1287. */
  1288. protected function findExistingEndBoard($gdbh, $yjno)
  1289. {
  1290. return db('成品入仓')
  1291. ->where('jjcp_gdbh', $gdbh)
  1292. ->where('jjcp_yjno', $yjno)
  1293. ->where('jjcp_smb', '<>', '')
  1294. ->find();
  1295. }
  1296. /**
  1297. * 校验工单印件是否已完全领料
  1298. * @param string $gdbh
  1299. * @param string $yjno
  1300. */
  1301. protected function validateMaterialReceived($gdbh, $yjno)
  1302. {
  1303. $materialRecord = db('工单_印件资料')
  1304. ->alias('a')
  1305. ->join('物料_收发记录 b', 'a.Yj_Gdbh=b.st_gdbh and a.yj_cpdh=b.cpdh')
  1306. ->where('a.Yj_Gdbh', $gdbh)
  1307. ->where('a.yj_Yjno', $yjno)
  1308. ->group('b.st_wlbh')
  1309. ->order('b.st_wlbh asc')
  1310. ->column('b.st_wlbh');
  1311. $bomRecord = db('工单_印件资料')
  1312. ->alias('a')
  1313. ->join('工单_bom资料 b', 'a.Yj_Gdbh=b.BOM_工单编号 and a.yj_Yjno=b.BOM_工单行号 and a.yj_cpdh=b.BOM_产品编号')
  1314. ->where('a.Yj_Gdbh', $gdbh)
  1315. ->where('a.yj_Yjno', $yjno)
  1316. ->where('b.BOM_实际用量', '<>', 0)
  1317. ->group('b.BOM_物料编码')
  1318. ->order('b.BOM_物料编码 asc')
  1319. ->column('b.BOM_物料编码');
  1320. foreach ($bomRecord as $materialCode) {
  1321. if (!in_array($materialCode, $materialRecord)) {
  1322. $this->error('工单' . $gdbh . '印件' . $yjno . '物料' . $materialCode . '未领料');
  1323. }
  1324. }
  1325. }
  1326. /**
  1327. * 生成成品入仓 UniqId
  1328. * @return int
  1329. */
  1330. protected function generateFinishStockUniqId()
  1331. {
  1332. $uniqId = db('成品入仓')->max('UniqId');
  1333. return $uniqId < 10000000 ? 10000000 : $uniqId + 1;
  1334. }
  1335. /**
  1336. * 保存成品入仓数据到 MySQL 和 MongoDB
  1337. * @param array $data
  1338. * @param mixed $mongodb
  1339. */
  1340. protected function saveFinishStock(array $data, $mongodb)
  1341. {
  1342. db()->startTrans();
  1343. try {
  1344. $sql = db('成品入仓')->fetchSql(true)->insert($data);
  1345. $inserted = db()->query($sql);
  1346. if ($inserted === false) {
  1347. throw new \Exception('MySQL 保存失败');
  1348. }
  1349. $mongodb->name('finished_products')->insert($data);
  1350. db()->commit();
  1351. } catch (\Exception $e) {
  1352. db()->rollback();
  1353. $this->error($e->getMessage());
  1354. }
  1355. }
  1356. /**
  1357. * 判断是否为末版
  1358. * @param string $smb
  1359. * @return bool
  1360. */
  1361. protected function isEndBoard($smb)
  1362. {
  1363. return strpos((string)$smb, '末') !== false;
  1364. }
  1365. /**
  1366. * 是否需要同步成品入仓中间表
  1367. * 规则:
  1368. * 1. 工单编号以 Y 开头,且 >= Y2608001
  1369. * 2. 工单_基本资料.接单日期 >= 2026-08-01
  1370. * 3. 中间库 U8_06工单资料 中存在该工单编号
  1371. * @param string $gdbh
  1372. * @return bool
  1373. */
  1374. protected function shouldSyncFinishStockMiddle($gdbh)
  1375. {
  1376. $gdbh = strtoupper(trim((string)$gdbh));
  1377. if ($gdbh === '' || $gdbh[0] !== 'Y') {
  1378. return false;
  1379. }
  1380. if ((int)substr($gdbh, 1) < 2608001) {
  1381. return false;
  1382. }
  1383. $orderDate = db('工单_基本资料')->where('Gd_gdbh', $gdbh)->value('接单日期');
  1384. if (empty($orderDate)) {
  1385. return false;
  1386. }
  1387. if (strtotime(trim((string)$orderDate)) < strtotime('2026-08-01')) {
  1388. return false;
  1389. }
  1390. // 与 syncFinishStockMiddle 一致:测试库 database.db8;正式库可改为 database.db3
  1391. $middleDb = Db::connect(config('database.db8'));
  1392. $exists = $middleDb->name('U8_06工单资料')->where('工单编号', $gdbh)->count();
  1393. return $exists > 0;
  1394. }
  1395. /**
  1396. * 删除时是否需要回冲同步
  1397. * 1. 该 UniqId 已有成功同步记录/中间表数据
  1398. * 2. 或满足新增同步条件
  1399. * 3. 或工单为 Y 且 >= Y2608001(删除回冲放宽,避免 U8_06 查不到导致静默跳过)
  1400. * @param string $gdbh
  1401. * @param int $uniqId
  1402. * @return bool
  1403. */
  1404. protected function shouldSyncFinishStockOnDelete($gdbh, $uniqId)
  1405. {
  1406. if ($this->hasFinishStockMiddleSynced($uniqId)) {
  1407. return true;
  1408. }
  1409. if ($this->shouldSyncFinishStockMiddle($gdbh)) {
  1410. return true;
  1411. }
  1412. $gdbh = strtoupper(trim((string)$gdbh));
  1413. if ($gdbh === '' || $gdbh[0] !== 'Y') {
  1414. return false;
  1415. }
  1416. return (int)substr($gdbh, 1) >= 2608001;
  1417. }
  1418. /**
  1419. * 该成品入仓是否已成功同步过(日志或中间表)
  1420. * @param int $uniqId
  1421. * @return bool
  1422. */
  1423. protected function hasFinishStockMiddleSynced($uniqId)
  1424. {
  1425. $uniqId = (int)$uniqId;
  1426. $logCount = db('工单_成品入仓同步操作日志')
  1427. ->where('UniqId', $uniqId)
  1428. ->where('status', '同步成功')
  1429. ->count();
  1430. if ($logCount > 0) {
  1431. return true;
  1432. }
  1433. try {
  1434. $middleDb = Db::connect(config('database.db8'));
  1435. return $middleDb->name('U8_成品入仓')->where('UniqId', $uniqId)->count() > 0;
  1436. } catch (\Exception $e) {
  1437. return false;
  1438. }
  1439. }
  1440. /**
  1441. * 末版入仓后进入超节损
  1442. * @param string $gdbh
  1443. * @param string $yjno
  1444. */
  1445. protected function handleEndBoardSuperLoss($gdbh, $yjno)
  1446. {
  1447. $orderLoss = new OrderSuperLoss();
  1448. $result = [];
  1449. try {
  1450. $result = $orderLoss->OneOrderSuperLoss($gdbh, $yjno);
  1451. } catch (DataNotFoundException $e) {
  1452. } catch (ModelNotFoundException $e) {
  1453. } catch (DbException $e) {
  1454. }
  1455. if (empty($result)) {
  1456. return;
  1457. }
  1458. db('工单_质量考核汇总')
  1459. ->where('Gy0_gdbh', $gdbh)
  1460. ->where('印件及工序', $yjno)
  1461. ->delete();
  1462. $sql = db('工单_质量考核汇总')->fetchSql(true)->insertAll($result);
  1463. $inserted = db()->query($sql);
  1464. if ($inserted === false) {
  1465. $this->error('超节损数据保存失败');
  1466. }
  1467. }
  1468. }