OrderSuperLoss.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Cache;
  5. use think\Exception;
  6. use think\Log;
  7. use \think\Request;
  8. use \think\Db;
  9. /**
  10. * 报表接口
  11. */
  12. class OrderSuperLoss 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. * @return void
  27. * @throws \think\db\exception\DataNotFoundException
  28. * @throws \think\db\exception\ModelNotFoundException
  29. * @throws \think\exception\DbException
  30. */
  31. public function getWorkOrderDates()
  32. {
  33. if ($this->request->isGet() === false) {
  34. $this->error('请求错误');
  35. }
  36. $list = \think\Db::table('工单_基本资料')
  37. ->whereNull('Mod_rq') // 排除 Mod_rq 不为 null 的数据
  38. ->where('落货日期', '<>', '') // 排除空值
  39. ->field(\think\Db::raw('DATE_FORMAT(落货日期, "%Y-%m") as 年月'))
  40. ->group('年月')
  41. ->order('年月', 'desc') // 按年月倒序排列,最新的在前面
  42. ->select();
  43. if (empty($list)) {
  44. $this->success('', []);
  45. }
  46. // 提取年月值组成数组
  47. $data = [];
  48. foreach ($list as $item) {
  49. $data[] = $item['年月'];
  50. }
  51. $this->success('成功', $data);
  52. }
  53. /**
  54. * 根据月份获取工单数据(排除已修改记录)
  55. * @return void
  56. * @throws \think\db\exception\DataNotFoundException
  57. * @throws \think\db\exception\ModelNotFoundException
  58. * @throws \think\exception\DbException
  59. */
  60. public function getWorkOrdersByMonth()
  61. {
  62. if ($this->request->isGet() === false) {
  63. $this->error('请求错误');
  64. }
  65. $rq = $this->request->get('rq');
  66. if (empty($rq) || !preg_match('/^\d{4}-\d{2}$/', $rq)) {
  67. $this->error('参数格式错误,请使用 YYYY-MM 格式');
  68. }
  69. // 计算月份起止日期
  70. list($year, $month) = explode('-', $rq);
  71. $start = "{$year}-{$month}-01";
  72. $end = date('Y-m-d', strtotime("{$year}-{$month}-01 +1 month"));
  73. $list = \think\Db::table('工单_基本资料')
  74. ->alias('g')
  75. ->join('库存_出入库明细 d', 'g.订单编号 = d.order_id', 'LEFT')
  76. ->where('g.落货日期', '>=', $start)
  77. ->where('g.落货日期', '<', $end)
  78. ->whereNull('g.Mod_rq')
  79. ->field([
  80. 'g.Uniqid',
  81. 'g.订单编号',
  82. 'g.审核 as 核批',
  83. 'g.审核日期 as 核批日期',
  84. 'g.接单日期 as 下单日期',
  85. 'g.落货日期 as 货期',
  86. 'g.生产款号 as 款号',
  87. 'g.订单数量',
  88. 'g.工单入仓数量 as 入库数量',
  89. 'g.款式',
  90. 'g.客户编号 as 客人编号',
  91. // 面料入库时间 - 使用子查询避免重复
  92. \think\Db::raw('(SELECT MIN(rq) FROM 库存_出入库明细 WHERE order_id = g.订单编号 AND name = "入库") as 面料入库时间'),
  93. // 裁剪相关 - 使用子查询
  94. \think\Db::raw('(SELECT MIN(sczl_rq) FROM 设备_产量计酬 WHERE 订单编号 = g.订单编号 AND 工序名称 = "裁剪" AND Mod_rq IS NULL) as 开裁日期'),
  95. \think\Db::raw('(SELECT SUM(数量) FROM 设备_产量计酬 WHERE 订单编号 = g.订单编号 AND 工序名称 = "裁剪" AND Mod_rq IS NULL) as 实裁数量'),
  96. // 车缝相关 - 使用子查询
  97. \think\Db::raw('(SELECT MIN(sczl_rq) FROM 设备_产量计酬 WHERE 订单编号 = g.订单编号 AND 工序名称 = "车缝" AND Mod_rq IS NULL) as 上车位日期'),
  98. \think\Db::raw('(SELECT MIN(sczl_rq) FROM 设备_产量计酬 WHERE 订单编号 = g.订单编号 AND 工序名称 = "车缝" AND 尾包 = 1 AND Mod_rq IS NULL) as 车位完成日期'),
  99. // 后道相关 - 使用子查询
  100. \think\Db::raw('(SELECT MIN(sczl_rq) FROM 设备_产量计酬 WHERE 订单编号 = g.订单编号 AND 工序名称 = "手工" AND 尾包 = 1 AND Mod_rq IS NULL) as 后道完成日期'),
  101. // 包装完成数量 - 使用子查询
  102. \think\Db::raw('(SELECT SUM(数量) FROM 设备_产量计酬 WHERE 订单编号 = g.订单编号 AND 工序名称 = "包装" AND Mod_rq IS NULL) as 已完成数量'),
  103. // 生产组别 - 使用子查询
  104. \think\Db::raw('(SELECT GROUP_CONCAT(DISTINCT SUBSTRING(sczl_jtbh, 3, 2) ORDER BY SUBSTRING(sczl_jtbh, 3, 2) SEPARATOR "、")
  105. FROM 设备_产量计酬
  106. WHERE 订单编号 = g.订单编号
  107. AND 工序名称 = "车缝"
  108. AND Mod_rq IS NULL) as 生产组别'),
  109. // 占位字段
  110. \think\Db::raw('NULL as 辅料入库时间'),
  111. \think\Db::raw('NULL as 台产'),
  112. \think\Db::raw('NULL as 产前样批核'),
  113. \think\Db::raw('NULL as 备注'),
  114. ])
  115. ->group('g.订单编号')
  116. ->select();
  117. if (empty($list)) {
  118. $this->success('', []);
  119. }
  120. // 处理日期字段,去掉时分秒
  121. $dateFields = ['下单日期', '货期', '面料入库时间', '辅料入库时间', '开裁日期', '上车位日期', '车位完成日期', '后道完成日期'];
  122. foreach ($list as &$item) {
  123. foreach ($dateFields as $field) {
  124. if (isset($item[$field]) && !empty($item[$field])) {
  125. $item[$field] = date('Y-m-d', strtotime($item[$field]));
  126. }
  127. }
  128. if (empty($item['生产组别'])) {
  129. $item['生产组别'] = '';
  130. }
  131. // 确保数值字段为整数
  132. $item['实裁数量'] = intval($item['实裁数量'] ?? 0);
  133. $item['已完成数量'] = intval($item['已完成数量'] ?? 0);
  134. $item['订单数量'] = intval($item['订单数量'] ?? 0);
  135. $item['入库数量'] = intval($item['入库数量'] ?? 0);
  136. }
  137. $this->success('成功', $list);
  138. }
  139. }