FinishedProductWarehousing.php 56 KB

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