FinishedProductWarehousing.php 61 KB

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