WorkOrderVerification.php 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use \think\Request;
  5. use \think\Db;
  6. use \think\cache;
  7. /**
  8. * 工单核验单维护接口
  9. */
  10. class WorkOrderVerification extends Api
  11. {
  12. protected $noNeedLogin = ['*'];
  13. protected $noNeedRight = ['*'];
  14. /**
  15. * 首页
  16. *
  17. */
  18. public function index()
  19. {
  20. $this->success('请求成功');
  21. }
  22. /**
  23. * 获取报工历史几记录
  24. *
  25. */
  26. public function getTab(){
  27. //get请求
  28. if(!$this->request->isGet()){
  29. $this->error('请求方式错误');
  30. }
  31. $param = $this->request->param();
  32. $where = [
  33. '子订单编号' => $param['order_id'],
  34. 'sczl_jtbh' => $param['sczl_jtbh']
  35. ];
  36. //通过当前子订单编号和机台查看历史记录数据
  37. $table_list = \db('设备_产量计酬')->alias('c')
  38. ->field('c.订单编号, c.子订单编号, c.款号, c.工序编号, c.工序名称, c.尺码, c.数量, c.sczl_jtbh, c.尾包,c.UniqId,c.ci_num,c.s_num,
  39. c.sys_rq, c.serial, y.zdtotal, y.sctotal, j.款式, y.颜色')
  40. ->join('工单_印件资料 y', 'c.子订单编号 = y.子订单编号', 'left')
  41. ->join('工单_基本资料 j', 'c.订单编号 = j.订单编号', 'left')
  42. ->where('c.子订单编号', $param['order_id'])
  43. ->where('c.sczl_jtbh', $param['sczl_jtbh'])
  44. ->whereNull('c.mod_rq') // 查询未删除数据
  45. ->where(function($query) {
  46. $query->whereNotNull('y.ck_rq')->where('y.ck_rq', '<>', '');
  47. }) // 查询出库数据
  48. ->order('c.UniqId', 'desc')
  49. ->distinct('c.子订单编号')
  50. ->select();
  51. $output = [];
  52. foreach ($table_list as $record) {
  53. $output[] = [
  54. 'serial' => $record['serial'],
  55. 'sys_rq' => $record['sys_rq'],
  56. '订单编号' => $record['订单编号'],
  57. '子订单编号' => $record['子订单编号'],
  58. '颜色' => $record['颜色'],
  59. '尺码' => $record['尺码'],
  60. '数量' => $record['数量'],
  61. '上报数量' => $record['s_num'],
  62. '尾包' => $record['尾包'],
  63. '组别' => $record['sczl_jtbh'],
  64. ];
  65. }
  66. $this->success('请求成功', [
  67. 'records' => $output
  68. ]);
  69. }
  70. /**
  71. * 记录报工日志历史记录
  72. */
  73. public function getTabByGdbh()
  74. {
  75. if (Request::instance()->isPost() === false){
  76. $this->error('请求错误');
  77. }
  78. $param = Request::instance()->post();
  79. if (empty($param)){
  80. $this->error('参数错误');
  81. }
  82. $rows = db()->table('工单_基本资料')
  83. ->where('订单编号',$param['订单编号'])
  84. ->find();
  85. $param['客户编号'] = $rows['客户编号'];
  86. $sql= \db('设备_报工记录')->fetchSql(true)->insert($param);
  87. $res = \db()->query($sql);
  88. if ($res !== false){
  89. $this->success('成功');
  90. }else{
  91. $this->error('失败');
  92. }
  93. }
  94. /**
  95. * 获取报工日志
  96. * @ApiMethod (GET)
  97. */
  98. public function getList()
  99. {
  100. // 检查是否为 GET 请求
  101. if(!$this->request->isGet()){
  102. $this->error('请求方式错误');
  103. }
  104. // 获取请求参数
  105. $param = $this->request->param();
  106. // 判断订单编号是否包含 '-'
  107. if (strpos($param['order'], '-') !== false) {
  108. // 如果订单包含 '-',则按子订单编号精确匹配
  109. $where['子订单编号'] = $param['order'];
  110. } else {
  111. // 如果订单不包含 '-',则按订单编号进行模糊匹配
  112. $where['订单编号'] = ['like', '%' . $param['order'] . '%'];
  113. }
  114. // 过滤条件中的 code
  115. $where['code'] = $param['code'];
  116. // 查询数据库
  117. $rows = db()->table('设备_报工记录')
  118. ->where($where)
  119. ->order('子订单编号 desc')
  120. ->select();
  121. // 提取 cm1 到 cm10 的数据
  122. $headers = [];
  123. if (!empty($rows)) {
  124. foreach ($rows as $row) {
  125. for ($i = 1; $i <= 10; $i++) {
  126. $cmField = 'cm' . $i;
  127. if (isset($row[$cmField]) && $row[$cmField] !== '') {
  128. // 仅在字段不为空时添加到 headers
  129. $headers[] = $row[$cmField];
  130. }
  131. }
  132. }
  133. // 去重,避免重复值
  134. $headers = array_unique($headers);
  135. // 按照数值升序排序
  136. sort($headers);
  137. }
  138. // 构建返回的数据结构
  139. $data = [
  140. 'table' => $rows,
  141. 'headers' => $headers,
  142. 'total' => count($rows),
  143. ];
  144. // 返回成功响应
  145. $this->success('成功', $data);
  146. }
  147. /**
  148. * 获取工单核验单信息
  149. * @ApiMethod (GET)
  150. * @param string $UniqId UniqId
  151. */
  152. public function getInfo()
  153. {
  154. //get请求
  155. if(!$this->request->isGet()){
  156. $this->error('请求方式错误');
  157. }
  158. $req = $this->request->param();
  159. if (isset($req['UniqId']) && !empty($req['UniqId'])){
  160. $UniqId = $req['UniqId'];
  161. }else{
  162. $this->error('参数错误');
  163. }
  164. $rows = db()->table('db_qczl')->alias('d')
  165. ->field('d.*, ')
  166. ->join('工单_基本资料 g', 'd.')
  167. ->where('d.UniqId',$UniqId)
  168. ->select();
  169. $this->success('成功',$rows);
  170. }
  171. /**
  172. * 生产产量进度月报表(Excel)
  173. * @params string UniqId
  174. */
  175. public function getOneWorkOrder() {
  176. // 确认请求为 GET 请求
  177. if (!$this->request->isGet()) {
  178. $this->error('请求方式错误');
  179. }
  180. // 获取请求参数
  181. $param = $this->request->param();
  182. // 获取当前月份和上个月的月份
  183. $currentMonth = date('Y-m', strtotime($param['riqi'])); // 当前月份
  184. $previousMonth = date('Y-m', strtotime('-1 month', strtotime($param['riqi']))); // 上个月
  185. // 查询当前月份的数据
  186. $where['上报时间'] = ['like', $currentMonth . '%'];
  187. $rows = db()->table('设备_报工记录')
  188. ->field('款式,款号,订单编号,组别,客户编号,code as 工序,上报数量,sctotal as 裁剪数,zdtotal as 制单数,scsl1,scsl2,scsl3,scsl4,scsl5,scsl6,scsl7,scsl8,scsl9,scsl10,上报时间')
  189. ->where($where)
  190. ->select();
  191. // 查询上个月的数据
  192. $wherePrevious['上报时间'] = ['like', $previousMonth . '%'];
  193. $rowsPrevious = db()->table('设备_报工记录')
  194. ->field('款式,款号,订单编号,组别,客户编号,sctotal as 裁剪数,zdtotal as 制单数,scsl1,scsl2,scsl3,scsl4,scsl5,scsl6,scsl7,scsl8,scsl9,scsl10,上报时间')
  195. ->where($wherePrevious)
  196. ->select();
  197. // 初始化结果数组
  198. $result = [];
  199. // 处理当前月份的数据
  200. foreach ($rows as $row) {
  201. // 使用“款式”、“款号”、“订单编号”、“组别”作为唯一标识
  202. $key = $row['款式'] . '_' . $row['款号'] . '_' . $row['订单编号'] . '_' . $row['组别'];
  203. // 如果结果集中不存在该条目,则初始化
  204. if (!isset($result[$key])) {
  205. $result[$key] = [
  206. '款式' => $row['款式'],
  207. '款号' => $row['款号'],
  208. '订单编号' => $row['订单编号'],
  209. '组别' => $row['组别'],
  210. '客户编号' => $row['客户编号'],
  211. '工序' => $row['工序'],
  212. '裁剪数' => $row['裁剪数'] ?? 0,
  213. '制单数' => $row['制单数'] ?? 0,
  214. '上报数量' => 0, // 初始化上报数量累计
  215. ];
  216. // 初始化每日数据
  217. for ($day = 1; $day <= 31; $day++) {
  218. $result[$key][$day . '号'] = 0;
  219. }
  220. }
  221. // 按天累计每日数据
  222. $day = (int)date('d', strtotime($row['上报时间']));
  223. // 检查 scsl1 到 scsl10 是否都是空
  224. $allScslEmpty = true;
  225. for ($i = 1; $i <= 10; $i++) {
  226. $keyScsl = 'scsl' . $i;
  227. if (!empty($row[$keyScsl])) {
  228. $allScslEmpty = false;
  229. break;
  230. }
  231. }
  232. // 如果 scsl1-scsl10 全部为空,则累加上报数量
  233. if ($allScslEmpty) {
  234. $result[$key][$day . '号'] += (int)$row['上报数量'];
  235. } else {
  236. // 如果有 scsl1-scsl10 的数据,则按原来的逻辑累加
  237. for ($i = 1; $i <= 10; $i++) {
  238. $keyScsl = 'scsl' . $i;
  239. $result[$key][$day . '号'] += !empty($row[$keyScsl]) ? (int)$row[$keyScsl] : 0;
  240. }
  241. }
  242. // 累计“上报数量”(非裁剪和制单)
  243. $result[$key]['上报数量'] += (int)$row['上报数量'];
  244. }
  245. // 计算本月累计数据
  246. foreach ($result as &$item) {
  247. $item['本月累计'] = 0; // 初始化本月累计
  248. $item['上个月累计'] = 0; // 初始化上个月累计
  249. // 累计本月每天的数据
  250. for ($day = 1; $day <= 31; $day++) {
  251. $item['本月累计'] += $item[$day . '号'];
  252. // 如果当天数据为 0,则显示为空字符串
  253. if ($item[$day . '号'] === 0) {
  254. $item[$day . '号'] = '';
  255. }
  256. }
  257. // 注意:上个月的每日统计数据不需要处理,仅保留本月的累计。
  258. }
  259. // 将结果转换为索引数组
  260. $result = array_values($result);
  261. // 返回结果
  262. $this->success('成功', $result);
  263. }
  264. // public function getOneWorkOrder() {
  265. // // 确认请求为 GET 请求
  266. // if (!$this->request->isGet()) {
  267. // $this->error('请求方式错误');
  268. // }
  269. //
  270. // // 获取请求参数
  271. // $param = $this->request->param();
  272. //
  273. // // 获取当前月份和上个月的月份
  274. // $currentMonth = date('Y-m', strtotime($param['riqi'])); // 当前月份
  275. // $previousMonth = date('Y-m', strtotime('-1 month', strtotime($param['riqi']))); // 上个月
  276. //
  277. // // 查询当前月份的数据
  278. // $where['上报时间'] = ['like', $currentMonth . '%'];
  279. // $rows = db()->table('设备_报工记录')
  280. // ->field('款式,款号,订单编号,组别,客户编号,code as 工序,上报数量,sctotal as 裁剪数,zdtotal as 制单数,scsl1,scsl2,scsl3,scsl4,scsl5,scsl6,scsl7,scsl8,scsl9,scsl10,上报时间')
  281. // ->where($where)
  282. // ->select();
  283. //
  284. // // 查询上个月的数据
  285. // $wherePrevious['上报时间'] = ['like', $previousMonth . '%'];
  286. // $rowsPrevious = db()->table('设备_报工记录')
  287. // ->field('款式,款号,订单编号,组别,客户编号,sctotal as 裁剪数,zdtotal as 制单数,scsl1,scsl2,scsl3,scsl4,scsl5,scsl6,scsl7,scsl8,scsl9,scsl10,上报时间')
  288. // ->where($wherePrevious)
  289. // ->select();
  290. //
  291. // // 初始化结果数组
  292. // $result = [];
  293. //
  294. // // 处理当前月份的数据
  295. // foreach ($rows as $row) {
  296. // // 使用“款式”、“款号”、“订单编号”、“组别”作为唯一标识
  297. // $key = $row['款式'] . '_' . $row['款号'] . '_' . $row['订单编号'] . '_' . $row['组别'];
  298. //
  299. // // 如果结果集中已经存在该条目,则进行累加
  300. // if (!isset($result[$key])) {
  301. // // 初始化一个新的条目
  302. // $result[$key] = [
  303. // '款式' => $row['款式'],
  304. // '款号' => $row['款号'],
  305. // '订单编号' => $row['订单编号'],
  306. // '组别' => $row['组别'],
  307. // '客户编号' => $row['客户编号'],
  308. // '工序' => $row['工序'],
  309. // '裁剪数' => $row['裁剪数'] ?? 0,
  310. // '制单数' => $row['制单数'] ?? 0,
  311. // ];
  312. //
  313. // // 初始化每日数据
  314. // for ($day = 1; $day <= 31; $day++) {
  315. // $result[$key][$day . '号'] = 0;
  316. // }
  317. // }
  318. //
  319. // // 按天累计数据
  320. // $day = (int)date('d', strtotime($row['上报时间']));
  321. // for ($i = 1; $i <= 10; $i++) {
  322. // $keyScsl = 'scsl' . $i;
  323. // $result[$key][$day . '号'] += !empty($row[$keyScsl]) ? (int)$row[$keyScsl] : 0;
  324. // }
  325. // }
  326. //
  327. //
  328. //
  329. // // 计算本月和上个月的累计
  330. // foreach ($result as &$item) {
  331. // $item['本月累计'] = 0;
  332. // $item['上个月累计'] = 0;
  333. //
  334. // // 本月累计
  335. // for ($day = 1; $day <= 31; $day++) {
  336. // $item['本月累计'] += $item[$day . '号'];
  337. // // 如果当日数据为0,则替换为空字符串
  338. // if ($item[$day . '号'] === 0) {
  339. // $item[$day . '号'] = '';
  340. // }
  341. // }
  342. //
  343. // // 上个月累计 (不需要再统计上个月的数据,只保留本月的每日统计)
  344. // }
  345. //
  346. // // 将结果转换为索引数组
  347. // $result = array_values($result);
  348. //
  349. // $this->success('成功', $result);
  350. // }
  351. /**
  352. * 获取工单基本信息
  353. * @ApiMethod GET
  354. * @params string order
  355. */
  356. public function getOrderInfo(){
  357. }
  358. /**
  359. * 获取印件名称及工序
  360. * @ApiMethod GET
  361. * @params string order
  362. * @params string yj_no
  363. */
  364. public function getYjInfo(){
  365. }
  366. /**
  367. * 获取废品分类
  368. * @ApiMethod GET
  369. * @params string search
  370. */
  371. public function getWastInfo(){
  372. }
  373. /**
  374. *获取工序及责任组长
  375. * @ApiMethod GET
  376. * @params string type
  377. * @params string order
  378. */
  379. public function getGxAndLeader(){
  380. }
  381. /**
  382. * 修改工单核检单
  383. * @ApiMethod POST
  384. * @params array data
  385. */
  386. public function edit(){
  387. if (Request::instance()->isPost() == false){
  388. $this->error('非法请求');
  389. }
  390. $params = Request::instance()->post();
  391. if (!isset($params['UniqId'])){
  392. $this->error('参数不能为空');
  393. }
  394. $id = $params['UniqId'];
  395. unset($params['UniqId']);
  396. $sql = db('db_qczl')->where('UniqId',$id)->fetchSql(true)->update($params);
  397. $res = Db::query($sql);
  398. if ($res !== 0){
  399. cache('WorkOrderVerification-getTable',null);
  400. $this->success('更新成功');
  401. }else{
  402. $this->error('更新失败');
  403. }
  404. }
  405. /**
  406. * 新增工单核检单
  407. * @ApiMethod POST
  408. * @params array data
  409. */
  410. public function add(){
  411. if (Request::instance()->isPost() == false){
  412. $this->error('非法请求');
  413. }
  414. $params = Request::instance()->post();
  415. $UniqId = db('db_qczl')->order('UniqId desc')->value('UniqId');
  416. if ($UniqId < 10000000){
  417. $UniqId = 10000000;
  418. }else{
  419. $UniqId = $UniqId + 1;
  420. }
  421. $params['UniqId'] = $UniqId;
  422. $params['sys_rq'] = date('Y-m-d H:i:s');
  423. $res = \db('db_qczl')->insert($params);
  424. if ($res !== false){
  425. cache('WorkOrderVerification-getTable',null);
  426. $this->success('新增成功');
  427. }else{
  428. $this->error('新增失败');
  429. }
  430. }
  431. /**
  432. * 工单核检废品分布
  433. * @ApiMethod GET
  434. * @params string order
  435. */
  436. public function wasteDistribution(){
  437. if (Request::instance()->isGet() == false){
  438. $this->error('非法请求');
  439. }
  440. $params = Request::instance()->param();
  441. if (!isset($params['order']) || empty($params['order'])){
  442. $this->error('参数错误');
  443. }
  444. if (!isset($params['yjno']) || empty($params['yjno'])){
  445. $this->error('参数错误');
  446. }
  447. $field = '
  448. rtrim(fp_lb1) as fp_lb1,rtrim(fp_lb2) as fp_lb2,rtrim(fp_lb3) as fp_lb3,rtrim(fp_lb4) as fp_lb4,rtrim(fp_lb5) as fp_lb5,rtrim(fp_lb6) as fp_lb6,rtrim(fp_lb7) as fp_lb7,
  449. rtrim(fp_lb8) as fp_lb8,rtrim(fp_lb9) as fp_lb9,rtrim(fp_lb10) as fp_lb10,rtrim(fp_lb11) as fp_lb11,rtrim(fp_lb12) as fp_lb12,rtrim(fp_lb13) as fp_lb13,
  450. fp_sl1,fp_sl2,fp_sl3,fp_sl4,fp_sl5,fp_sl6,fp_sl7,fp_sl8,fp_sl9,fp_sl10,fp_sl11,fp_sl12,fp_sl13,qczl_yjno
  451. ';
  452. // $data = db('db_qczl')->name('db_qczl')->where('qczl_gdbh',$params['order'])->field($field)->select();
  453. $data = db('db_qczl')->name('db_qczl')
  454. ->where('qczl_gdbh',$params['order'])
  455. ->where('qczl_yjno', $params['yjno'])
  456. ->field($field)->select();
  457. if (empty($data)){
  458. $this->error('暂无废品数据');
  459. }
  460. // 用于存储废品类别和对应废品数量的数组
  461. $categories = array();
  462. // 循环遍历原始数组
  463. foreach ($data as $item) {
  464. // 提取废品类别和对应废品数量
  465. foreach ($item as $key => $value) {
  466. if (strpos($key, "fp_lb") === 0) {
  467. $categoryKey = $key;
  468. $quantityKey = str_replace("fp_lb", "fp_sl", $key);
  469. $quantityValue = $item[$quantityKey];
  470. if (!isset($categories[$value])) {
  471. $categories[$value] = 0;
  472. }
  473. $categories[$value] += (int)$quantityValue;
  474. }
  475. }
  476. }
  477. ksort($categories);
  478. $wasteTotal = db('db_qczl')->where('qczl_gdbh',$params['order'])->sum('qczl_fp');
  479. if (empty($wasteTotal)){
  480. $wasteTotal = 0;
  481. }
  482. $where['a.Gd_gdbh'] = $params['order'];
  483. $where['a.行号'] = 1;
  484. $gdInfo = db('工单_基本资料')->alias('a')
  485. ->join('工单_印件资料 b','a.Gd_gdbh = b.Yj_Gdbh','left')
  486. ->where($where)->field('a.Gd_gdbh,a.实际投料,b.yj_Yjno,b.yj_yjmc')->find();
  487. $gdInfo['yj_Yjno'] = $gdInfo['yj_Yjno'] > 10 ? $gdInfo['yj_Yjno']:'0'.$gdInfo['yj_Yjno'];
  488. $gdInfo['实际投料'] = (int)($gdInfo['实际投料'] * 10000);
  489. $gdInfo['wasteTotal'] = $wasteTotal;
  490. $finishNum = db('成品入仓')->where('jjcp_gdbh',$params['order'])->sum('jjcp_sl');
  491. $gdInfo['passRate'] = '';
  492. if (!empty($finishNum)){
  493. $gdInfo['passRate'] = number_format((int)$finishNum/$gdInfo['实际投料']*100,2).'%';
  494. }
  495. //左侧废品数据
  496. $wasteData = array();
  497. $i = 0;
  498. foreach ($categories as $key=>$value){
  499. if ($value != 0){
  500. $wasteData[$i]['type'] = $key;
  501. $wasteData[$i]['num'] = $value;
  502. $wasteData[$i]['lossesRate'] = number_format($value/$gdInfo['实际投料']*100,4).'%';
  503. $wasteData[$i]['wasteRate'] = number_format($value/$wasteTotal*100,2).'%';
  504. $i++;
  505. }
  506. }
  507. // 按首字母相同的键将值相加
  508. $newArray = array();
  509. foreach ($categories as $key => $value) {
  510. $firstLetter = strtoupper(substr($key, 0, 1)); // 获取首字母并转换为大写
  511. if (!isset($newArray[$firstLetter])) {
  512. $newArray[$firstLetter] = 0;
  513. }
  514. $newArray[$firstLetter] += $value;
  515. }
  516. // 去掉值为零的项
  517. $newArray = array_filter($newArray, function($value) {
  518. return $value !== 0;
  519. });
  520. //右侧废品数据
  521. $rightData = array();
  522. $i = 0;
  523. $name = array();
  524. foreach ($newArray as $key=>$value){
  525. $item = db('erp_常用字典')->where('名称','like','%'.$key.'%')->value('名称');
  526. $item = explode('_',substr($item,0,-1));
  527. $rightData[$i]['type'] = $item[1];
  528. $rightData[$i]['rate'] = number_format($value/$wasteTotal*100,0).'%';
  529. $name[$i] = $item[1];
  530. $i++;
  531. }
  532. $gdInfo['wasteData'] = $wasteData;
  533. $gdInfo['rightData'] = $rightData;
  534. $gdInfo['rightTitle'] = $name;
  535. $this->success('请求成功',$gdInfo);
  536. }
  537. /**
  538. * 获取工单工序状态
  539. * @ApiMethod GET
  540. * @params string order
  541. * 计划产量计算方式:上个工序计划产量-(基础损耗+损耗率*上个工序计划产量)*损耗系数*计损色数)
  542. * lastNum - (Gy0_Rate0 + Gy0_Rate1 * lastNum )* 损耗系数(Gy0_Rate3) * Gy0_Ms
  543. */
  544. public function getOrderDate(){
  545. if (Request::instance()->isGet() == false){
  546. $this->error('非法请求');
  547. }
  548. $params = Request::instance()->param();
  549. if (!isset($params['order']) || empty($params['order'])){
  550. $this->error('参数错误');
  551. }
  552. $where['a.Gy0_gdbh'] = $params['order'];
  553. $where['a.Gy0_sbbh'] = array('neq',' ');
  554. $productNum = db('工单_基本资料')->where('Gd_gdbh',$params['order'])->value('计划投料');
  555. $gyData = db('工单_工艺资料')->alias('a')
  556. ->join('设备_产量计酬 b','a.Gy0_gdbh = b.sczl_gdbh and a.Gy0_gxh = b.sczl_gxh','left')
  557. ->where($where)
  558. ->field('a.Gy0_yjno,a.Gy0_gxh,rtrim(a.Gy0_gxmc) as Gy0_gxmc,a.Gy0_sbbh,a.Gy0_Rate0,a.Gy0_Rate1,a.损耗系数 as Gy0_Rate3,a.Gy0_Ms,a.Gy0_SITE,a.Gy0_计划接货数,a.PD_WG,
  559. a.UniqId,sum(b.sczl_cl) as cl')
  560. ->group('a.Gy0_gxh')
  561. ->select();
  562. $lastNum = $productNum;
  563. $res = [];
  564. foreach ($gyData as $key=> $process) {
  565. $res[$key]['UniqId'] = $process['UniqId'];
  566. $res[$key]['Gy0_yjno'] = $process['Gy0_yjno'] > 10 ? $process['Gy0_yjno'] : '0'.$process['Gy0_yjno'];
  567. $res[$key]['Gy0_gxh'] = $process['Gy0_gxh'] > 10 ? $process['Gy0_gxh'] : '0'.$process['Gy0_gxh'];
  568. $res[$key]['Gy0_gxmc'] = $process['Gy0_gxmc'];
  569. $res[$key]['Gy0_sbbh'] = rtrim($process['Gy0_sbbh']);
  570. $res[$key]['PD_WG'] = $process['PD_WG'];
  571. $res[$key]['finish'] = empty($process['cl']) ? 0: (int)$process['cl'];
  572. // 计算工序计划产量
  573. if ($process["Gy0_gxh"] == "1") {
  574. // 第一道工序直接使用初始计划产量
  575. $Num = $lastNum;
  576. } else {
  577. $Gy0_Ms = $gyData[$key-1]['Gy0_Ms'];
  578. $Gy0_Rate0 = $gyData[$key-1]['Gy0_Rate0'];
  579. $Gy0_Rate1 = $gyData[$key-1]['Gy0_Rate1'];
  580. $Gy0_Rate3 = $gyData[$key-1]['Gy0_Rate3'];
  581. // 大于第一道工序,使用上一道工序的计划产量
  582. // 根据公式计算工序计划产量
  583. if ($Gy0_Ms != "0.00") {
  584. $Num = $lastNum - ($Gy0_Rate0 + $Gy0_Rate1 * $lastNum) * $Gy0_Rate3 * $Gy0_Ms;
  585. } else {
  586. $Num = $lastNum - ($Gy0_Rate0 + $Gy0_Rate1 * $lastNum) * $Gy0_Rate3;
  587. }
  588. }
  589. if (trim($process['Gy0_SITE']) == '检验车间'){
  590. $res[$key]['plan'] = (int)$process['Gy0_计划接货数'];
  591. }else{
  592. $res[$key]['plan'] = (int)round($Num);
  593. }
  594. $finish_rate = $res[$key]['finish']/$res[$key]['plan'];
  595. $res[$key]['finish_rate'] = '';
  596. if ($finish_rate != 0){
  597. $res[$key]['finish_rate'] = number_format($finish_rate*100,2).'%';
  598. }
  599. // 更新 $lastNum 为当前工序的计划产量
  600. $lastNum = $Num;
  601. }
  602. $this->success('请求成功',$res);
  603. }
  604. /**
  605. * 工单工序状态更正
  606. * @ApiMethod POST
  607. * @params string UniqId
  608. * @params string date
  609. */
  610. public function editOrderFinishDate(){
  611. if (Request::instance()->isPost() == false){
  612. $this->error('非法请求');
  613. }
  614. $params = Request::instance()->post();
  615. if (!isset($params['UniqId']) || !isset($params['date'])){
  616. $this->error('参数错误');
  617. }
  618. if (empty($params['UniqId'])){
  619. $this->error('参数不能为空');
  620. }
  621. if (empty($params['date'])){
  622. $params['date'] = '1900-01-01 00:00:00';
  623. }
  624. $res = db('工单_工艺资料')->where('UniqId',$params['UniqId'])->setField('PD_WG',$params['date']);
  625. if ($res != false){
  626. $this->success('更新成功');
  627. }else{
  628. $this->error('更新失败');
  629. }
  630. }
  631. /**
  632. * 工单工序生产进程菜单栏
  633. * @ApiMethod GET
  634. * @params string order
  635. */
  636. public function getOrderProcessLeft(){
  637. if (Request::instance()->isGet() == false){
  638. $this->error('非法请求');
  639. }
  640. $params = Request::instance()->param();
  641. if (!isset($params['order']) || empty($params['order'])){
  642. $this->error('参数错误');
  643. }
  644. if (!isset($params['yjno']) || empty($params['yjno'])){
  645. $this->error('参数错误');
  646. }
  647. $where['Gd_gdbh'] = $params['order'];
  648. $where['行号'] = 1;
  649. //工单基本资料
  650. $info = db('工单_基本资料')->where($where)->field('rtrim(成品代号) as code,rtrim(成品名称) as name')->find();
  651. //工艺资料
  652. $option['Gy0_gdbh'] = $params['order'];
  653. $option['Gy0_yjno'] = $params['yjno'];
  654. $gyInfo = db('工单_工艺资料')
  655. ->where($option)
  656. ->where(function ($query) {
  657. $query->where('Gy0_sbbh', '<>', '')
  658. ->whereOr('Gy0_gxmc', '核检')
  659. ->whereOr('Gy0_gxmc', '成品防护');
  660. })
  661. ->field('Gy0_yjno,Gy0_gxh,rtrim(Gy0_gxmc) as Gy0_gxmc')
  662. ->select();
  663. foreach ($gyInfo as $key=>$value){
  664. $gyInfo[$key]['Gy0_yjno'] = $value['Gy0_yjno'] > 10 ? $value['Gy0_yjno'] : '0'.$value['Gy0_yjno'];
  665. $gyInfo[$key]['Gy0_gxh'] = $value['Gy0_gxh'] > 10 ? $value['Gy0_gxh'] : '0'.$value['Gy0_gxh'];
  666. }
  667. $res['Gd_info'] = $info;
  668. $res['Gy_info'] = $gyInfo;
  669. $this->success('请求成功',$res);
  670. }
  671. /**
  672. * 工单工序生产进程右侧
  673. * @ApiMethod GET
  674. * @params string order
  675. * @params string gxNo
  676. */
  677. public function getOrderProcessRight(){
  678. if (Request::instance()->isGet() == false){
  679. $this->error('非法请求');
  680. }
  681. $params = Request::instance()->param();
  682. if (!isset($params['order']) || empty($params['order'])){
  683. $this->error('参数错误');
  684. }
  685. if (!isset($params['yjno']) || empty($params['yjno'])){
  686. $this->error('参数错误');
  687. }
  688. if (!isset($params['gxNo']) || empty($params['gxNo'])){
  689. $this->error('参数错误');
  690. }
  691. $total = db('设备_产量计酬')->where('sczl_gdbh',$params['order'])->where('sczl_gdbh',$params['yjno'])->order('sczl_num')->column('distinct(sczl_num)');
  692. $where['sczl_gxh'] = (int)$params['gxNo'];
  693. $where['sczl_gdbh'] = $params['order'];
  694. $where['sczl_yjno'] = $params['yjno'];
  695. //机器设备数据
  696. $process = db('设备_产量计酬')->where($where)->order('sczl_num')->column('distinct(sczl_num)');
  697. //手工数据
  698. $option['qczl_gdbh'] = $params['order'];
  699. $option['qczl_gxh'] = (int)$params['gxNo'];
  700. $option['qczl_yjno'] = $params['yjno'];
  701. $handProcess = db('db_qczl')->where($option)->field('qczl_num,qczl_NumDesc1,qczl_NumDesc2,qczl_NumDesc3,qczl_NumDesc4,qczl_NumDesc5,qczl_NumDesc6,qczl_NumDesc7,qczl_NumDesc8')->select();
  702. // 提取数据
  703. $result = array();
  704. foreach ($handProcess as $subArray) {
  705. $qczl_num = $subArray["qczl_num"];
  706. $result[] = $qczl_num;
  707. // 提取以qczl_NumDesc开头的键对应的值
  708. for ($i = 1; $i <= 8; $i++) {
  709. $key = "qczl_NumDesc" . $i;
  710. if ($subArray[$key] != 0){
  711. $result[] = $subArray[$key];
  712. }
  713. }
  714. }
  715. // 去重
  716. $result = array_unique($result);
  717. // 对结果进行排序
  718. sort($result);
  719. $res['total_process'] = $total;
  720. $res['process'] = $process;
  721. if (empty($process)){
  722. $res['process'] = $result;
  723. }
  724. $this->success('请求成功',$res);
  725. }
  726. /**
  727. * 核检废品日统计
  728. * @ApiMethod GET
  729. * @params date start_date
  730. * @params date end_date
  731. */
  732. public function getDaysWast()
  733. {
  734. if (Request::instance()->isGet() == false) {
  735. $this->error('非法请求');
  736. }
  737. $params = Request::instance()->param();
  738. if (!isset($params['start_date']) || empty($params['start_date'])) {
  739. $this->error('参数错误');
  740. }
  741. if (!isset($params['end_date']) || empty($params['end_date'])) {
  742. $this->error('参数错误');
  743. }
  744. $where['qczl_rq'] = array('between time', [$params['start_date'], $params['end_date']]);
  745. $field = '
  746. rtrim(fp_lb1) as fp_lb1,rtrim(fp_lb2) as fp_lb2,rtrim(fp_lb3) as fp_lb3,rtrim(fp_lb4) as fp_lb4,rtrim(fp_lb5) as fp_lb5,rtrim(fp_lb6) as fp_lb6,rtrim(fp_lb7) as fp_lb7,
  747. rtrim(fp_lb8) as fp_lb8,rtrim(fp_lb9) as fp_lb9,rtrim(fp_lb10) as fp_lb10,rtrim(fp_lb11) as fp_lb11,rtrim(fp_lb12) as fp_lb12,rtrim(fp_lb13) as fp_lb13,
  748. fp_sl1,fp_sl2,fp_sl3,fp_sl4,fp_sl5,fp_sl6,fp_sl7,fp_sl8,fp_sl9,fp_sl10,fp_sl11,fp_sl12,fp_sl13,
  749. fp_bh1,fp_bh2,fp_bh3,fp_bh4,fp_bh5,fp_bh6,fp_bh7,fp_bh8,fp_bh9,fp_bh10,fp_bh11,fp_bh12,fp_bh13,
  750. rtrim(fp_bz1) as fp_bz1, rtrim(fp_bz2) as fp_bz2, rtrim(fp_bz3) as fp_bz3, rtrim(fp_bz4) as fp_bz4, rtrim(fp_bz5) as fp_bz5, rtrim(fp_bz6) as fp_bz6, rtrim(fp_bz7) as fp_bz7,
  751. rtrim(fp_bz8) as fp_bz8, rtrim(fp_bz9) as fp_bz9, rtrim(fp_bz10) as fp_bz10, rtrim(fp_bz11) as fp_bz11, rtrim(fp_bz12) as fp_bz12, rtrim(fp_bz13) as fp_bz13,
  752. qczl_rq
  753. ';
  754. $resultArray = array();
  755. $key = 'getDaysWast'.$params['start_date'].'/'.$params['end_date'];
  756. $is_have_cache = Cache::has($key);
  757. if ($is_have_cache === false){
  758. $data = db('db_qczl')
  759. ->where($where)
  760. ->where(function ($query) {
  761. for ($i = 1; $i <= 13; $i++) {
  762. $query->whereOr("fp_lb$i", 'like', '%K%','AND',"fp_sl$i",'>',0);
  763. }
  764. })
  765. ->field($field)->select();
  766. $list = [];
  767. $j = 0;
  768. foreach ($data as $entry) {
  769. for ($i = 1; $i <= 13; $i++) {
  770. $labelKey = "fp_lb" . $i;
  771. $slKey = "fp_sl" . $i;
  772. $bhKey = "fp_bh" . $i;
  773. $bzKey = "fp_bz" . $i;
  774. if (!empty($entry[$labelKey])) {
  775. if ((substr($entry[$labelKey],0,3) == 'K02' || substr($entry[$labelKey],0,3) == 'K01') && $entry[$slKey] != '0'){
  776. $list[$j]['fp_lb'] = $entry[$labelKey];
  777. $list[$j]['fp_sl'] = $entry[$slKey];
  778. $list[$j]['fp_bh'] = $entry[$bhKey];
  779. $name = db('人事_基本资料')->where('员工编号',$entry[$bhKey])->value('rtrim(员工姓名)');
  780. $list[$j]['fp_name'] = empty($name) ? '计时工' : $name;
  781. $list[$j]['fp_bz'] = $entry[$bzKey];
  782. $list[$j]['qczl_rq'] = $entry['qczl_rq'];
  783. $j++;
  784. }
  785. }
  786. }
  787. }
  788. foreach ($list as $item) {
  789. $found = false;
  790. foreach ($resultArray as &$resultItem) {
  791. if ($item["fp_lb"] === $resultItem["fp_lb"] && $item["fp_bh"] === $resultItem["fp_bh"]) {
  792. $resultItem["fp_sl"] += (int)$item["fp_sl"];
  793. $found = true;
  794. break;
  795. }
  796. }
  797. if (!$found) {
  798. $resultArray[] = $item;
  799. }
  800. }
  801. // 使用usort进行排序
  802. usort($resultArray, function($a, $b) {
  803. // First, sort by fp_lb (K01/K02)
  804. $compareLb = strcmp($a['fp_lb'], $b['fp_lb']);
  805. if ($compareLb !== 0) {
  806. return $compareLb;
  807. }
  808. // If fp_lb is the same, prioritize B班 over A班
  809. $compareBz = strcmp($a['fp_bz'], $b['fp_bz']);
  810. if ($compareBz !== 0) {
  811. return (trim($a['fp_bz']) === 'B班') ? -1 : 1;
  812. }
  813. // If fp_bz is the same, sort by fp_bh
  814. $compareBh = strcmp($a['fp_bh'], $b['fp_bh']);
  815. return $compareBh;
  816. });
  817. Cache::set($key,$resultArray,86400);
  818. }else{
  819. $resultArray = Cache::get($key);
  820. }
  821. $this->success('请求成功',$resultArray);
  822. }
  823. /**
  824. * 月度核检废品责任人统计
  825. * @ApiMethod GET
  826. * @params string date
  827. */
  828. public function getMonthPeopleTotal(){
  829. if (Request::instance()->isGet() == false) {
  830. $this->error('非法请求');
  831. }
  832. $params = Request::instance()->param();
  833. if (!isset($params['date']) || empty($params['date'])) {
  834. $this->error('参数错误');
  835. }
  836. }
  837. /**
  838. * 工单质检废品统计
  839. * @ApiMethod GET
  840. * @params string order
  841. */
  842. public function getOrderWasteTotal(){
  843. if (Request::instance()->isGet() == false) {
  844. $this->error('非法请求');
  845. }
  846. $params = Request::instance()->param();
  847. if (!isset($params['order']) || empty($params['order'])) {
  848. $this->error('参数错误');
  849. }
  850. $field = '
  851. rtrim(a.fp_lb1) as fp_lb1,rtrim(a.fp_lb2) as fp_lb2,rtrim(a.fp_lb3) as fp_lb3,rtrim(a.fp_lb4) as fp_lb4,rtrim(a.fp_lb5) as fp_lb5,rtrim(a.fp_lb6) as fp_lb6,rtrim(a.fp_lb7) as fp_lb7,
  852. rtrim(a.fp_lb8) as fp_lb8,rtrim(a.fp_lb9) as fp_lb9,rtrim(a.fp_lb10) as fp_lb10,rtrim(a.fp_lb11) as fp_lb11,rtrim(a.fp_lb12) as fp_lb12,rtrim(a.fp_lb13) as fp_lb13,
  853. a.fp_sl1,a.fp_sl2,a.fp_sl3,a.fp_sl4,a.fp_sl5,a.fp_sl6,a.fp_sl7,a.fp_sl8,a.fp_sl9,a.fp_sl10,a.fp_sl11,a.fp_sl12,a.fp_sl13,
  854. a.fp_bh1,a.fp_bh2,a.fp_bh3,a.fp_bh4,a.fp_bh5,a.fp_bh6,a.fp_bh7,a.fp_bh8,a.fp_bh9,a.fp_bh10,a.fp_bh11,a.fp_bh12,a.fp_bh13,
  855. rtrim(a.fp_gxmc1) as fp_gxmc1, rtrim(a.fp_gxmc2) as fp_gxmc2, rtrim(a.fp_gxmc3) as fp_gxmc3, rtrim(a.fp_gxmc4) as fp_gxmc4, rtrim(a.fp_gxmc5) as fp_gxmc5, rtrim(a.fp_gxmc6) as fp_gxmc6,
  856. rtrim(a.fp_gxmc7) as fp_gxmc7,rtrim(a.fp_gxmc8) as fp_gxmc8, rtrim(a.fp_gxmc9) as fp_gxmc9, rtrim(a.fp_gxmc10) as fp_gxmc10, rtrim(a.fp_gxmc11) as fp_gxmc11, rtrim(a.fp_gxmc12) as fp_gxmc12,
  857. rtrim(a.fp_gxmc13) as fp_gxmc13,a.qczl_num,a.qczl_yjno,rtrim(b.成品代号) as product_code,rtrim(b.成品名称) as product_name,b.订单数量,rtrim(b.计量单位) as 计量单位,b.实际投料,b.交货日期,
  858. c.yj_ls,d.jjcp_sl,d.jjcp_sj
  859. ';
  860. $where['a.qczl_gdbh'] = $params['order'];
  861. $where['a.qczl_yjno'] = $params['yjno'];
  862. $where['b.行号'] = 1;
  863. $data = db('db_qczl')->alias('a')
  864. ->join('工单_基本资料 b','a.qczl_gdbh = b.Gd_gdbh','left')
  865. ->join('工单_印件资料 c','a.qczl_gdbh = c.Yj_Gdbh','left')
  866. ->join('成品入仓 d','a.qczl_gdbh = d.jjcp_gdbh','left')
  867. ->where($where)
  868. ->field($field)
  869. // ->where('a.qczl_yjno','1')
  870. ->group('qczl_yjno,qczl_num')
  871. ->select();
  872. $list = [];
  873. $j = 0;
  874. foreach ($data as $entry) {
  875. for ($i = 1; $i <= 13; $i++) {
  876. $labelKey = "fp_lb" . $i;
  877. $slKey = "fp_sl" . $i;
  878. $bhKey = "fp_bh" . $i;
  879. $gxhKey = "fp_gxmc". $i;
  880. if (!empty($entry[$labelKey]) && $entry[$slKey] > 0) {
  881. $list[$j]['fp_lb'] = $entry[$labelKey];
  882. $list[$j]['fp_sl'] = $entry[$slKey];
  883. $list[$j]['fp_bh'] = $entry[$bhKey];
  884. $name = db('人事_基本资料')->where('员工编号',$entry[$bhKey])->value('rtrim(员工姓名)');
  885. $list[$j]['fp_name'] = $name;
  886. $list[$j]['fp_gxh'] = empty(substr($entry[$gxhKey],0,2)) ? "0" : substr($entry[$gxhKey],0,2);
  887. $list[$j]['qczl_num'] = $entry['qczl_num'];
  888. $list[$j]['qczl_yjno'] = $entry['qczl_yjno'];
  889. $list[$j]['product_code'] = $entry['product_code'];
  890. $list[$j]['product_name'] = $entry['product_name'];
  891. $list[$j]['订单数量'] = $entry['订单数量'];
  892. $list[$j]['计量单位'] = $entry['计量单位'];
  893. $list[$j]['交货日期'] = substr($entry['交货日期'],0,10);
  894. $list[$j]['yj_ls'] = $entry['yj_ls'];
  895. $list[$j]['jjcp_sl'] = $entry['jjcp_sl'];
  896. $list[$j]['jjcp_sj'] = substr($entry['jjcp_sj'],0,10);
  897. $j++;
  898. }
  899. }
  900. }
  901. $summedData = [];
  902. foreach ($list as $item) {
  903. $key = $item['qczl_num'] . $item['fp_lb'];
  904. if (!isset($summedData[$key])) {
  905. $summedData[$key] = $item;
  906. } else {
  907. $summedData[$key]['fp_sl'] += $item['fp_sl'];
  908. }
  909. }
  910. $fpGxhColumn = array_column($summedData, 'fp_gxh');
  911. $qczlNumColumn = array_column($summedData, 'qczl_num');
  912. $fpLbColumn = array_column($summedData, 'fp_lb');
  913. array_multisort($fpGxhColumn, SORT_ASC, $qczlNumColumn, SORT_ASC, $fpLbColumn, SORT_ASC, $summedData);
  914. $summedData = array_values($summedData);
  915. $this->success('请求成功',$summedData);
  916. }
  917. /**
  918. * 工单核检单删除
  919. * @return void
  920. * @throws \think\Exception
  921. * @throws \think\exception\PDOException
  922. */
  923. public function del()
  924. {
  925. if ($this->request->isGet() === false){
  926. $this->error('请求错误');
  927. }
  928. $param = $this->request->param();
  929. if (empty($param['UniqId'])){
  930. $this->error('参数错误');
  931. }
  932. $res = \db('db_qczl')
  933. ->where('UniqId',$param['UniqId'])
  934. ->delete();
  935. if ($res !== false){
  936. $this->success('删除成功');
  937. }else{
  938. $this->error('产出失败');
  939. }
  940. }
  941. }