OrderSuperLoss.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. ->join('设备_产量计酬 c', 'g.订单编号 = c.订单编号', 'LEFT')
  77. ->where('g.落货日期', '>=', $start)
  78. ->where('g.落货日期', '<', $end)
  79. ->whereNull('g.Mod_rq')
  80. ->whereNull('c.Mod_rq')
  81. ->field([
  82. 'g.接单日期 as 下单日期',
  83. 'g.落货日期 as 货期',
  84. 'g.生产款号 as 款号',
  85. 'g.订单数量',
  86. 'g.工单入仓数量 as 入库数量',
  87. 'g.款式',
  88. 'g.客户编号 as 客人编号',
  89. \think\Db::raw('MIN(CASE WHEN d.name = "入库" THEN d.rq END) as 面料入库时间'),
  90. \think\Db::raw('MIN(CASE WHEN c.工序名称 = "裁剪" THEN c.sczl_rq END) as 开裁日期'),
  91. \think\Db::raw('SUM(CASE WHEN c.工序名称 = "裁剪" THEN c.数量 ELSE 0 END) as 实裁数量'),
  92. \think\Db::raw('MIN(CASE WHEN c.工序名称 = "车缝" THEN c.sczl_rq END) as 上车位日期'),
  93. \think\Db::raw('MIN(CASE WHEN c.工序名称 = "车缝" AND c.尾包 = 1 THEN c.sczl_rq END) as 车位完成日期'),
  94. \think\Db::raw('MIN(CASE WHEN c.工序名称 = "手工" AND c.尾包 = 1 THEN c.sczl_rq END) as 后道完成日期'),
  95. \think\Db::raw('SUM(CASE WHEN c.工序名称 = "包装" THEN c.数量 ELSE 0 END) as 已完成数量'),
  96. // 生产组别:只从车缝工序获取,使用子查询
  97. \think\Db::raw('(SELECT GROUP_CONCAT(DISTINCT SUBSTRING(sczl_jtbh, 3, 2) ORDER BY SUBSTRING(sczl_jtbh, 3, 2) SEPARATOR "、")
  98. FROM 设备_产量计酬
  99. WHERE 订单编号 = g.订单编号
  100. AND 工序名称 = "车缝"
  101. AND Mod_rq IS NULL) as 生产组别'),
  102. \think\Db::raw('NULL as 辅料入库时间'),
  103. \think\Db::raw('NULL as 台产'),
  104. \think\Db::raw('NULL as 产前样批核'),
  105. \think\Db::raw('NULL as 备注'),
  106. ])
  107. ->group('g.订单编号')
  108. ->select();
  109. if (empty($list)) {
  110. $this->success('', []);
  111. }
  112. // 处理日期字段,去掉时分秒
  113. $dateFields = ['下单日期', '货期', '面料入库时间', '辅料入库时间', '开裁日期', '上车位日期', '车位完成日期', '后道完成日期'];
  114. foreach ($list as &$item) {
  115. foreach ($dateFields as $field) {
  116. if (isset($item[$field]) && !empty($item[$field])) {
  117. $item[$field] = date('Y-m-d', strtotime($item[$field]));
  118. }
  119. }
  120. if (empty($item['生产组别'])) {
  121. $item['生产组别'] = '';
  122. }
  123. }
  124. $this->success('成功', $list);
  125. }
  126. }