WorkOrderVerification.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. * 生产产量进度月报表(Excel)
  24. */
  25. public function getOneWorkOrder() {
  26. if (!$this->request->isGet()) {
  27. $this->error('请求方式错误');
  28. }
  29. $param = $this->request->param();
  30. // 查询当前订单的生产总数和整单总数
  31. $totals = db()->table('工单_印件资料')->alias('y')
  32. ->field('y.订单编号, SUM(y.sctotal) as sctotal, SUM(y.zdtotal) as zdtotal')
  33. ->group('y.订单编号')
  34. ->whereNull('y.Mod_rq')
  35. ->order('y.订单编号')
  36. ->select();
  37. // 保存当前订单的生产总数和整单总数到一个数组,以便后续关联
  38. $totalsMap = [];
  39. foreach ($totals as $total) {
  40. $totalsMap[$total['订单编号']] = [
  41. 'sctotal' => $total['sctotal'],
  42. 'zdtotal' => $total['zdtotal'],
  43. ];
  44. }
  45. // 获取上个月的日期,格式为 Y-m
  46. $lastMonth = date('Y-m', strtotime($param['riqi'] . ' -1 month'));
  47. // 构造查询条件
  48. $wheres['sczl_rq'] = ['like', '%' . $lastMonth . '%']; // 查询上个月的数据
  49. $wheres['sczl_bh'] = ['neq', '']; // 根据需要加上你想要的工序条件(假设 sczl_bh 是工序字段)
  50. $sql = db()->table('设备_产量计酬')
  51. ->field('订单编号, sczl_jtbh, sczl_bh,
  52. CASE
  53. WHEN 工序名称 IN ("裁剪", "车缝") THEN SUM(数量)
  54. ELSE SUM(s_num)
  55. END as 上月累计') // 加上工序字段
  56. ->where($wheres)
  57. ->whereNull('mod_rq') // 确保 mod_rq 字段为空
  58. ->group('订单编号, sczl_jtbh, sczl_bh') // 按 订单编号, sczl_jtbh, sczl_bh 分组
  59. ->select();
  60. // 将上个月的累计数据保存到一个数组,确保按订单编号和机台号保存
  61. $lastMonthMap = [];
  62. foreach ($sql as $lastRow) {
  63. $lastMonthMap[$lastRow['订单编号']][$lastRow['sczl_jtbh']] = (int)$lastRow['上月累计'];
  64. }
  65. $currentMonth = date('Y-m', strtotime($param['riqi'])); // 获取前端传来的日期所在月份,格式为 Y-m
  66. $lastMonth = date('Y-m', strtotime($param['riqi'] . ' -1 month')); // 获取前端日期的上个月,格式为 Y-m
  67. // 当前月份的查询条件
  68. $where['c.sczl_rq'] = ['like', '%' . $currentMonth . '%'];
  69. // 查询每天的上报数量,并关联生产总数和整单总数
  70. $rows = db()->table('设备_产量计酬')->alias('c')
  71. ->join('工单_基本资料 j', 'c.订单编号 = j.订单编号', 'left')
  72. ->field('
  73. c.订单编号, j.款式, j.生产款号, j.客户编号,
  74. c.sczl_jtbh, c.工序名称 as 工序, c.sczl_bh,
  75. CASE
  76. WHEN c.工序名称 IN ("裁剪", "车缝") THEN c.数量
  77. ELSE c.s_num
  78. END as 上报数量,
  79. c.sczl_rq as 上报时间
  80. ')
  81. ->where($where)
  82. ->whereNull('c.mod_rq')
  83. ->order('c.sczl_rq desc')
  84. ->select();
  85. // 初始化
  86. $result = [];
  87. // 处理当前月份的数据
  88. foreach ($rows as $row) {
  89. $key = $row['款式'] . '_' . $row['生产款号'] . '_' . $row['订单编号'] . '_' . $row['sczl_jtbh'];
  90. // 获取当前订单编号的生产总数和整单总数
  91. $sctotal = isset($totalsMap[$row['订单编号']]) ? $totalsMap[$row['订单编号']]['sctotal'] : 0;
  92. $zdtotal = isset($totalsMap[$row['订单编号']]) ? $totalsMap[$row['订单编号']]['zdtotal'] : 0;
  93. // 获取上个月的累计数量,按机台号获取
  94. $lastMonthTotal = isset($lastMonthMap[$row['订单编号']][$row['sczl_jtbh']]) ? $lastMonthMap[$row['订单编号']][$row['sczl_jtbh']] : 0;
  95. // 初始化当前订单编号和组别的数据
  96. if (!isset($result[$key])) {
  97. $result[$key] = [
  98. '订单编号' => $row['订单编号'],
  99. '客户编号' => $row['客户编号'],
  100. '机台号' => $row['sczl_jtbh'],
  101. '款式' => $row['款式'],
  102. '款号' => $row['生产款号'],
  103. '工序' => $row['工序'],
  104. '组别' => $row['sczl_bh'],
  105. '制单数' => $zdtotal,
  106. '裁剪数' => $sctotal,
  107. '上月累计' => $lastMonthTotal,
  108. '上报数量' => 0,
  109. ];
  110. // 初始化每一天的数据
  111. for ($day = 1; $day <= 31; $day++) {
  112. $result[$key][$day] = 0;
  113. }
  114. }
  115. // 获取上报的具体日期
  116. $day = (int)date('d', strtotime($row['上报时间']));
  117. // 按天累加上报数量
  118. $result[$key][$day] += (int)$row['上报数量'];
  119. // 累计上报数量
  120. $result[$key]['上报数量'] += (int)$row['上报数量'];
  121. }
  122. // 计算本月累计数据
  123. foreach ($result as &$item) {
  124. $item['本月累计'] = 0;
  125. // 累计本月每天的数据
  126. for ($day = 1; $day <= 31; $day++) {
  127. $item['本月累计'] += $item[$day];
  128. if ($item[$day] === 0) {
  129. $item[$day] = '';
  130. }
  131. }
  132. }
  133. // 排序
  134. usort($result, function ($a, $b) {
  135. // 先按客户编号排序
  136. $clientComparison = strcmp($a['客户编号'], $b['客户编号']);
  137. if ($clientComparison !== 0) {
  138. return $clientComparison;
  139. }
  140. // 再按组别排序
  141. $groupComparison = strcmp($a['组别'], $b['组别']);
  142. if ($groupComparison !== 0) {
  143. return $groupComparison;
  144. }
  145. // 最后按订单编号排序
  146. return strcmp($a['订单编号'], $b['订单编号']);
  147. });
  148. $result = array_values($result);
  149. $this->success('成功', $result);
  150. }
  151. /**
  152. *
  153. */
  154. public function getInfo()
  155. {
  156. }
  157. /**
  158. *
  159. */
  160. public function getOrderInfo(){
  161. }
  162. /**
  163. *
  164. */
  165. public function getYjInfo(){
  166. }
  167. /**
  168. *
  169. */
  170. public function getWastInfo(){
  171. }
  172. /**
  173. *
  174. */
  175. public function getGxAndLeader(){
  176. }
  177. /**
  178. *
  179. */
  180. public function edit(){
  181. }
  182. /**
  183. *
  184. */
  185. public function add(){
  186. }
  187. /**
  188. *
  189. */
  190. public function wasteDistribution(){
  191. }
  192. /**
  193. *
  194. */
  195. public function getOrderDate(){
  196. }
  197. /**
  198. *
  199. */
  200. public function editOrderFinishDate(){
  201. }
  202. /**
  203. *
  204. */
  205. public function getOrderProcessLeft(){
  206. }
  207. /**
  208. *
  209. */
  210. public function getOrderProcessRight(){
  211. }
  212. /**
  213. *
  214. */
  215. public function getDaysWast()
  216. {
  217. }
  218. /**
  219. *
  220. */
  221. public function getMonthPeopleTotal(){
  222. }
  223. /**
  224. *
  225. */
  226. public function getOrderWasteTotal(){
  227. }
  228. /**
  229. *
  230. */
  231. public function del()
  232. {
  233. }
  234. }