WorkOrderVerification.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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.工序名称', '手工')
  44. // ->where('c.sczl_jtbh', $param['sczl_jtbh'])
  45. // ->whereNull('c.mod_rq') // 查询未删除数据
  46. // ->where(function($query) {
  47. // $query->whereNotNull('y.ck_rq')->where('y.ck_rq', '<>', '');
  48. // }) // 查询出库数据
  49. // ->order('c.UniqId', 'desc')
  50. // ->distinct('c.子订单编号')
  51. // ->select();
  52. // $output = [];
  53. // foreach ($table_list as $record) {
  54. // $output[] = [
  55. // 'serial' => '第('.$record['serial'].')包',
  56. // 'sys_rq' => $record['sys_rq'],
  57. // '订单编号' => $record['订单编号'],
  58. // '子订单编号' => $record['子订单编号'],
  59. // '颜色' => $record['颜色'],
  60. // '尺码' => $record['尺码'],
  61. // '数量' => $record['数量'],
  62. // '上报数量' => $record['s_num'],
  63. // '尾包' => $record['尾包'],
  64. // '组别' => $record['sczl_jtbh'],
  65. // ];
  66. // }
  67. //
  68. // $this->success('请求成功', [
  69. // 'records' => $output
  70. // ]);
  71. // }
  72. /**
  73. * 记录报工日志历史记录
  74. */
  75. public function getTabByGdbh()
  76. {
  77. if (Request::instance()->isPost() === false){
  78. $this->error('请求错误');
  79. }
  80. $param = Request::instance()->post();
  81. if (empty($param)){
  82. $this->error('参数错误');
  83. }
  84. $rows = db()->table('工单_基本资料')
  85. ->where('订单编号',$param['订单编号'])
  86. ->find();
  87. $param['客户编号'] = $rows['客户编号'];
  88. $sql= \db('设备_报工记录')->fetchSql(true)->insert($param);
  89. $res = \db()->query($sql);
  90. if ($res !== false){
  91. $this->success('成功');
  92. }else{
  93. $this->error('失败');
  94. }
  95. }
  96. /**
  97. * 获取报工日志
  98. * @ApiMethod (GET)
  99. */
  100. public function getList()
  101. {
  102. if(!$this->request->isGet()){
  103. $this->error('请求方式错误');
  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. sort($headers);
  136. }
  137. $data = [
  138. 'table' => $rows,
  139. 'headers' => $headers,
  140. 'total' => count($rows),
  141. ];
  142. $this->success('成功', $data);
  143. }
  144. /**
  145. * 生产产量进度月报表(Excel)
  146. */
  147. public function getOneWorkOrder() {
  148. if (!$this->request->isGet()) {
  149. $this->error('请求方式错误');
  150. }
  151. $param = $this->request->param();
  152. // 查询当前订单的生产总数和整单总数
  153. $totals = db()->table('工单_印件资料')->alias('y')
  154. ->field('y.订单编号, SUM(y.sctotal) as sctotal, SUM(y.zdtotal) as zdtotal')
  155. ->group('y.订单编号')
  156. ->whereNull('y.Mod_rq')
  157. ->order('y.订单编号')
  158. ->select();
  159. // 保存当前订单的生产总数和整单总数到一个数组,以便后续关联
  160. $totalsMap = [];
  161. foreach ($totals as $total) {
  162. $totalsMap[$total['订单编号']] = [
  163. 'sctotal' => $total['sctotal'],
  164. 'zdtotal' => $total['zdtotal'],
  165. ];
  166. }
  167. // 获取上个月的日期,格式为 Y-m
  168. $lastMonth = date('Y-m', strtotime($param['riqi'] . ' -1 month'));
  169. // 构造查询条件
  170. $wheres['sczl_rq'] = ['like', '%' . $lastMonth . '%']; // 查询上个月的数据
  171. $wheres['sczl_bh'] = ['neq', '']; // 根据需要加上你想要的工序条件(假设 sczl_bh 是工序字段)
  172. $sql = db()->table('设备_产量计酬')
  173. ->field('订单编号, sczl_jtbh, sczl_bh,
  174. CASE
  175. WHEN 工序名称 IN ("裁剪", "车缝") THEN SUM(数量)
  176. ELSE SUM(s_num)
  177. END as 上月累计') // 加上工序字段
  178. ->where($wheres)
  179. ->whereNull('mod_rq') // 确保 mod_rq 字段为空
  180. ->group('订单编号, sczl_jtbh, sczl_bh') // 按 订单编号, sczl_jtbh, sczl_bh 分组
  181. ->select();
  182. // 将上个月的累计数据保存到一个数组,确保按订单编号和机台号保存
  183. $lastMonthMap = [];
  184. foreach ($sql as $lastRow) {
  185. $lastMonthMap[$lastRow['订单编号']][$lastRow['sczl_jtbh']] = (int)$lastRow['上月累计'];
  186. }
  187. $currentMonth = date('Y-m', strtotime($param['riqi'])); // 获取前端传来的日期所在月份,格式为 Y-m
  188. $lastMonth = date('Y-m', strtotime($param['riqi'] . ' -1 month')); // 获取前端日期的上个月,格式为 Y-m
  189. // 当前月份的查询条件
  190. $where['c.sczl_rq'] = ['like', '%' . $currentMonth . '%'];
  191. // 查询每天的上报数量,并关联生产总数和整单总数
  192. $rows = db()->table('设备_产量计酬')->alias('c')
  193. ->join('工单_基本资料 j', 'c.订单编号 = j.订单编号', 'left')
  194. ->field('
  195. c.订单编号, j.款式, j.生产款号, j.客户编号,
  196. c.sczl_jtbh, c.工序名称 as 工序, c.sczl_bh,
  197. CASE
  198. WHEN c.工序名称 IN ("裁剪", "车缝") THEN c.数量
  199. ELSE c.s_num
  200. END as 上报数量,
  201. c.sczl_rq as 上报时间
  202. ')
  203. ->where($where)
  204. ->whereNull('c.mod_rq')
  205. ->order('c.sczl_rq desc')
  206. ->select();
  207. // 初始化
  208. $result = [];
  209. // 处理当前月份的数据
  210. foreach ($rows as $row) {
  211. $key = $row['款式'] . '_' . $row['生产款号'] . '_' . $row['订单编号'] . '_' . $row['sczl_jtbh'];
  212. // 获取当前订单编号的生产总数和整单总数
  213. $sctotal = isset($totalsMap[$row['订单编号']]) ? $totalsMap[$row['订单编号']]['sctotal'] : 0;
  214. $zdtotal = isset($totalsMap[$row['订单编号']]) ? $totalsMap[$row['订单编号']]['zdtotal'] : 0;
  215. // 获取上个月的累计数量,按机台号获取
  216. $lastMonthTotal = isset($lastMonthMap[$row['订单编号']][$row['sczl_jtbh']]) ? $lastMonthMap[$row['订单编号']][$row['sczl_jtbh']] : 0;
  217. // 初始化当前订单编号和组别的数据
  218. if (!isset($result[$key])) {
  219. $result[$key] = [
  220. '订单编号' => $row['订单编号'],
  221. '客户编号' => $row['客户编号'],
  222. '机台号' => $row['sczl_jtbh'],
  223. '款式' => $row['款式'],
  224. '款号' => $row['生产款号'],
  225. '工序' => $row['工序'],
  226. '组别' => $row['sczl_bh'],
  227. '制单数' => $zdtotal,
  228. '裁剪数' => $sctotal,
  229. '上月累计' => $lastMonthTotal,
  230. '上报数量' => 0,
  231. ];
  232. // 初始化每一天的数据
  233. for ($day = 1; $day <= 31; $day++) {
  234. $result[$key][$day] = 0;
  235. }
  236. }
  237. // 获取上报的具体日期
  238. $day = (int)date('d', strtotime($row['上报时间']));
  239. // 按天累加上报数量
  240. $result[$key][$day] += (int)$row['上报数量'];
  241. // 累计上报数量
  242. $result[$key]['上报数量'] += (int)$row['上报数量'];
  243. }
  244. // 计算本月累计数据
  245. foreach ($result as &$item) {
  246. $item['本月累计'] = 0;
  247. // 累计本月每天的数据
  248. for ($day = 1; $day <= 31; $day++) {
  249. $item['本月累计'] += $item[$day];
  250. if ($item[$day] === 0) {
  251. $item[$day] = '';
  252. }
  253. }
  254. }
  255. // 排序
  256. usort($result, function ($a, $b) {
  257. // 先按客户编号排序
  258. $clientComparison = strcmp($a['客户编号'], $b['客户编号']);
  259. if ($clientComparison !== 0) {
  260. return $clientComparison;
  261. }
  262. // 再按组别排序
  263. $groupComparison = strcmp($a['组别'], $b['组别']);
  264. if ($groupComparison !== 0) {
  265. return $groupComparison;
  266. }
  267. // 最后按订单编号排序
  268. return strcmp($a['订单编号'], $b['订单编号']);
  269. });
  270. $result = array_values($result);
  271. $this->success('成功', $result);
  272. }
  273. /**
  274. *
  275. */
  276. public function getInfo()
  277. {
  278. }
  279. /**
  280. *
  281. */
  282. public function getOrderInfo(){
  283. }
  284. /**
  285. *
  286. */
  287. public function getYjInfo(){
  288. }
  289. /**
  290. *
  291. */
  292. public function getWastInfo(){
  293. }
  294. /**
  295. *
  296. */
  297. public function getGxAndLeader(){
  298. }
  299. /**
  300. *
  301. */
  302. public function edit(){
  303. }
  304. /**
  305. *
  306. */
  307. public function add(){
  308. }
  309. /**
  310. *
  311. */
  312. public function wasteDistribution(){
  313. }
  314. /**
  315. *
  316. */
  317. public function getOrderDate(){
  318. }
  319. /**
  320. *
  321. */
  322. public function editOrderFinishDate(){
  323. }
  324. /**
  325. *
  326. */
  327. public function getOrderProcessLeft(){
  328. }
  329. /**
  330. *
  331. */
  332. public function getOrderProcessRight(){
  333. }
  334. /**
  335. *
  336. */
  337. public function getDaysWast()
  338. {
  339. }
  340. /**
  341. *
  342. */
  343. public function getMonthPeopleTotal(){
  344. }
  345. /**
  346. *
  347. */
  348. public function getOrderWasteTotal(){
  349. }
  350. /**
  351. *
  352. */
  353. public function del()
  354. {
  355. }
  356. }