Achievementatestatistics.php 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014
  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. ->alias('a')
  272. ->field([
  273. 'a.sczl_jtbh as 机台编号',
  274. 'a.sczl_bzdh as 班组',
  275. 'a.sczl_rq',
  276. 'a.sczl_cl',
  277. 'a.sczl_ls',
  278. 'a.sczl_rq',
  279. 'a.sczl_装版总工时 as 装板实际工时',
  280. 'a.sczl_装版工时 as 装板补产工时',
  281. 'a.sczl_保养工时 as 保养工时',
  282. 'a.sczl_打样总工时 as 打样总工时',
  283. 'a.sczl_打样工时 as 打样补产工时',
  284. 'a.sczl_异常停机工时 as 异常总工时',
  285. 'a.sczl_异常工时1 as 异常补时',
  286. 'a.sczl_设备运行工时 as 运行工时',
  287. 'c.排单小时定额 as 小时产能'
  288. ])
  289. ->join('设备_基本资料 c', 'a.sczl_jtbh = c.设备编号')
  290. ->whereIn('a.sczl_jtbh', $devices)
  291. ->where('a.sczl_rq', 'like', $ym . '%')
  292. ->select();
  293. // 分组汇总:按 机台编号 + 班组
  294. $resultList = [];
  295. foreach ($records as $row) {
  296. $jtbh = $row['机台编号'];
  297. $bz = $row['班组'] ?: '未分组';
  298. $key = $jtbh . '|' . $bz;
  299. if (!isset($resultList[$key])) {
  300. $resultList[$key] = [
  301. '机台编号' => $jtbh,
  302. '班组' => $bz,
  303. '实际产量' => 0,
  304. '装板实际工时' => 0,
  305. '装板补产工时' => 0,
  306. '保养工时' => 0,
  307. '打样总工时' => 0,
  308. '打样补产工时' => 0,
  309. '异常总工时' => 0,
  310. '异常补时' => 0,
  311. '运行工时' => 0,
  312. 'sczl_rq' => date('Ym', strtotime($row['sczl_rq'])),
  313. // 新增字段
  314. '目标产量' => 0,
  315. '负荷产量' => 0,
  316. '小时产能' => $row['小时产能']
  317. ];
  318. }
  319. // 累加字段
  320. $resultList[$key]['实际产量'] += floatval($row['sczl_cl']);
  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. $resultList[$key]['异常总工时'] += floatval($row['异常总工时']);
  327. $resultList[$key]['异常补时'] += floatval($row['异常补时']);
  328. $resultList[$key]['运行工时'] += floatval($row['运行工时']);
  329. }
  330. // 汇总并计算目标/负荷产量及达成率
  331. $grouped = []; // [机台编号 => [班组数据...]]
  332. foreach ($resultList as $item) {
  333. $jtbh = $item['机台编号'];
  334. $grouped[$jtbh][] = $item;
  335. }
  336. $finalList = [];
  337. foreach ($grouped as $jtbh => $rows) {
  338. $sum = [
  339. '机台编号' => $jtbh,
  340. '班组' => '合计',
  341. '实际产量' => 0,
  342. '装板实际工时' => 0,
  343. '装板补产工时' => 0,
  344. '保养工时' => 0,
  345. '打样总工时' => 0,
  346. '打样补产工时' => 0,
  347. '异常总工时' => 0,
  348. '异常补时' => 0,
  349. '运行工时' => 0,
  350. '目标产量' => 0,
  351. '负荷产量' => 0,
  352. 'sczl_rq' => $rows[0]['sczl_rq'] ?? '',
  353. ];
  354. foreach ($rows as &$row) {
  355. // 计算目标 & 负荷产量
  356. $有效工时 = max(0, $row['运行工时'] - $row['保养工时'] - $row['装板补产工时'] - $row['异常总工时']);
  357. $row['目标产量'] = round($有效工时 * $row['小时产能'], 2);
  358. $row['负荷产量'] = $row['目标产量'];
  359. // 计算达成率 & 利用率
  360. $row['目标达成'] = ($row['目标产量'] > 0)
  361. ? round($row['实际产量'] / $row['目标产量'] * 100, 2) . '%'
  362. : '0%';
  363. $row['综合利用率'] = ($row['负荷产量'] > 0)
  364. ? round($row['实际产量'] / $row['负荷产量'] * 100, 2) . '%'
  365. : '0%';
  366. $finalList[] = $row;
  367. // 合计累加
  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. $sum['运行工时'] += $row['运行工时'];
  377. $sum['目标产量'] += $row['目标产量'];
  378. $sum['负荷产量'] += $row['负荷产量'];
  379. }
  380. // 合计达成率
  381. $sum['目标达成'] = ($sum['目标产量'] > 0)
  382. ? round($sum['实际产量'] / $sum['目标产量'] * 100, 2) . '%'
  383. : '0%';
  384. $sum['综合利用率'] = ($sum['负荷产量'] > 0)
  385. ? round($sum['实际产量'] / $sum['负荷产量'] * 100, 2) . '%'
  386. : '0%';
  387. $finalList[] = $sum;
  388. }
  389. $this->success('汇总成功', $finalList);
  390. }
  391. /**
  392. * 1.5达成率统计 -> 按日期+工单统计生产明细
  393. */
  394. public function Machine_production_details()
  395. {
  396. if (!$this->request->isGet()) {
  397. $this->error('请求方式错误');
  398. }
  399. $param = $this->request->param();
  400. if (empty($param['jtbh']) || empty($param['rq'])) {
  401. $this->error('缺少必要参数:jtbh(机台编号)、rq(年月)');
  402. }
  403. $jtbh = $param['jtbh'];
  404. $rq = $param['rq'];
  405. $ym = substr($rq, 0, 4) . '-' . substr($rq, 4, 2);
  406. // 查询该月份内该机台的每日工单产量明细
  407. $records = Db::name('设备_产量计酬')->alias('a')
  408. ->field([
  409. 'a.sczl_jtbh as 机台编号',
  410. 'a.sczl_rq as 日期',
  411. 'a.sczl_bzdh as 班组',
  412. 'a.sczl_gdbh as 工单编号',
  413. 'b.yj_yjmc as 印件名称',
  414. 'a.sczl_yjno as 印件号',
  415. 'a.sczl_gxh as 工序号',
  416. 'a.sczl_gxmc as 工序名称',
  417. 'a.sczl_ls',
  418. 'SUM(a.sczl_cl) as 实际产量',
  419. 'SUM(a.sczl_装版总工时) as 装版实际工时',
  420. 'SUM(a.sczl_装版工时) as 装版补产工时',
  421. 'SUM(a.sczl_保养工时) as 保养工时',
  422. 'SUM(a.sczl_打样总工时) as 打样总工时',
  423. 'SUM(a.sczl_打样工时) as 打样补产工时',
  424. 'SUM(a.sczl_异常停机工时) as 异常总工时',
  425. 'SUM(a.sczl_异常工时1) as 异常补时',
  426. 'SUM(a.sczl_设备运行工时) as 运行工时',
  427. 'c.排单小时定额 as 小时产能',
  428. 'c.平均车速'
  429. ])
  430. ->join('工单_印件资料 b', 'a.sczl_gdbh = b.Yj_Gdbh AND a.sczl_yjno = b.Yj_YjNo', 'LEFT') // 添加印件号关联条件
  431. ->join('设备_基本资料 c', 'a.sczl_jtbh = c.设备编号')
  432. ->where('a.sczl_jtbh', $jtbh)
  433. ->whereLike('a.sczl_rq', $ym . '%')
  434. ->group('a.sczl_rq, a.sczl_gdbh, a.sczl_yjno, a.sczl_gxh, a.sczl_jtbh ,a.sczl_bzdh') // 确保唯一性分组
  435. ->order('a.sczl_rq desc, a.sczl_bzdh')
  436. ->select();
  437. // 查询每个班组的运行工时
  438. $Operating_hours = \db('设备_产量计酬')
  439. ->field('sczl_rq as 日期,sczl_bzdh as 班组,sum(sczl_设备运行工时) as 设备运行工时')
  440. ->where('sczl_jtbh', $jtbh)
  441. ->whereLike('sczl_rq', $ym . '%')
  442. ->group('sczl_rq, sczl_bzdh')
  443. ->order('sczl_rq desc, sczl_bzdh')
  444. ->select();
  445. // 设备运行达成率设备运行工时计算规则:
  446. // 1.上报通电工时>8小时,设备运行工时=上报通电工时-1小时
  447. // 2.上报通电工时<8小时且上报通电工时>4小时,设备运行工时= 上报通电工时-0.5小时
  448. // 3.上报通电工时<4小时,设备运行工时 = 上报通电工时
  449. foreach ($Operating_hours as $k => $v) {
  450. if ($v['设备运行工时'] >= 8) {
  451. $Operating_hours[$k]['设备运行工时'] = $v['设备运行工时'] - 1;
  452. }elseif ($v['设备运行工时'] < 8 && $v['设备运行工时'] > 4) {
  453. $Operating_hours[$k]['设备运行工时'] = $v['设备运行工时'] - 0.5;
  454. }else{
  455. $Operating_hours[$k]['设备运行工时'] = $v['设备运行工时'];
  456. }
  457. }
  458. // 数据格式化与目标产量计算
  459. foreach ($records as $key => $row) {
  460. $records[$key]['日期'] = date('Y-m-d', strtotime($row['日期']));
  461. $records[$key]['印件工序'] = $row['印件号'] . '-' . $row['工序名称'];
  462. // 胶印设备换型工时(装版补产工时)计算规则:
  463. //1.按班次工单分开计算装版补产工时,基础计算规则:装版补产工时*1.7
  464. //2.当装版补产工时*1.7<实际装版工时*1.5时,按照装版补产工时*1.7计算
  465. //3.当装版补产工时*1.7>实际装版工时*1.5,按照实际装版工时*1.5计算
  466. //4.当装版补产工时>实际装版工时*1.5,按照装版补产工时计算
  467. if (strpos($row['机台编号'],'YJY') !== false) {
  468. $plate_hours = $row['装版补产工时'] * 1.7;
  469. $plate_mounting_hours = $row['装版实际工时'] * 1.5;
  470. if ($plate_hours < $plate_mounting_hours) {
  471. $records[$key]['装版补产工时'] = $plate_hours;
  472. }elseif ($plate_hours > $plate_mounting_hours) {
  473. $records[$key]['装版补产工时'] = $plate_mounting_hours;
  474. }elseif ($row['装版补产工时'] > $plate_mounting_hours) {
  475. $records[$key]['装版补产工时'] = $row['装版补产工时'];
  476. }else{
  477. $records[$key]['装版补产工时'] = $plate_hours;
  478. }
  479. }
  480. // 计算目标产量
  481. foreach ($Operating_hours as $k => $v) {
  482. if ($v['日期'] === $row['日期'] && $v['班组'] === $row['班组']) {
  483. //重新赋值运行工时
  484. $records[$key]['运行工时'] = $v['设备运行工时'];
  485. //计算目标产量
  486. $records[$key]['目标产量'] = round(
  487. max(0, $v['设备运行工时'] - $row['保养工时'] - $row['装版补产工时'] - $row['异常补时'] - $row['打样补产工时']) * $row['小时产能'],
  488. 2
  489. );
  490. // 计算负荷产量
  491. $records[$key]['负荷产量'] = round(
  492. max(0, $v['设备运行工时'] - $row['保养工时'] - $row['异常补时'] - $row['打样补产工时'] ) * $row['平均车速'],
  493. 2
  494. );
  495. // 计算目标达成率
  496. $records[$key]['目标达成'] = $records[$key]['目标产量'] > 0
  497. ? round($row['实际产量'] / $records[$key]['目标产量'] * 100, 2) . '%'
  498. : '0%';
  499. // 计算综合利用率
  500. $records[$key]['综合利用率'] = $records[$key]['负荷产量'] > 0
  501. ? round($row['实际产量'] / $records[$key]['负荷产量'] * 100, 2) . '%'
  502. : '0%';
  503. }
  504. }
  505. }
  506. $this->success('明细获取成功', $records);
  507. }
  508. /**
  509. * 1.6达成率统计->按时段导出Excel
  510. */
  511. public function Machine_date_excel()
  512. {
  513. if (!$this->request->isGet()) {
  514. $this->error('请求方式错误');
  515. }
  516. $param = $this->request->param();
  517. if (empty($param['start_rq']) || empty($param['end_rq']) || empty($param['bm'])) {
  518. $this->error('缺少参数:start_rq(开始日期)或 end_rq(结束日期)或 bm(部门)');
  519. }
  520. $start_rq = $param['start_rq'];
  521. $end_rq = $param['end_rq'];
  522. $bm = $param['bm'];
  523. // 1. 先查询设备_基本资料表,获取该部门下的所有设备编号(去重)
  524. $deviceIds = Db::name('设备_基本资料')
  525. ->where('使用部门', $bm)
  526. ->group('设备编号') // 去重
  527. ->column('设备编号');
  528. if (empty($deviceIds)) {
  529. $this->error('该部门下没有设备数据');
  530. }
  531. // 2. 查询设备_产量计酬表,筛选符合条件的记录
  532. $records = Db::name('设备_产量计酬')->alias('a')
  533. ->field([
  534. 'a.sczl_jtbh as 机台编号',
  535. 'a.sczl_rq as 日期',
  536. 'a.sczl_bzdh as 班组',
  537. 'a.sczl_gdbh as 工单编号',
  538. 'b.yj_yjmc as 印件名称',
  539. 'a.sczl_yjno as 印件号',
  540. 'a.sczl_gxh as 工序号',
  541. 'a.sczl_gxmc as 工序名称',
  542. 'a.sczl_ls',
  543. 'SUM(a.sczl_cl) as 实际产量',
  544. 'sczl_装版总工时 as 装板实际工时',
  545. 'sczl_装版工时 as 装板补产工时',
  546. 'sczl_保养工时 as 保养工时',
  547. 'sczl_打样总工时 as 打样总工时',
  548. 'sczl_打样工时 as 打样补产工时',
  549. 'sczl_异常停机工时 as 异常总工时',
  550. 'sczl_异常工时1 as 异常补时',
  551. 'sczl_设备运行工时 as 运行工时'
  552. ])
  553. ->join('工单_印件资料 b', 'a.sczl_gdbh = b.Yj_Gdbh')
  554. ->whereIn('a.sczl_jtbh', $deviceIds) // 使用设备编号列表筛选
  555. ->whereBetween('a.sczl_rq', [
  556. $start_rq . ' 00:00:00',
  557. $end_rq . ' 23:59:59'
  558. ])
  559. ->group('a.sczl_jtbh, a.sczl_rq, a.sczl_bzdh, a.sczl_gdbh') // 按设备、日期、班组、工单分组
  560. ->order('a.sczl_rq desc')
  561. ->select();
  562. // 格式化数据
  563. foreach ($records as &$row) {
  564. $row['日期'] = date('Y-m-d', strtotime($row['日期']));
  565. $row['印件工序'] = $row['印件号'] . '-' . $row['工序名称'];
  566. }
  567. $this->success('明细获取成功', $records);
  568. }
  569. /**
  570. * 01.产品年度投入产出率统计->左侧菜单
  571. */
  572. // public function left_Productyear() {
  573. // $list = \db('产品_基本资料')
  574. // ->field('客户编号,客户名称,Mod_rq')
  575. // ->group('客户编号,客户名称,Mod_rq')
  576. // ->where('客户编号','<>','')
  577. // ->order('客户编号')
  578. // ->select();
  579. //
  580. //
  581. // $data['翌星工单'] = [];
  582. // $data['MN工单'] = [];
  583. //
  584. // foreach ($list as $key => $value) {
  585. // $value['客户编号'] = rtrim($value['客户编号']);
  586. // $value['客户名称'] = rtrim($value['客户名称']);
  587. // if (empty($value['客户名称'])) {
  588. // continue;
  589. // }
  590. //
  591. // $name = $value['客户编号'] . '【' . $value['客户名称'] . '】';
  592. // $year = date('Y', strtotime($value['Mod_rq']));
  593. // $uniqueKey = $value['客户编号'] . '_' . $value['客户名称'] . '_' . $year;
  594. //
  595. // if (isset($seenEntries[$uniqueKey])) {
  596. // continue; // 如果已经存在,跳过重复项
  597. // }
  598. // $seenEntries[$uniqueKey] = true;
  599. //
  600. // if (strpos($value['客户编号'], 'J') !== false || strpos($value['客户编号'], 'Y') !== false) {
  601. // if (!isset($data['翌星工单'][$year])) {
  602. // $data['翌星工单'][$year] = [];
  603. // }
  604. // array_push($data['翌星工单'][$year], $name);
  605. // } else {
  606. // if (!isset($data['MN工单'][$year])) {
  607. // $data['MN工单'][$year] = [];
  608. // }
  609. // array_push($data['MN工单'][$year], $name);
  610. // }
  611. // }
  612. //
  613. // // 按年份排序(从最新到最旧)
  614. // krsort($data['翌星工单']);
  615. // krsort($data['MN工单']);
  616. //
  617. // // 对每个年份内的数据进行排序(可选)
  618. // foreach ($data['翌星工单'] as $year => &$items) {
  619. // sort($items);
  620. // }
  621. // foreach ($data['MN工单'] as $year => &$items) {
  622. // sort($items);
  623. // }
  624. // $this->success('成功', $data);
  625. // }
  626. public function left_Productyear() {
  627. $list = \db('产品_基本资料')
  628. ->field('客户编号,客户名称')
  629. ->group('客户编号,客户名称')
  630. ->where('客户编号','<>','')
  631. ->order('客户编号')
  632. ->select();
  633. $data['翌星工单'] = [];
  634. $data['MN工单'] = [];
  635. $seenEntries = []; // 添加这个变量来跟踪已处理的条目
  636. foreach ($list as $key => $value) {
  637. $value['客户编号'] = rtrim($value['客户编号']);
  638. $value['客户名称'] = rtrim($value['客户名称']);
  639. if (empty($value['客户名称'])) {
  640. continue;
  641. }
  642. // 获取工单信息,包含Sys_rq字段
  643. $productIng = \db('工单_基本资料')
  644. ->field('Sys_rq')
  645. ->where('Gd_cpdh|成品代号', 'LIKE', rtrim($value['客户编号']) . '%')
  646. ->order('Sys_rq DESC')
  647. ->find();
  648. // 如果没有找到相关工单,跳过
  649. if (!$productIng || empty($productIng['Sys_rq'])) {
  650. continue;
  651. }
  652. $name = $value['客户编号'] . '【' . $value['客户名称'] . '】';
  653. $year = date('Y', strtotime($productIng['Sys_rq']));
  654. $uniqueKey = $value['客户编号'] . '_' . $value['客户名称'] . '_' . $year;
  655. if (isset($seenEntries[$uniqueKey])) {
  656. continue; // 如果已经存在,跳过重复项
  657. }
  658. $seenEntries[$uniqueKey] = true;
  659. if (strpos($value['客户编号'], 'J') !== false || strpos($value['客户编号'], 'Y') !== false) {
  660. if (!isset($data['翌星工单'][$year])) {
  661. $data['翌星工单'][$year] = [];
  662. }
  663. array_push($data['翌星工单'][$year], $name);
  664. } else {
  665. if (!isset($data['MN工单'][$year])) {
  666. $data['MN工单'][$year] = [];
  667. }
  668. array_push($data['MN工单'][$year], $name);
  669. }
  670. }
  671. // 按年份排序(从最新到最旧)
  672. krsort($data['翌星工单']);
  673. krsort($data['MN工单']);
  674. // 对每个年份内的数据进行排序(可选)
  675. foreach ($data['翌星工单'] as $year => &$items) {
  676. sort($items);
  677. }
  678. foreach ($data['MN工单'] as $year => &$items) {
  679. sort($items);
  680. }
  681. $this->success('成功', $data);
  682. }
  683. /**
  684. * 01.产品年度投入产出率统计->数据汇总表
  685. */
  686. public function list_Productyear() {
  687. if ($this->request->isGet() === false) {
  688. $this->error('请求错误');
  689. }
  690. $param = $this->request->param();
  691. $rq = $param['rq'];
  692. $where = [];
  693. if (!empty($param['search'])){
  694. $where['产品代号|产品名称'] = ['like','%'.$param['search'].'%'];
  695. }
  696. if (!empty($param['khdh'])){
  697. $where['产品代号'] = ['like',$param['khdh'].'%'];
  698. }
  699. $qualityData = \db('工单_质量考核汇总')
  700. ->field('Gy0_gdbh, 客户代号, 入仓日期 as sys_rq, 实际投料, 入仓数量, 产品代号, 产品名称, 销售订单号, 订单数量,计量单位')
  701. ->where('YEAR(Sys_rq)', $rq)
  702. ->where($where)
  703. ->select();
  704. if (empty($qualityData)) {
  705. $this->success('未查询到数据', []);
  706. }
  707. // 第一步:按工单去重,每个工单只取一条记录
  708. $uniqueByGongdan = [];
  709. foreach ($qualityData as $data) {
  710. $gdbh = $data['Gy0_gdbh'];
  711. if (!isset($uniqueByGongdan[$gdbh])) {
  712. $uniqueByGongdan[$gdbh] = $data;
  713. }
  714. }
  715. // 第二步:按产品代号和月份分组并计算总和
  716. $productMap = [];
  717. foreach ($uniqueByGongdan as $gdbh => $data) {
  718. $productCode = $data['产品代号'];
  719. $sysRq = $data['sys_rq'];
  720. // 提取月份1-12 的月份数字
  721. $month = date('n', strtotime($sysRq));
  722. if (!isset($productMap[$productCode])) {
  723. $productMap[$productCode] = [
  724. 'sys_rq' => substr($data['sys_rq'], 0, 4),
  725. '计量单位' => $data['计量单位'],
  726. '成品编码' => $data['产品代号'],
  727. '成品名称' => $data['产品名称'],
  728. 'months' => [],
  729. '实际投料' => 0,
  730. '入仓数量' => 0,
  731. '工单列表' => [] // 记录包含的工单编号
  732. ];
  733. }
  734. if (!isset($productMap[$productCode]['months'][$month])) {
  735. $productMap[$productCode]['months'][$month] = [
  736. '实际投料' => 0,
  737. '入仓数量' => 0
  738. ];
  739. }
  740. //实际投料先乘以10000再累加
  741. $adjustedActual = round($data['实际投料'] * 10000);
  742. // 累加月度数据
  743. $productMap[$productCode]['months'][$month]['实际投料'] += $adjustedActual;
  744. $productMap[$productCode]['months'][$month]['入仓数量'] += $data['入仓数量'];
  745. // 累加年度总量(实际投料已经乘以10000)
  746. $productMap[$productCode]['实际投料'] += $adjustedActual;
  747. $productMap[$productCode]['入仓数量'] += $data['入仓数量'];
  748. // 记录工单编号
  749. $productMap[$productCode]['工单列表'][] = $data['Gy0_gdbh'];
  750. }
  751. //月份
  752. $monthNames = [
  753. 1 => '1月', 2 => '2月', 3 => '3月', 4 => '4月',
  754. 5 => '5月', 6 => '6月', 7 => '7月', 8 => '8月',
  755. 9 => '9月', 10 => '10月', 11 => '11月', 12 => '12月'
  756. ];
  757. //合并数据并计算合格率
  758. $result = [];
  759. foreach ($productMap as $productCode => $productData) {
  760. $resultItem = [
  761. 'sys_rq' => $productData['sys_rq'],
  762. '成品编码' => $productData['成品编码'],
  763. '成品名称' => $productData['成品名称'],
  764. '实际投料' => $productData['实际投料'],
  765. '入仓数量' => $productData['入仓数量'],
  766. '计量单位' => $productData['计量单位'],
  767. '工单数量' => count($productData['工单列表']),
  768. '工单列表' => implode(', ', $productData['工单列表'])
  769. ];
  770. // 初始化各月份数据
  771. foreach ($monthNames as $monthNum => $monthName) {
  772. $resultItem[$monthName] = '-';
  773. }
  774. // 处理该产品的月度数据
  775. if (isset($productData['months'])) {
  776. foreach ($productData['months'] as $month => $monthData) {
  777. $adjustedActual = $monthData['实际投料'];
  778. $delivery = $monthData['入仓数量'];
  779. // 计算月度合格率
  780. if ($adjustedActual > 0) {
  781. $monthlyRate = round(($delivery / $adjustedActual) * 100, 2);
  782. $resultItem[$monthNames[$month]] = $monthlyRate . '%';
  783. } else if ($delivery > 0) {
  784. $resultItem[$monthNames[$month]] = '100%';
  785. } else {
  786. $resultItem[$monthNames[$month]] = '0%';
  787. }
  788. }
  789. }
  790. // 计算年度综合合格率
  791. if ($resultItem['实际投料'] > 0) {
  792. $overallRate = round(($resultItem['入仓数量'] / $resultItem['实际投料']) * 100, 2);
  793. $resultItem['综合合格率'] = $overallRate . '%';
  794. } else if ($resultItem['入仓数量'] > 0) {
  795. $resultItem['综合合格率'] = '100%';
  796. } else {
  797. $resultItem['综合合格率'] = '0%';
  798. }
  799. $result[] = $resultItem;
  800. }
  801. $this->success('成功', $result);
  802. }
  803. /**
  804. * 01.产品年度投入产出率统计->数据明细表
  805. */
  806. public function list_Productmonth() {
  807. if ($this->request->isGet() === false) {
  808. $this->error('请求错误');
  809. }
  810. $param = $this->request->param();
  811. $productCode = $param['product_code'] ?? '';
  812. $year = $param['year'] ?? date('Y');
  813. if (empty($productCode)) {
  814. $this->error('请提供成品编码');
  815. }
  816. // 1. 查询对应的工单数据
  817. $qualityData = \db('工单_质量考核汇总')
  818. ->field('Gy0_gdbh as 工单编号, 订单数量, 入仓日期, 实际投料, 入仓数量, 客户代号, 客户名称, 产品代号, 产品名称, 销售订单号')
  819. ->where('产品代号', $productCode)
  820. ->where('YEAR(入仓日期)', $year)
  821. ->order('入仓日期','desc')
  822. ->select();
  823. if (empty($qualityData)) {
  824. $this->success('该产品无质量考核数据', []);
  825. }
  826. // 2. 先按工单去重,每个工单只取一条记录
  827. $uniqueByGongdan = [];
  828. foreach ($qualityData as $data) {
  829. $gdbh = $data['工单编号'];
  830. if (!isset($uniqueByGongdan[$gdbh])) {
  831. $uniqueByGongdan[$gdbh] = $data;
  832. }
  833. }
  834. // 3. 按月份分组累计数据
  835. $monthlyData = [];
  836. $monthNames = [
  837. 1 => '1月', 2 => '2月', 3 => '3月', 4 => '4月',
  838. 5 => '5月', 6 => '6月', 7 => '7月', 8 => '8月',
  839. 9 => '9月', 10 => '10月', 11 => '11月', 12 => '12月'
  840. ];
  841. foreach ($uniqueByGongdan as $gdbh => $data) {
  842. $sysRq = $data['入仓日期'];
  843. $month = date('n', strtotime($sysRq)); // 1-12 的月份数字
  844. // 初始化月份数据
  845. if (!isset($monthlyData[$month])) {
  846. $monthlyData[$month] = [
  847. '月份' => $monthNames[$month],
  848. '订单数量' => 0,
  849. '实际投料' => 0,
  850. '入仓数量' => 0,
  851. '入仓日期' => $data['入仓日期'],
  852. '客户代号' => $data['客户代号'],
  853. '客户名称' => $data['客户名称'],
  854. '产品代号' => $data['产品代号'],
  855. '产品名称' => $data['产品名称'],
  856. '销售订单号' => $data['销售订单号']
  857. ];
  858. }
  859. $monthlyData[$month]['订单数量'] += round($data['订单数量'] * 10000);
  860. $monthlyData[$month]['实际投料'] += round($data['实际投料'] * 10000);
  861. $monthlyData[$month]['入仓数量'] += round($data['入仓数量']);
  862. // 记录工单编号
  863. $monthlyData[$month]['工单编号'][] = $data['工单编号'];
  864. }
  865. // 4. 计算每个月的实际合格率并整理结果
  866. $detailList = [];
  867. foreach ($monthlyData as $month => $monthData) {
  868. // 计算实际合格率
  869. $actualRate = '-';
  870. if ($monthData['实际投料'] > 0) {
  871. $actualRate = round(($monthData['入仓数量'] / $monthData['实际投料']) * 100, 2) . '%';
  872. } else if ($monthData['入仓数量'] > 0) {
  873. $actualRate = '100%';
  874. } else {
  875. $actualRate = '0%';
  876. }
  877. $detailList[$month] = [
  878. '日期' => $monthData['月份'],
  879. '入仓日期' => substr($monthData['入仓日期'], 0, 10),
  880. '订单数量' => $monthData['订单数量'],
  881. '实际投料' => $monthData['实际投料'],
  882. '入仓数量' => $monthData['入仓数量'],
  883. '实际合格率' => $actualRate,
  884. '客户代号' => $monthData['客户代号'],
  885. '客户名称' => $monthData['客户名称'],
  886. '产品代号' => $monthData['产品代号'],
  887. '产品名称' => $monthData['产品名称'],
  888. '销售订单号' => $monthData['销售订单号'],
  889. '工单编号' => implode(', ', $monthData['工单编号'])
  890. ];
  891. }
  892. $this->success('成功', array_values($detailList));
  893. }
  894. }