Achievementatestatistics.php 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use Monolog\Handler\IFTTTHandler;
  5. use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
  6. use think\Db;
  7. use think\Request;
  8. use PhpOffice\PhpSpreadsheet\IOFactory;
  9. class Achievementatestatistics extends Api
  10. {
  11. protected $noNeedLogin = ['*'];
  12. protected $noNeedRight = ['*'];
  13. /**
  14. * 1.1达成率统计->左侧菜单
  15. */
  16. public function Leftmenu(){
  17. // 获取所有部门(排除特定)
  18. $departments = Db::name('设备_基本资料')
  19. ->field('使用部门')
  20. ->whereNotIn('使用部门', ['打样室', '智能车间'])
  21. ->group('使用部门')
  22. ->select();
  23. // 部门名数组
  24. $deptList = array_map(function($item) {
  25. return trim($item['使用部门']);
  26. }, $departments);
  27. // 获取产量表日期
  28. $rawData = Db::name('设备_产量计酬')
  29. ->field("DATE(sys_rq) as rq")
  30. ->whereRaw("YEAR(sys_rq) >= 2000")
  31. ->order("rq desc")
  32. ->select();
  33. $workStats = [];
  34. $addedMonths = []; // 记录已处理年月
  35. foreach ($rawData as $item) {
  36. $rq = $item['rq'];
  37. $year = date('Y', strtotime($rq));
  38. $yearMonth = date('Ym', strtotime($rq));
  39. // 如果该年月还没加过,才添加
  40. if (!isset($addedMonths[$year][$yearMonth])) {
  41. $workStats[$year][$yearMonth] = $deptList;
  42. $addedMonths[$year][$yearMonth] = true;
  43. }
  44. }
  45. $result = [
  46. '排产计划达成率统计' => $departments,
  47. '设备工时达成率统计' => $workStats
  48. ];
  49. $this->success('成功',$result);
  50. }
  51. public function Machine_List()
  52. {
  53. if (!$this->request->isGet()) {
  54. $this->error('请求方式错误');
  55. }
  56. $param = $this->request->param();
  57. if (empty($param['Machine'])) {
  58. $this->error('缺少参数 Machine');
  59. }
  60. // 日期映射:近 7 天
  61. $dateMap = [];
  62. $recentDates = [];
  63. for ($i = 0; $i < 7; $i++) {
  64. $date = date('Y-m-d', strtotime("-{$i} days"));
  65. $label = date('m月d日', strtotime($date));
  66. $dateMap[$date] = $label;
  67. $recentDates[] = $date;
  68. }
  69. $devices = Db::name('设备_基本资料')
  70. ->field('设备编号, 设备名称')
  71. ->whereLike('使用部门', '%' . $param['Machine'] . '%')
  72. ->select();
  73. $results = [];
  74. foreach ($devices as $device) {
  75. $jtbh = $device['设备编号'];
  76. $jtname = trim($device['设备名称']);
  77. // 获取原始产量数据
  78. $records = Db::name('设备_产量计酬')
  79. ->field([
  80. 'sczl_bzdh as 班组',
  81. 'sczl_cl',
  82. 'sczl_Pgcl',
  83. 'sczl_rq as 日期',
  84. 'sczl_gdbh',
  85. 'sczl_yjno',
  86. 'sczl_gxh'
  87. ])
  88. ->where('sczl_jtbh', $jtbh)
  89. ->where('sczl_rq', '>=', date('Y-m-d 00:00:00', strtotime('-6 days')))
  90. ->where('sczl_rq', '<=', date('Y-m-d 23:59:59'))
  91. ->select();
  92. $stat = [];
  93. $summary = [];
  94. foreach ($records as $row) {
  95. $bz = $row['班组'] ?: '未分组';
  96. $rq = date('Y-m-d', strtotime($row['日期']));
  97. $cl = floatval($row['sczl_cl']);
  98. $pgcl = floatval($row['sczl_Pgcl']);
  99. $actual = ($cl == 0) ? 0 : (($pgcl > 0) ? $cl * $pgcl : $cl);
  100. // 查询排产产量
  101. $plan = Db::name('工单_工艺资料')
  102. ->where('Gy0_gdbh', $row['sczl_gdbh'])
  103. ->where('Gy0_yjno', $row['sczl_yjno'])
  104. ->where('Gy0_gxh', $row['sczl_gxh'])
  105. ->value('Gy0_计划接货数');
  106. $plan = floatval($plan);
  107. // 班组维度统计
  108. if (!isset($stat[$bz][$rq])) {
  109. $stat[$bz][$rq] = ['实际产量' => 0, '排产产量' => 0];
  110. }
  111. $stat[$bz][$rq]['实际产量'] += $actual;
  112. $stat[$bz][$rq]['排产产量'] += $plan;
  113. // 合计维度统计
  114. if (!isset($summary[$rq])) {
  115. $summary[$rq] = ['实际产量' => 0, '排产产量' => 0];
  116. }
  117. $summary[$rq]['实际产量'] += $actual;
  118. $summary[$rq]['排产产量'] += $plan;
  119. }
  120. // 输出每个班组
  121. foreach ($stat as $bz => $dateList) {
  122. $item = [
  123. '机台编号' => $jtbh,
  124. '机台名称' => $jtname,
  125. '班组' => $bz,
  126. '实际总产量' => 0,
  127. '排产总产量' => 0,
  128. '近7天综合达成率' => '0%'
  129. ];
  130. $i = 1;
  131. foreach ($dateMap as $date => $label) {
  132. $actual = isset($dateList[$date]) ? round($dateList[$date]['实际产量'], 2) : 0;
  133. $plan = isset($dateList[$date]) ? round($dateList[$date]['排产产量'], 2) : 0;
  134. $rate = ($plan > 0) ? round($actual / $plan * 100, 2) . '%' : '0%';
  135. $item[$label] = $rate;
  136. $item["实际产量{$i}"] = $actual;
  137. $item["排产产量{$i}"] = $plan;
  138. $item['实际总产量'] += $actual;
  139. $item['排产总产量'] += $plan;
  140. $i++;
  141. }
  142. $item['近7天综合达成率'] = ($item['排产总产量'] > 0)
  143. ? round($item['实际总产量'] / $item['排产总产量'] * 100, 2) . '%'
  144. : '0%';
  145. $results[] = $item;
  146. }
  147. // 合计行
  148. $item = [
  149. '机台编号' => $jtbh,
  150. '机台名称' => '合计',
  151. '班组' => '',
  152. '实际总产量' => 0,
  153. '排产总产量' => 0,
  154. '近7天综合达成率' => '0%'
  155. ];
  156. $i = 1;
  157. foreach ($dateMap as $date => $label) {
  158. $actual = isset($summary[$date]) ? round($summary[$date]['实际产量'], 2) : 0;
  159. $plan = isset($summary[$date]) ? round($summary[$date]['排产产量'], 2) : 0;
  160. // 重新计算达成率,不使用之前累加的百分比
  161. $rate = ($plan > 0) ? round($actual / $plan * 100, 2) . '%' : '0%';
  162. $item[$label] = $rate;
  163. $item["实际产量{$i}"] = $actual;
  164. $item["排产产量{$i}"] = $plan;
  165. $item['实际总产量'] += $actual;
  166. $item['排产总产量'] += $plan;
  167. $i++;
  168. }
  169. // 修正“近7天综合达成率”的计算
  170. $item['近7天综合达成率'] = ($item['排产总产量'] > 0)
  171. ? round($item['实际总产量'] / $item['排产总产量'] * 100, 2) . '%'
  172. : '0%';
  173. $results[] = $item;
  174. }
  175. $this->success('成功', [
  176. 'data' => $results,
  177. 'list' => $recentDates
  178. ]);
  179. }
  180. /**
  181. * 1.3达成率统计->机台生产详情
  182. */
  183. public function Machine_Detail()
  184. {
  185. if (!$this->request->isGet()) {
  186. $this->error('请求方式错误');
  187. }
  188. $param = $this->request->param();
  189. if (empty($param['jtbh']) || empty($param['bz'])) {
  190. // $this->error('缺少参数:jtbh(机台编号)或 bz(班组)');
  191. }
  192. $jtbh = $param['jtbh'];
  193. $bz = $param['bz'];
  194. // 查询近 7 天产量记录
  195. $records = Db::name('设备_产量计酬')->alias('a')
  196. ->field('
  197. a.sczl_rq as 日期,
  198. a.sczl_jtbh as 机台编号,
  199. a.sczl_bzdh as 班组,
  200. a.sczl_gdbh as 工单编号,
  201. a.sczl_yjno as 印件号,
  202. b.yj_yjmc as 印件名称,
  203. a.sczl_gxh as 工序号,
  204. a.sczl_gxmc as 工序名称,
  205. a.sczl_cl as 实际产量,
  206. c.排单小时定额 as 排产标准产能,
  207. d.Gy0_计划接货数 as 排产产量,
  208. a.sczl_设备运行工时 as 上报运行工时,
  209. a.sczl_装版总工时 as 装版实际工时,
  210. a.sczl_保养工时 as 保养工时,
  211. a.sczl_打样总工时 as 打样工时,
  212. d.版距 as 版距,
  213. c.设备名称 as 机台名称
  214. ')
  215. ->join('工单_印件资料 b','a.sczl_gdbh = b.Yj_Gdbh')
  216. ->join('设备_基本资料 c','a.sczl_jtbh = c.设备编号')
  217. ->join('工单_工艺资料 d','a.sczl_gdbh = d.Gy0_gdbh AND a.sczl_yjno = d.Gy0_yjno AND a.sczl_gxh = d.Gy0_gxh')
  218. ->where('a.sczl_jtbh', $jtbh)
  219. ->where('a.sczl_bzdh', $bz)
  220. ->where('a.sczl_rq', '>=', date('Y-m-d 00:00:00', strtotime('-6 days')))
  221. ->where('a.sczl_rq', '<=', date('Y-m-d 23:59:59'))
  222. ->order('a.sczl_rq desc')
  223. ->select();
  224. $details = [];
  225. foreach ($records as $row) {
  226. $rq = date('Y-m-d', strtotime($row['日期']));
  227. $details[] = [
  228. '日期' => $rq,
  229. '机台编号' => $row['机台编号'],
  230. '班组' => $row['班组'],
  231. '工单编号' => $row['工单编号'],
  232. '印件号' => $row['印件号'],
  233. '印件名称' => $row['印件名称'],
  234. '工序号' => $row['工序号'],
  235. '工序名称' => $row['工序名称'],
  236. '实际产量' => floatval($row['实际产量']),
  237. '排产标准产能' => $row['排产标准产能'],
  238. '排产产量' => floatval($row['排产产量']),
  239. '上报运行工时' => $row['上报运行工时'],
  240. '装版实际工时' => $row['装版实际工时'],
  241. '保养工时' => $row['保养工时'],
  242. '打样工时' => $row['打样工时'],
  243. '版距' => $row['版距'],
  244. '机台名称' => $row['机台名称'],
  245. ];
  246. }
  247. $this->success('明细获取成功', $details);
  248. }
  249. /**
  250. * 1.4达成率统计 -> 按月份汇总机台产量(按班组分组)
  251. */
  252. public function Machine_production_list()
  253. {
  254. if (!$this->request->isGet()) {
  255. $this->error('请求方式错误');
  256. }
  257. $param = $this->request->param();
  258. if (empty($param['Machine']) || empty($param['rq'])) {
  259. $this->error('参数错误');
  260. }
  261. $machine = $param['Machine'];
  262. $rq = $param['rq'];
  263. $ym = substr($rq, 0, 4) . '-' . substr($rq, 4, 2);
  264. // 查询设备编号
  265. $devices = Db::name('设备_基本资料')
  266. ->where('使用部门', $machine)
  267. ->column('设备编号');
  268. $devices = array_map('trim', $devices);
  269. // 查询产量记录
  270. $records = Db::name('设备_产量计酬')
  271. ->field([
  272. 'sczl_jtbh as 机台编号',
  273. 'sczl_bzdh as 班组',
  274. 'sczl_rq',
  275. 'sczl_cl',
  276. 'sczl_ls',
  277. 'sczl_rq',
  278. 'sczl_装版总工时 as 装板实际工时',
  279. 'sczl_装版工时 as 装板补产工时',
  280. 'sczl_保养工时 as 保养工时',
  281. 'sczl_打样总工时 as 打样总工时',
  282. 'sczl_打样工时 as 打样补产工时',
  283. 'sczl_异常停机工时 as 异常总工时',
  284. 'sczl_异常工时1 as 异常补时',
  285. 'sczl_设备运行工时 as 运行工时'
  286. ])
  287. ->whereIn('sczl_jtbh', $devices)
  288. ->where('sczl_rq', 'like', $ym . '%')
  289. ->select();
  290. $小时产能 = 50000;
  291. // 分组汇总:按 机台编号 + 班组
  292. $resultList = [];
  293. foreach ($records as $row) {
  294. $jtbh = $row['机台编号'];
  295. $bz = $row['班组'] ?: '未分组';
  296. $key = $jtbh . '|' . $bz;
  297. if (!isset($resultList[$key])) {
  298. $resultList[$key] = [
  299. '机台编号' => $jtbh,
  300. '班组' => $bz,
  301. '实际产量' => 0,
  302. '装板实际工时' => 0,
  303. '装板补产工时' => 0,
  304. '保养工时' => 0,
  305. '打样总工时' => 0,
  306. '打样补产工时' => 0,
  307. '异常总工时' => 0,
  308. '异常补时' => 0,
  309. '运行工时' => 0,
  310. 'sczl_rq' => date('Ym', strtotime($row['sczl_rq'])),
  311. // 新增字段
  312. '目标产量' => 0,
  313. '负荷产量' => 0,
  314. ];
  315. }
  316. // 累加字段
  317. $resultList[$key]['实际产量'] += floatval($row['sczl_cl']);
  318. $resultList[$key]['装板实际工时'] += floatval($row['装板实际工时']);
  319. $resultList[$key]['装板补产工时'] += floatval($row['装板补产工时']);
  320. $resultList[$key]['保养工时'] += floatval($row['保养工时']);
  321. $resultList[$key]['打样总工时'] += floatval($row['打样总工时']);
  322. $resultList[$key]['打样补产工时'] += floatval($row['打样补产工时']);
  323. $resultList[$key]['异常总工时'] += floatval($row['异常总工时']);
  324. $resultList[$key]['异常补时'] += floatval($row['异常补时']);
  325. $resultList[$key]['运行工时'] += floatval($row['运行工时']);
  326. }
  327. // 汇总并计算目标/负荷产量及达成率
  328. $grouped = []; // [机台编号 => [班组数据...]]
  329. foreach ($resultList as $item) {
  330. $jtbh = $item['机台编号'];
  331. $grouped[$jtbh][] = $item;
  332. }
  333. $finalList = [];
  334. foreach ($grouped as $jtbh => $rows) {
  335. $sum = [
  336. '机台编号' => $jtbh,
  337. '班组' => '合计',
  338. '实际产量' => 0,
  339. '装板实际工时' => 0,
  340. '装板补产工时' => 0,
  341. '保养工时' => 0,
  342. '打样总工时' => 0,
  343. '打样补产工时' => 0,
  344. '异常总工时' => 0,
  345. '异常补时' => 0,
  346. '运行工时' => 0,
  347. '目标产量' => 0,
  348. '负荷产量' => 0,
  349. 'sczl_rq' => $rows[0]['sczl_rq'] ?? '',
  350. ];
  351. foreach ($rows as &$row) {
  352. // 计算目标 & 负荷产量
  353. $有效工时 = max(0, $row['运行工时'] - $row['保养工时'] - $row['装板补产工时'] - $row['异常总工时']);
  354. $row['目标产量'] = round($有效工时 * $小时产能, 2);
  355. $row['负荷产量'] = $row['目标产量'];
  356. // 计算达成率 & 利用率
  357. $row['目标达成'] = ($row['目标产量'] > 0)
  358. ? round($row['实际产量'] / $row['目标产量'] * 100, 2) . '%'
  359. : '0%';
  360. $row['综合利用率'] = ($row['负荷产量'] > 0)
  361. ? round($row['实际产量'] / $row['负荷产量'] * 100, 2) . '%'
  362. : '0%';
  363. $finalList[] = $row;
  364. // 合计累加
  365. $sum['实际产量'] += $row['实际产量'];
  366. $sum['装板实际工时'] += $row['装板实际工时'];
  367. $sum['装板补产工时'] += $row['装板补产工时'];
  368. $sum['保养工时'] += $row['保养工时'];
  369. $sum['打样总工时'] += $row['打样总工时'];
  370. $sum['打样补产工时'] += $row['打样补产工时'];
  371. $sum['异常总工时'] += $row['异常总工时'];
  372. $sum['异常补时'] += $row['异常补时'];
  373. $sum['运行工时'] += $row['运行工时'];
  374. $sum['目标产量'] += $row['目标产量'];
  375. $sum['负荷产量'] += $row['负荷产量'];
  376. }
  377. // 合计达成率
  378. $sum['目标达成'] = ($sum['目标产量'] > 0)
  379. ? round($sum['实际产量'] / $sum['目标产量'] * 100, 2) . '%'
  380. : '0%';
  381. $sum['综合利用率'] = ($sum['负荷产量'] > 0)
  382. ? round($sum['实际产量'] / $sum['负荷产量'] * 100, 2) . '%'
  383. : '0%';
  384. $finalList[] = $sum;
  385. }
  386. $this->success('汇总成功', $finalList);
  387. }
  388. /**
  389. * 1.5达成率统计 -> 按日期+工单统计生产明细
  390. */
  391. public function Machine_production_details()
  392. {
  393. if (!$this->request->isGet()) {
  394. $this->error('请求方式错误');
  395. }
  396. $param = $this->request->param();
  397. if (empty($param['jtbh']) || empty($param['rq'])) {
  398. $this->error('缺少必要参数:jtbh(机台编号)、rq(年月)');
  399. }
  400. $jtbh = $param['jtbh'];
  401. $rq = $param['rq'];
  402. $ym = substr($rq, 0, 4) . '-' . substr($rq, 4, 2);
  403. // 查询该月份内该机台的每日工单产量明细
  404. $records = Db::name('设备_产量计酬')->alias('a')
  405. ->field([
  406. 'a.sczl_jtbh as 机台编号',
  407. 'a.sczl_rq as 日期',
  408. 'a.sczl_bzdh as 班组',
  409. 'a.sczl_gdbh as 工单编号',
  410. 'b.yj_yjmc as 印件名称',
  411. 'a.sczl_yjno as 印件号',
  412. 'a.sczl_gxh as 工序号',
  413. 'a.sczl_gxmc as 工序名称',
  414. 'a.sczl_ls',
  415. 'SUM(a.sczl_cl) as 实际产量',
  416. 'SUM(a.sczl_装版总工时) as 装版实际工时',
  417. 'SUM(a.sczl_装版工时) as 装版补产工时',
  418. 'SUM(a.sczl_保养工时) as 保养工时',
  419. 'SUM(a.sczl_打样总工时) as 打样总工时',
  420. 'SUM(a.sczl_打样工时) as 打样补产工时',
  421. 'SUM(a.sczl_异常停机工时) as 异常总工时',
  422. 'SUM(a.sczl_异常工时1) as 异常补时',
  423. 'SUM(a.sczl_设备运行工时) as 运行工时',
  424. 'c.排单小时定额 as 小时产能',
  425. 'c.平均车速'
  426. ])
  427. ->join('工单_印件资料 b', 'a.sczl_gdbh = b.Yj_Gdbh AND a.sczl_yjno = b.Yj_YjNo', 'LEFT') // 添加印件号关联条件
  428. ->join('设备_基本资料 c', 'a.sczl_jtbh = c.设备编号')
  429. ->where('a.sczl_jtbh', $jtbh)
  430. ->whereLike('a.sczl_rq', $ym . '%')
  431. ->group('a.sczl_rq, a.sczl_gdbh, a.sczl_yjno, a.sczl_gxh, a.sczl_jtbh ,a.sczl_bzdh') // 确保唯一性分组
  432. ->order('a.sczl_rq desc, a.sczl_bzdh')
  433. ->select();
  434. // 查询每个班组的运行工时
  435. $Operating_hours = \db('设备_产量计酬')
  436. ->field('sczl_rq as 日期,sczl_bzdh as 班组,sum(sczl_设备运行工时) as 设备运行工时')
  437. ->where('sczl_jtbh', $jtbh)
  438. ->whereLike('sczl_rq', $ym . '%')
  439. ->group('sczl_rq, sczl_bzdh')
  440. ->order('sczl_rq desc, sczl_bzdh')
  441. ->select();
  442. // 设备运行达成率设备运行工时计算规则:
  443. // 1.上报通电工时>8小时,设备运行工时=上报通电工时-1小时
  444. // 2.上报通电工时<8小时且上报通电工时>4小时,设备运行工时= 上报通电工时-0.5小时
  445. // 3.上报通电工时<4小时,设备运行工时 = 上报通电工时
  446. foreach ($Operating_hours as $k => $v) {
  447. if ($v['设备运行工时'] >= 8) {
  448. $Operating_hours[$k]['设备运行工时'] = $v['设备运行工时'] - 1;
  449. }elseif ($v['设备运行工时'] < 8 && $v['设备运行工时'] > 4) {
  450. $Operating_hours[$k]['设备运行工时'] = $v['设备运行工时'] - 0.5;
  451. }else{
  452. $Operating_hours[$k]['设备运行工时'] = $v['设备运行工时'];
  453. }
  454. }
  455. // 数据格式化与目标产量计算
  456. foreach ($records as $key => $row) {
  457. $records[$key]['日期'] = date('Y-m-d', strtotime($row['日期']));
  458. $records[$key]['印件工序'] = $row['印件号'] . '-' . $row['工序名称'];
  459. // 胶印设备换型工时(装版补产工时)计算规则:
  460. //1.按班次工单分开计算装版补产工时,基础计算规则:装版补产工时*1.7
  461. //2.当装版补产工时*1.7<实际装版工时*1.5时,按照装版补产工时*1.7计算
  462. //3.当装版补产工时*1.7>实际装版工时*1.5,按照实际装版工时*1.5计算
  463. //4.当装版补产工时>实际装版工时*1.5,按照装版补产工时计算
  464. if (strpos($row['机台编号'],'YJY') !== false) {
  465. $plate_hours = $row['装版补产工时'] * 1.7;
  466. $plate_mounting_hours = $row['装版实际工时'] * 1.5;
  467. if ($plate_hours < $plate_mounting_hours) {
  468. $records[$key]['装版补产工时'] = $plate_hours;
  469. }elseif ($plate_hours > $plate_mounting_hours) {
  470. $records[$key]['装版补产工时'] = $plate_mounting_hours;
  471. }elseif ($row['装版补产工时'] > $plate_mounting_hours) {
  472. $records[$key]['装版补产工时'] = $row['装版补产工时'];
  473. }else{
  474. $records[$key]['装版补产工时'] = $plate_hours;
  475. }
  476. }
  477. // 计算目标产量
  478. foreach ($Operating_hours as $k => $v) {
  479. if ($v['日期'] === $row['日期'] && $v['班组'] === $row['班组']) {
  480. //重新赋值运行工时
  481. $records[$key]['运行工时'] = $v['设备运行工时'];
  482. //计算目标产量
  483. $records[$key]['目标产量'] = round(
  484. max(0, $v['设备运行工时'] - $row['保养工时'] - $row['装版补产工时'] - $row['异常补时'] - $row['打样补产工时']) * $row['小时产能'],
  485. 2
  486. );
  487. // 计算负荷产量
  488. $records[$key]['负荷产量'] = round(
  489. max(0, $v['设备运行工时'] - $row['保养工时'] - $row['异常补时'] - $row['打样补产工时'] ) * $row['平均车速'],
  490. 2
  491. );
  492. // 计算目标达成率
  493. $records[$key]['目标达成'] = $records[$key]['目标产量'] > 0
  494. ? round($row['实际产量'] / $records[$key]['目标产量'] * 100, 2) . '%'
  495. : '0%';
  496. // 计算综合利用率
  497. $records[$key]['综合利用率'] = $records[$key]['负荷产量'] > 0
  498. ? round($row['实际产量'] / $records[$key]['负荷产量'] * 100, 2) . '%'
  499. : '0%';
  500. }
  501. }
  502. }
  503. $this->success('明细获取成功', $records);
  504. }
  505. /**
  506. * 1.6达成率统计->按时段导出Excel
  507. */
  508. public function Machine_date_excel()
  509. {
  510. if (!$this->request->isGet()) {
  511. $this->error('请求方式错误');
  512. }
  513. $param = $this->request->param();
  514. if (empty($param['start_rq']) || empty($param['end_rq']) || empty($param['bm'])) {
  515. $this->error('缺少参数:start_rq(开始日期)或 end_rq(结束日期)或 bm(部门)');
  516. }
  517. $start_rq = $param['start_rq'];
  518. $end_rq = $param['end_rq'];
  519. $bm = $param['bm'];
  520. // 1. 先查询设备_基本资料表,获取该部门下的所有设备编号(去重)
  521. $deviceIds = Db::name('设备_基本资料')
  522. ->where('使用部门', $bm)
  523. ->group('设备编号') // 去重
  524. ->column('设备编号');
  525. if (empty($deviceIds)) {
  526. $this->error('该部门下没有设备数据');
  527. }
  528. // 2. 查询设备_产量计酬表,筛选符合条件的记录
  529. $records = Db::name('设备_产量计酬')->alias('a')
  530. ->field([
  531. 'a.sczl_jtbh as 机台编号',
  532. 'a.sczl_rq as 日期',
  533. 'a.sczl_bzdh as 班组',
  534. 'a.sczl_gdbh as 工单编号',
  535. 'b.yj_yjmc as 印件名称',
  536. 'a.sczl_yjno as 印件号',
  537. 'a.sczl_gxh as 工序号',
  538. 'a.sczl_gxmc as 工序名称',
  539. 'a.sczl_ls',
  540. 'SUM(a.sczl_cl) as 实际产量',
  541. 'sczl_装版总工时 as 装板实际工时',
  542. 'sczl_装版工时 as 装板补产工时',
  543. 'sczl_保养工时 as 保养工时',
  544. 'sczl_打样总工时 as 打样总工时',
  545. 'sczl_打样工时 as 打样补产工时',
  546. 'sczl_异常停机工时 as 异常总工时',
  547. 'sczl_异常工时1 as 异常补时',
  548. 'sczl_设备运行工时 as 运行工时'
  549. ])
  550. ->join('工单_印件资料 b', 'a.sczl_gdbh = b.Yj_Gdbh')
  551. ->whereIn('a.sczl_jtbh', $deviceIds) // 使用设备编号列表筛选
  552. ->whereBetween('a.sczl_rq', [
  553. $start_rq . ' 00:00:00',
  554. $end_rq . ' 23:59:59'
  555. ])
  556. ->group('a.sczl_jtbh, a.sczl_rq, a.sczl_bzdh, a.sczl_gdbh') // 按设备、日期、班组、工单分组
  557. ->order('a.sczl_rq desc')
  558. ->select();
  559. // 格式化数据
  560. foreach ($records as &$row) {
  561. $row['日期'] = date('Y-m-d', strtotime($row['日期']));
  562. $row['印件工序'] = $row['印件号'] . '-' . $row['工序名称'];
  563. }
  564. $this->success('明细获取成功', $records);
  565. }
  566. /**
  567. * 01.产品年度投入产出率统计->左侧菜单
  568. */
  569. // public function left_Productyear() {
  570. // $list = \db('产品_基本资料')
  571. // ->field('客户编号,客户名称,Mod_rq')
  572. // ->group('客户编号,客户名称,Mod_rq')
  573. // ->where('客户编号','<>','')
  574. // ->order('客户编号')
  575. // ->select();
  576. //
  577. //
  578. // $data['翌星工单'] = [];
  579. // $data['MN工单'] = [];
  580. //
  581. // foreach ($list as $key => $value) {
  582. // $value['客户编号'] = rtrim($value['客户编号']);
  583. // $value['客户名称'] = rtrim($value['客户名称']);
  584. // if (empty($value['客户名称'])) {
  585. // continue;
  586. // }
  587. //
  588. // $name = $value['客户编号'] . '【' . $value['客户名称'] . '】';
  589. // $year = date('Y', strtotime($value['Mod_rq']));
  590. // $uniqueKey = $value['客户编号'] . '_' . $value['客户名称'] . '_' . $year;
  591. //
  592. // if (isset($seenEntries[$uniqueKey])) {
  593. // continue; // 如果已经存在,跳过重复项
  594. // }
  595. // $seenEntries[$uniqueKey] = true;
  596. //
  597. // if (strpos($value['客户编号'], 'J') !== false || strpos($value['客户编号'], 'Y') !== false) {
  598. // if (!isset($data['翌星工单'][$year])) {
  599. // $data['翌星工单'][$year] = [];
  600. // }
  601. // array_push($data['翌星工单'][$year], $name);
  602. // } else {
  603. // if (!isset($data['MN工单'][$year])) {
  604. // $data['MN工单'][$year] = [];
  605. // }
  606. // array_push($data['MN工单'][$year], $name);
  607. // }
  608. // }
  609. //
  610. // // 按年份排序(从最新到最旧)
  611. // krsort($data['翌星工单']);
  612. // krsort($data['MN工单']);
  613. //
  614. // // 对每个年份内的数据进行排序(可选)
  615. // foreach ($data['翌星工单'] as $year => &$items) {
  616. // sort($items);
  617. // }
  618. // foreach ($data['MN工单'] as $year => &$items) {
  619. // sort($items);
  620. // }
  621. // $this->success('成功', $data);
  622. // }
  623. public function left_Productyear() {
  624. $list = \db('产品_基本资料')
  625. ->field('客户编号,客户名称')
  626. ->group('客户编号,客户名称')
  627. ->where('客户编号','<>','')
  628. ->order('客户编号')
  629. ->select();
  630. $data['翌星工单'] = [];
  631. $data['MN工单'] = [];
  632. $seenEntries = []; // 添加这个变量来跟踪已处理的条目
  633. foreach ($list as $key => $value) {
  634. $value['客户编号'] = rtrim($value['客户编号']);
  635. $value['客户名称'] = rtrim($value['客户名称']);
  636. if (empty($value['客户名称'])) {
  637. continue;
  638. }
  639. // 获取工单信息,包含Sys_rq字段
  640. $productIng = \db('工单_基本资料')
  641. ->field('Sys_rq')
  642. ->where('Gd_cpdh|成品代号', 'LIKE', rtrim($value['客户编号']) . '%')
  643. ->order('Sys_rq DESC')
  644. ->find();
  645. // 如果没有找到相关工单,跳过
  646. if (!$productIng || empty($productIng['Sys_rq'])) {
  647. continue;
  648. }
  649. $name = $value['客户编号'] . '【' . $value['客户名称'] . '】';
  650. $year = date('Y', strtotime($productIng['Sys_rq']));
  651. $uniqueKey = $value['客户编号'] . '_' . $value['客户名称'] . '_' . $year;
  652. if (isset($seenEntries[$uniqueKey])) {
  653. continue; // 如果已经存在,跳过重复项
  654. }
  655. $seenEntries[$uniqueKey] = true;
  656. if (strpos($value['客户编号'], 'J') !== false || strpos($value['客户编号'], 'Y') !== false) {
  657. if (!isset($data['翌星工单'][$year])) {
  658. $data['翌星工单'][$year] = [];
  659. }
  660. array_push($data['翌星工单'][$year], $name);
  661. } else {
  662. if (!isset($data['MN工单'][$year])) {
  663. $data['MN工单'][$year] = [];
  664. }
  665. array_push($data['MN工单'][$year], $name);
  666. }
  667. }
  668. // 按年份排序(从最新到最旧)
  669. krsort($data['翌星工单']);
  670. krsort($data['MN工单']);
  671. // 对每个年份内的数据进行排序(可选)
  672. foreach ($data['翌星工单'] as $year => &$items) {
  673. sort($items);
  674. }
  675. foreach ($data['MN工单'] as $year => &$items) {
  676. sort($items);
  677. }
  678. $this->success('成功', $data);
  679. }
  680. /**
  681. * 01.产品年度投入产出率统计->数据汇总表
  682. */
  683. public function list_Productyear() {
  684. if ($this->request->isGet() === false) {
  685. $this->error('请求错误');
  686. }
  687. $param = $this->request->param();
  688. $rq = $param['rq'];
  689. $where = [];
  690. if (!empty($param['search'])){
  691. $where['产品代号|产品名称'] = ['like','%'.$param['search'].'%'];
  692. }
  693. if (!empty($param['khdh'])){
  694. $where['产品代号'] = ['like',$param['khdh'].'%'];
  695. }
  696. $qualityData = \db('工单_质量考核汇总')
  697. ->field('Gy0_gdbh, 客户代号, 入仓日期 as sys_rq, 实际投料, 入仓数量, 产品代号, 产品名称, 销售订单号, 订单数量,计量单位')
  698. ->where('YEAR(Sys_rq)', $rq)
  699. ->where($where)
  700. ->select();
  701. if (empty($qualityData)) {
  702. $this->success('未查询到数据', []);
  703. }
  704. // 第一步:按工单去重,每个工单只取一条记录
  705. $uniqueByGongdan = [];
  706. foreach ($qualityData as $data) {
  707. $gdbh = $data['Gy0_gdbh'];
  708. if (!isset($uniqueByGongdan[$gdbh])) {
  709. $uniqueByGongdan[$gdbh] = $data;
  710. }
  711. }
  712. // 第二步:按产品代号和月份分组并计算总和
  713. $productMap = [];
  714. foreach ($uniqueByGongdan as $gdbh => $data) {
  715. $productCode = $data['产品代号'];
  716. $sysRq = $data['sys_rq'];
  717. // 提取月份1-12 的月份数字
  718. $month = date('n', strtotime($sysRq));
  719. if (!isset($productMap[$productCode])) {
  720. $productMap[$productCode] = [
  721. 'sys_rq' => substr($data['sys_rq'], 0, 4),
  722. '计量单位' => $data['计量单位'],
  723. '成品编码' => $data['产品代号'],
  724. '成品名称' => $data['产品名称'],
  725. 'months' => [],
  726. '实际投料' => 0,
  727. '入仓数量' => 0,
  728. '工单列表' => [] // 记录包含的工单编号
  729. ];
  730. }
  731. if (!isset($productMap[$productCode]['months'][$month])) {
  732. $productMap[$productCode]['months'][$month] = [
  733. '实际投料' => 0,
  734. '入仓数量' => 0
  735. ];
  736. }
  737. //实际投料先乘以10000再累加
  738. $adjustedActual = round($data['实际投料'] * 10000);
  739. // 累加月度数据
  740. $productMap[$productCode]['months'][$month]['实际投料'] += $adjustedActual;
  741. $productMap[$productCode]['months'][$month]['入仓数量'] += $data['入仓数量'];
  742. // 累加年度总量(实际投料已经乘以10000)
  743. $productMap[$productCode]['实际投料'] += $adjustedActual;
  744. $productMap[$productCode]['入仓数量'] += $data['入仓数量'];
  745. // 记录工单编号
  746. $productMap[$productCode]['工单列表'][] = $data['Gy0_gdbh'];
  747. }
  748. //月份
  749. $monthNames = [
  750. 1 => '1月', 2 => '2月', 3 => '3月', 4 => '4月',
  751. 5 => '5月', 6 => '6月', 7 => '7月', 8 => '8月',
  752. 9 => '9月', 10 => '10月', 11 => '11月', 12 => '12月'
  753. ];
  754. //合并数据并计算合格率
  755. $result = [];
  756. foreach ($productMap as $productCode => $productData) {
  757. $resultItem = [
  758. 'sys_rq' => $productData['sys_rq'],
  759. '成品编码' => $productData['成品编码'],
  760. '成品名称' => $productData['成品名称'],
  761. '实际投料' => $productData['实际投料'],
  762. '入仓数量' => $productData['入仓数量'],
  763. '计量单位' => $productData['计量单位'],
  764. '工单数量' => count($productData['工单列表']),
  765. '工单列表' => implode(', ', $productData['工单列表'])
  766. ];
  767. // 初始化各月份数据
  768. foreach ($monthNames as $monthNum => $monthName) {
  769. $resultItem[$monthName] = '-';
  770. }
  771. // 处理该产品的月度数据
  772. if (isset($productData['months'])) {
  773. foreach ($productData['months'] as $month => $monthData) {
  774. $adjustedActual = $monthData['实际投料'];
  775. $delivery = $monthData['入仓数量'];
  776. // 计算月度合格率
  777. if ($adjustedActual > 0) {
  778. $monthlyRate = round(($delivery / $adjustedActual) * 100, 2);
  779. $resultItem[$monthNames[$month]] = $monthlyRate . '%';
  780. } else if ($delivery > 0) {
  781. $resultItem[$monthNames[$month]] = '100%';
  782. } else {
  783. $resultItem[$monthNames[$month]] = '0%';
  784. }
  785. }
  786. }
  787. // 计算年度综合合格率
  788. if ($resultItem['实际投料'] > 0) {
  789. $overallRate = round(($resultItem['入仓数量'] / $resultItem['实际投料']) * 100, 2);
  790. $resultItem['综合合格率'] = $overallRate . '%';
  791. } else if ($resultItem['入仓数量'] > 0) {
  792. $resultItem['综合合格率'] = '100%';
  793. } else {
  794. $resultItem['综合合格率'] = '0%';
  795. }
  796. $result[] = $resultItem;
  797. }
  798. $this->success('成功', $result);
  799. }
  800. /**
  801. * 01.产品年度投入产出率统计->数据明细表
  802. */
  803. public function list_Productmonth() {
  804. if ($this->request->isGet() === false) {
  805. $this->error('请求错误');
  806. }
  807. $param = $this->request->param();
  808. $productCode = $param['product_code'] ?? '';
  809. $year = $param['year'] ?? date('Y');
  810. if (empty($productCode)) {
  811. $this->error('请提供成品编码');
  812. }
  813. // 1. 查询对应的工单数据
  814. $qualityData = \db('工单_质量考核汇总')
  815. ->field('Gy0_gdbh as 工单编号, 订单数量, 入仓日期, 实际投料, 入仓数量, 客户代号, 客户名称, 产品代号, 产品名称, 销售订单号')
  816. ->where('产品代号', $productCode)
  817. ->where('YEAR(入仓日期)', $year)
  818. ->order('入仓日期','desc')
  819. ->select();
  820. if (empty($qualityData)) {
  821. $this->success('该产品无质量考核数据', []);
  822. }
  823. // 2. 先按工单去重,每个工单只取一条记录
  824. $uniqueByGongdan = [];
  825. foreach ($qualityData as $data) {
  826. $gdbh = $data['工单编号'];
  827. if (!isset($uniqueByGongdan[$gdbh])) {
  828. $uniqueByGongdan[$gdbh] = $data;
  829. }
  830. }
  831. // 3. 按月份分组累计数据
  832. $monthlyData = [];
  833. $monthNames = [
  834. 1 => '1月', 2 => '2月', 3 => '3月', 4 => '4月',
  835. 5 => '5月', 6 => '6月', 7 => '7月', 8 => '8月',
  836. 9 => '9月', 10 => '10月', 11 => '11月', 12 => '12月'
  837. ];
  838. foreach ($uniqueByGongdan as $gdbh => $data) {
  839. $sysRq = $data['入仓日期'];
  840. $month = date('n', strtotime($sysRq)); // 1-12 的月份数字
  841. // 初始化月份数据
  842. if (!isset($monthlyData[$month])) {
  843. $monthlyData[$month] = [
  844. '月份' => $monthNames[$month],
  845. '订单数量' => 0,
  846. '实际投料' => 0,
  847. '入仓数量' => 0,
  848. '入仓日期' => $data['入仓日期'],
  849. '客户代号' => $data['客户代号'],
  850. '客户名称' => $data['客户名称'],
  851. '产品代号' => $data['产品代号'],
  852. '产品名称' => $data['产品名称'],
  853. '销售订单号' => $data['销售订单号']
  854. ];
  855. }
  856. $monthlyData[$month]['订单数量'] += round($data['订单数量'] * 10000);
  857. $monthlyData[$month]['实际投料'] += round($data['实际投料'] * 10000);
  858. $monthlyData[$month]['入仓数量'] += round($data['入仓数量']);
  859. // 记录工单编号
  860. $monthlyData[$month]['工单编号'][] = $data['工单编号'];
  861. }
  862. // 4. 计算每个月的实际合格率并整理结果
  863. $detailList = [];
  864. foreach ($monthlyData as $month => $monthData) {
  865. // 计算实际合格率
  866. $actualRate = '-';
  867. if ($monthData['实际投料'] > 0) {
  868. $actualRate = round(($monthData['入仓数量'] / $monthData['实际投料']) * 100, 2) . '%';
  869. } else if ($monthData['入仓数量'] > 0) {
  870. $actualRate = '100%';
  871. } else {
  872. $actualRate = '0%';
  873. }
  874. $detailList[$month] = [
  875. '日期' => $monthData['月份'],
  876. '入仓日期' => substr($monthData['入仓日期'], 0, 10),
  877. '订单数量' => $monthData['订单数量'],
  878. '实际投料' => $monthData['实际投料'],
  879. '入仓数量' => $monthData['入仓数量'],
  880. '实际合格率' => $actualRate,
  881. '客户代号' => $monthData['客户代号'],
  882. '客户名称' => $monthData['客户名称'],
  883. '产品代号' => $monthData['产品代号'],
  884. '产品名称' => $monthData['产品名称'],
  885. '销售订单号' => $monthData['销售订单号'],
  886. '工单编号' => implode(', ', $monthData['工单编号'])
  887. ];
  888. }
  889. $this->success('成功', array_values($detailList));
  890. }
  891. }