ReportFormsService.php 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892
  1. <?php
  2. namespace App\Service;
  3. use App\Model\Employee;
  4. use App\Model\OrdersProduct;
  5. use App\Model\OrdersProductProcess;
  6. use App\Model\Scrapp;
  7. use App\Model\SystemL;
  8. use App\Model\Team;
  9. use Illuminate\Support\Facades\DB;
  10. /**
  11. * 设备相关设置报表
  12. * Class ReportFormsService
  13. * @package App\Service
  14. */
  15. class ReportFormsService extends Service
  16. {
  17. /**
  18. * 生产进度
  19. * @param $data
  20. * @return array
  21. */
  22. public function productionReport($data){
  23. if(empty($data['production_time'][0]) || empty($data['production_time'][1])) return [false, '生产订单时间必须选择!'];
  24. //检索条件 生产订单主表----------------
  25. $model = OrdersProduct::where('del_time',0)->select('production_time','production_no','out_order_no','out_order_no_time','customer_no','customer_name','table_header_mark','order_quantity','production_quantity');
  26. $model->whereBetween('production_time',[$data['production_time'][0],$data['production_time'][1]]);
  27. if(! empty($data['production_no'])) $model->where('production_no', 'LIKE', '%'.$data['production_no'].'%');
  28. if(! empty($data['out_order_no_time'][0]) && ! empty($data['out_order_no_time'][1])) $model->whereBetween('out_order_no_time',[$data['out_order_no_time'][0],$data['out_order_no_time'][1]]);
  29. if(! empty($data['out_order_no'])) $model->where('out_order_no', 'LIKE', '%'.$data['out_order_no'].'%');
  30. if(! empty($data['customer_no'])) $model->where('customer_no', 'LIKE', '%'.$data['customer_no'].'%');
  31. if(! empty($data['customer_name'])) $model->where('customer_name', 'LIKE', '%'.$data['customer_name'].'%');
  32. $orderList = $model->get()->toArray();
  33. //生产订单主表----------------
  34. //筛选出制单日期 分表的依据
  35. $out_order_no_time = array_unique(array_column($orderList,'out_order_no_time'));
  36. //制单日期
  37. $out_time = $this->checkSameQuarter($out_order_no_time);
  38. //分组以后的订单列表
  39. $list = [];
  40. foreach ($orderList as $value){
  41. if(! isset($list[$value['production_no']])){
  42. $list[$value['production_no']] = $value;
  43. }else{
  44. $list[$value['production_no']]['order_quantity'] += $value['order_quantity'];
  45. $list[$value['production_no']]['production_quantity'] += $value['production_quantity'];
  46. }
  47. }unset($orderList);
  48. //查询分表数据
  49. $production_no = array_column($list,'production_no');
  50. $detail = [];
  51. $process_id = $data['process_id'] ?? [];
  52. foreach ($out_time as $value){
  53. //子表搜索
  54. $models = new OrdersProductProcess(['channel' => $value]);
  55. $tmp = $models->whereIn('production_no',$production_no)
  56. ->where('del_time',0)
  57. ->whereIn('status',array(1,2))
  58. ->when(!empty($process_id), function ($query) use ($process_id) {
  59. return $query->whereIn('process_id', $process_id);
  60. })
  61. ->select('production_no','process_id',DB::raw('SUM(CASE WHEN status >= 1 THEN 1 ELSE 0 END) as dispatch_count'),DB::raw('SUM(CASE WHEN status = 2 THEN 1 ELSE 0 END) as finish_count'))
  62. ->groupBy('production_no','process_id')
  63. ->get()->toArray();//派工和完工数据
  64. foreach ($tmp as $t){
  65. $keys = $t['production_no'] . "|" .$t['process_id'];
  66. if(isset($detail[$keys])){
  67. $detail[$keys]['dispatch_count'] += $t['dispatch_count'];
  68. $detail[$keys]['finish_count'] += $t['finish_count'];
  69. }else{
  70. $detail[$keys] = $t;
  71. }
  72. }
  73. }
  74. //返回统计数据
  75. foreach ($list as $key => $value) {
  76. $list[$key]['production_time'] = $value['production_time'] ? date('Y-m-d',$value['production_time']) : '';
  77. $list[$key]['out_order_no_time'] = $value['out_order_no_time'] ? date('Y-m-d',$value['out_order_no_time']) : '';
  78. $detail_key = $value['production_no'] . "|";
  79. foreach ($detail as $key_son => $value_son) {
  80. if (strpos($key_son,$detail_key) !== false) {
  81. $dispatch_count = bcdiv($value_son['dispatch_count'],1000,3);
  82. $finish_count = bcdiv($value_son['finish_count'],1000,3);
  83. $value_son['dispatch_count'] = $dispatch_count;
  84. $value_son['finish_count'] = $finish_count;
  85. $value_son['rate'] = number_format($value_son['finish_count'] / $value['order_quantity'] * 100, 2);
  86. $list[$key]['process'][] = $value_son;
  87. }
  88. }
  89. if(empty($list[$key]['process'])) unset($list[$key]);
  90. }
  91. return [true,array_values($list)];
  92. }
  93. private function checkSameQuarter($timestamps) {
  94. $quarters = $out_time = [];
  95. foreach ($timestamps as $timestamp) {
  96. $date = date('Ym', $timestamp);
  97. $year = intval(date('Y', $timestamp));
  98. $quarter = ceil(intval(date('n', $timestamp)) / 3);
  99. if(! isset($quarters[$year]) || ! in_array($quarter,$quarters[$year])){
  100. $quarters[$year][] = $quarter;
  101. $out_time[] = $date;
  102. }
  103. }
  104. return $out_time;
  105. }
  106. /**
  107. * 班组
  108. * @param $data
  109. * @return array
  110. */
  111. public function teamReport($data){
  112. if(empty($data['finish_time'][0]) || empty($data['finish_time'][1])) return [false, '完工时间必须选择!'];
  113. //班组
  114. $team_id = $data['team_id'] ?? [];
  115. //根据完工时间 扩大时间戳范围前后三个月
  116. $new_finish_time = $this->increaseTimeArea($data['finish_time']);
  117. //根据时间戳范围 获取分表的时间
  118. $return_time = $this->getTimeAreaData($new_finish_time);
  119. //检索分表数据
  120. $batchSize = 1000; // 每次处理的数据量
  121. $result = $team = [];
  122. foreach ($return_time as $value){
  123. $offset = 0;
  124. $hasMoreData = true;
  125. while ($hasMoreData) {
  126. // 子表搜索
  127. $models = new OrdersProductProcess(['channel' => $value]);
  128. $tmp = $models->where('del_time', 0)
  129. ->whereBetween('finished_time', [$data['finish_time'][0], $data['finish_time'][1]])
  130. ->where('status', 2)
  131. ->when(!empty($team_id), function ($query) use ($team_id) {
  132. return $query->whereIn('team_id', $team_id);
  133. })
  134. ->skip($offset) // 跳过前面的数据
  135. ->take($batchSize) // 获取固定数量的数据
  136. ->select('team_id', 'finished_time', 'production_no')
  137. ->get()->toArray(); // 完工数据
  138. if (empty($tmp)) {
  139. $hasMoreData = false;
  140. } else {
  141. $result = array_merge_recursive($result, $tmp);
  142. $team = array_merge_recursive($team, array_unique(array_column($tmp, 'team_id')));
  143. $offset += $batchSize;
  144. }
  145. }
  146. // //子表搜索
  147. // $models = new OrdersProductProcess(['channel' => $value]);
  148. // $tmp = $models->where('del_time',0)
  149. // ->whereBetween('finished_time',[$data['finish_time'][0], $data['finish_time'][1]])
  150. // ->where('status',2)
  151. // ->when(!empty($team_id), function ($query) use ($team_id) {
  152. // return $query->whereIn('team_id', $team_id);
  153. // })
  154. // ->select('team_id','finished_time','production_no')
  155. // ->get()->toArray();//完工数据
  156. // $result = array_merge_recursive($result,$tmp);
  157. // $team = array_merge_recursive($team,array_unique(array_column($tmp,'team_id')));
  158. }
  159. if(empty($result)) return [true , []];
  160. //组织数据
  161. $team_map = Team::whereIn('id',array_unique($team))
  162. ->pluck('title','id')
  163. ->toArray();
  164. $return_team = $return_team_time_tmp = $return_team_time= [];
  165. foreach ($result as $value){
  166. if(isset($return_team[$value['team_id']])){
  167. $return_team[$value['team_id']]['num'] += 1;
  168. if(! in_array($value['production_no'], $return_team[$value['team_id']]['production_no'])) {
  169. $return_team[$value['team_id']]['production_no'] = array_merge_recursive($return_team[$value['team_id']]['production_no'],[$value['production_no']]);
  170. }
  171. }else{
  172. $return_team[$value['team_id']] = [
  173. 'num' => 1,
  174. 'team_name' => $team_map[$value['team_id']] ?? '',
  175. 'production_no' => [$value['production_no']]
  176. ];
  177. }
  178. $tmp = date("Y-m-d",$value['finished_time']);
  179. if(isset($return_team_time_tmp[$tmp][$value['team_id']])){
  180. $return_team_time_tmp[$tmp][$value['team_id']]['num'] += 1;
  181. }else{
  182. $return_team_time_tmp[$tmp][$value['team_id']] = [
  183. 'time' => $tmp,
  184. 'num' => 1,
  185. 'team_name' => $team_map[$value['team_id']] ?? '',
  186. ];
  187. }
  188. }ksort($return_team_time_tmp);unset($result);
  189. $all_team_map = Team::where('del_time',0)
  190. ->pluck('title','id')
  191. ->toArray();
  192. foreach ($return_team_time_tmp as $key => $value){
  193. $t_k = array_keys($value);
  194. foreach ($all_team_map as $k => $v){
  195. if(! in_array($k,$t_k)){
  196. $return_team_time_tmp[$key][$k] = [
  197. 'time' => $key,
  198. 'num' => 0,
  199. 'team_name' => $v,
  200. ];
  201. }
  202. }
  203. ksort($return_team_time_tmp[$key]);
  204. $tmp = [];
  205. $tmp['time'] = $key;
  206. $tmp['sub'] = array_values($return_team_time_tmp[$key]);
  207. $return_team_time[] = $tmp;
  208. }unset($return_team_time_tmp);
  209. foreach ($return_team as $key => $value){
  210. $return_team[$key]['num'] = bcdiv($value['num'],1000,3);
  211. }
  212. foreach ($return_team_time as $key => $value){
  213. foreach ($value['sub'] as $k => $v){
  214. $return_team_time[$key]['sub'][$k]['num'] = bcdiv($v['num'],1000,3);
  215. }
  216. }
  217. //列表数据 图表数据
  218. return [true,['list'=>array_values($return_team),'chart'=>$return_team_time]];
  219. }
  220. /**
  221. * 时间特殊处理
  222. * @param $time_area
  223. * @return array
  224. */
  225. private function increaseTimeArea($time_area){
  226. // 增加三个月的时间戳
  227. $newStartTimestamp = strtotime('-3 months', $time_area[0]);
  228. $newEndTimestamp = strtotime('+3 months', $time_area[1]);
  229. return [$time_area[0],$time_area[1]];
  230. }
  231. /**
  232. * 获取时间区间数据
  233. * @param $time_area
  234. * @return array
  235. */
  236. private function getTimeAreaData($time_area){
  237. $startYear = date('Y', $time_area[0]);
  238. $endYear = date('Y', $time_area[1]);
  239. $return = [];
  240. for ($year = $startYear; $year <= $endYear; $year++) {
  241. for ($quarter = 1; $quarter <= 4; $quarter++) {
  242. $quarterStart = strtotime($year . '-' . (($quarter - 1) * 3 + 1) . '-01');
  243. $quarterEnd = strtotime($year . '-' . ($quarter * 3) . '-01') - 1;
  244. if ($quarterStart <= $time_area[1] && $quarterEnd >= $time_area[0]) {
  245. // $tmp = $year . sprintf('%02d', $quarter);//年季度
  246. $return[] = $year .sprintf('%02d',($quarter - 1) * 3 + 1);
  247. }
  248. }
  249. }
  250. return $return;
  251. }
  252. /**
  253. * 班组 详情
  254. * @param $data
  255. * @return array
  256. */
  257. public function teamReportDetail($data){
  258. if(empty($data['production_no'])) return [false,'生产订单号不能为空!'];
  259. $list = OrdersProduct::whereIn('production_no',$data['production_no'])
  260. ->select('production_time','production_no','customer_no','customer_name','table_header_mark','product_no','product_title','product_size','product_unit','dispatch_complete_quantity','finished_num','technology_material','technology_name','wood_name','process_mark','table_body_mark')
  261. ->get()->toArray();
  262. foreach ($list as $key => $value) {
  263. $list[$key]['production_time'] = $value['production_time'] ? date('Y-m-d',$value['production_time']) : '';
  264. }
  265. return [true,$list];
  266. }
  267. /**
  268. * 不良品
  269. * @param $data
  270. * @return array
  271. */
  272. public function badGoodsReport($data){
  273. if(empty($data['production_time'][0]) || empty($data['production_time'][1])) return [false, '生产订单时间必须选择!'];
  274. //检索条件 生产订单主表----------------
  275. $model = OrdersProduct::where('del_time',0)->select('production_time','production_no','out_order_no','out_order_no_time','customer_no','customer_name','table_header_mark','order_quantity','production_quantity','out_crt_man');
  276. $model->whereBetween('production_time',[$data['production_time'][0],$data['production_time'][1]]);
  277. if(! empty($data['production_no'])) $model->where('production_no', 'LIKE', '%'.$data['production_no'].'%');
  278. if(! empty($data['out_order_no_time'][0]) && ! empty($data['out_order_no_time'][1])) $model->whereBetween('out_order_no_time',[$data['out_order_no_time'][0],$data['out_order_no_time'][1]]);
  279. if(! empty($data['out_order_no'])) $model->where('out_order_no', 'LIKE', '%'.$data['out_order_no'].'%');
  280. if(! empty($data['customer_no'])) $model->where('customer_no', 'LIKE', '%'.$data['customer_no'].'%');
  281. if(! empty($data['customer_name'])) $model->where('customer_name', 'LIKE', '%'.$data['customer_name'].'%');
  282. if(! empty($data['out_crt_man'])) $model->where('out_crt_man', 'LIKE', '%'.$data['out_crt_man'].'%');
  283. $orderList = $model->get()->toArray();
  284. //生产订单主表----------------
  285. //筛选出制单日期 分表的依据
  286. $out_order_no_time = array_unique(array_column($orderList,'out_order_no_time'));
  287. //制单日期
  288. $out_time = $this->checkSameQuarter($out_order_no_time);
  289. //分组以后的订单列表
  290. $list = [];
  291. foreach ($orderList as $value){
  292. if(! isset($list[$value['production_no']])){
  293. $list[$value['production_no']] = $value;
  294. }else{
  295. $list[$value['production_no']]['order_quantity'] += $value['order_quantity'];
  296. $list[$value['production_no']]['production_quantity'] += $value['production_quantity'];
  297. }
  298. }unset($orderList);
  299. //查询分表数据
  300. $production_no = array_column($list,'production_no');
  301. $detail = [];
  302. foreach ($out_time as $value){
  303. //子表搜索
  304. $models = new OrdersProductProcess(['channel' => $value]);
  305. $tmp = $models->whereIn('production_no',$production_no)
  306. ->where('del_time',0)
  307. ->where('status',4)
  308. ->select('production_no',DB::raw('COUNT(id) as bad_goods_num'))
  309. ->groupBy('production_no')
  310. ->get()->toArray();//不良品数据
  311. foreach ($tmp as $t){
  312. if(isset($detail[$t['production_no']])){
  313. $detail[$t['production_no']]['bad_goods_num'] += $t['bad_goods_num'];
  314. }else{
  315. $detail[$t['production_no']] = $t;
  316. }
  317. }
  318. }
  319. //返回统计数据
  320. foreach ($list as $key => $value) {
  321. $list[$key]['production_time'] = $value['production_time'] ? date('Y-m-d',$value['production_time']) : '';
  322. $list[$key]['out_order_no_time'] = $value['out_order_no_time'] ? date('Y-m-d',$value['out_order_no_time']) : '';
  323. $del_num = $detail[$value['production_no']] ?? 0;
  324. $list[$key]['bad_goods_num'] = $del_num;
  325. $list[$key]['rate'] = number_format($del_num / $value['production_quantity'], 2);
  326. }
  327. return [true,array_values($list)];
  328. }
  329. /**
  330. * 不良品 详情
  331. * @param $data
  332. * @return array
  333. */
  334. public function badGoodsReportDetail($data){
  335. if(empty($data['production_no'])) return [false,'生产订单号不能为空!'];
  336. $list = OrdersProduct::where('production_no',$data['production_no'])
  337. ->select('id','production_time','production_no','out_order_no','out_order_no_time','customer_no','customer_name','table_header_mark','product_no','product_title','product_size','product_unit','dispatch_complete_quantity','finished_num','technology_material','technology_name','wood_name','process_mark','table_body_mark','out_crt_man','production_quantity','order_quantity')
  338. ->get()->toArray();
  339. //筛选出制单日期 分表的依据
  340. $out_order_no_time = array_unique(array_column($list,'out_order_no_time'));
  341. //制单日期
  342. $out_time = $this->checkSameQuarter($out_order_no_time);
  343. $detail = $detail_scrapp = [];
  344. foreach ($out_time as $value){
  345. //子表搜索
  346. $models = new OrdersProductProcess(['channel' => $value]);
  347. $tmp = $models->where('production_no',$data['production_no'])
  348. ->where('del_time',0)
  349. ->where('status',4)
  350. ->select('order_product_id',DB::raw('COUNT(id) as bad_goods_num'), DB::raw("GROUP_CONCAT(DISTINCT scrapp_id ORDER BY scrapp_id SEPARATOR ',') as scrapp_ids"))
  351. ->groupBy('order_product_id')
  352. ->get()
  353. ->toArray();//不良品数据
  354. foreach ($tmp as $v){
  355. if(isset($detail[$v['order_product_id']])){
  356. $detail[$v['order_product_id']] += $v['bad_goods_num'];
  357. }else{
  358. $detail[$v['order_product_id']] = $v['bad_goods_num'];
  359. }
  360. if(isset($detail_scrapp[$v['order_product_id']])){
  361. $detail_scrapp[$v['order_product_id']] .= "," . $v['scrapp_ids'];
  362. }else{
  363. $detail_scrapp[$v['order_product_id']] = $v['scrapp_ids'];
  364. }
  365. }
  366. }
  367. $scrapp_map = Scrapp::where('del_time',0)->pluck('title','id')->toArray();
  368. foreach ($list as $key => $value) {
  369. $list[$key]['production_time'] = $value['production_time'] ? date('Y-m-d',$value['production_time']) : '';
  370. $list[$key]['out_order_no_time'] = $value['out_order_no_time'] ? date('Y-m-d',$value['out_order_no_time']) : '';
  371. $list[$key]['bad_goods_num'] = $detail[$value['id']] ?? 0;
  372. $list[$key]['rate'] = number_format($list[$key]['bad_goods_num'] / $value['production_quantity'], 2);
  373. $scrapp = $detail_scrapp[$value['id']] ?? '';
  374. $tmp_str = '';
  375. if(! empty($scrapp)){
  376. $tmp = explode(',',$scrapp);
  377. foreach ($tmp as $vv){
  378. $tmp_str .= ($scrapp_map[$vv] ? $scrapp_map[$vv] . ',' : '');
  379. }
  380. }
  381. $list[$key]['scrapp_name'] = rtrim($tmp_str,',');
  382. }
  383. return [true, $list];
  384. }
  385. /**
  386. * 不良品原因
  387. * @param $data
  388. * @return array
  389. */
  390. public function badGoodsReasonReport($data){
  391. if(empty($data['production_time'][0]) || empty($data['production_time'][1])) return [false, '生产订单时间必须选择!'];
  392. //检索条件 生产订单主表----------------
  393. $model = OrdersProduct::where('del_time',0)->select('production_time','production_no','out_order_no','out_order_no_time','customer_no','customer_name','table_header_mark','order_quantity','production_quantity','out_crt_man');
  394. $model->whereBetween('production_time',[$data['production_time'][0],$data['production_time'][1]]);
  395. if(! empty($data['production_no'])) $model->where('production_no', 'LIKE', '%'.$data['production_no'].'%');
  396. if(! empty($data['out_order_no_time'][0]) && ! empty($data['out_order_no_time'][1])) $model->whereBetween('out_order_no_time',[$data['out_order_no_time'][0],$data['out_order_no_time'][1]]);
  397. if(! empty($data['out_order_no'])) $model->where('out_order_no', 'LIKE', '%'.$data['out_order_no'].'%');
  398. if(! empty($data['customer_no'])) $model->where('customer_no', 'LIKE', '%'.$data['customer_no'].'%');
  399. if(! empty($data['customer_name'])) $model->where('customer_name', 'LIKE', '%'.$data['customer_name'].'%');
  400. if(! empty($data['out_crt_man'])) $model->where('out_crt_man', 'LIKE', '%'.$data['out_crt_man'].'%');
  401. $orderList = $model->get()->toArray();
  402. //生产订单主表----------------
  403. //筛选出制单日期 分表的依据
  404. $out_order_no_time = array_unique(array_column($orderList,'out_order_no_time'));
  405. //制单日期
  406. $out_time = $this->checkSameQuarter($out_order_no_time);
  407. //分组以后的订单列表
  408. $list = [];
  409. foreach ($orderList as $value){
  410. if(! isset($list[$value['production_no']])){
  411. $list[$value['production_no']] = $value;
  412. }else{
  413. $list[$value['production_no']]['order_quantity'] += $value['order_quantity'];
  414. $list[$value['production_no']]['production_quantity'] += $value['production_quantity'];
  415. }
  416. }unset($orderList);
  417. //查询分表数据
  418. $production_no = array_column($list,'production_no');
  419. $detail = [];
  420. foreach ($out_time as $value){
  421. //子表搜索
  422. $models = new OrdersProductProcess(['channel' => $value]);
  423. $tmp = $models->whereIn('production_no',$production_no)
  424. ->where('del_time',0)
  425. ->where('status',4)
  426. ->select('production_no',DB::raw('COUNT(id) as bad_goods_num'))
  427. ->groupBy('production_no')
  428. ->get()->toArray();//不良品数据
  429. foreach ($tmp as $t){
  430. if(isset($detail[$t['production_no']])){
  431. $detail[$t['production_no']]['bad_goods_num'] += $t['bad_goods_num'];
  432. }else{
  433. $detail[$t['production_no']] = $t;
  434. }
  435. }
  436. }
  437. //返回统计数据
  438. foreach ($list as $key => $value) {
  439. $list[$key]['production_time'] = $value['production_time'] ? date('Y-m-d',$value['production_time']) : '';
  440. $list[$key]['out_order_no_time'] = $value['out_order_no_time'] ? date('Y-m-d',$value['out_order_no_time']) : '';
  441. $del_num = $detail[$value['production_no']] ?? 0;
  442. $list[$key]['bad_goods_num'] = $del_num;
  443. $list[$key]['rate'] = number_format($del_num / $value['production_quantity'], 2);
  444. }
  445. return [true,array_values($list)];
  446. }
  447. //不良品原因 详情
  448. public function badGoodsReasonReportDetail($data){
  449. if(empty($data['production_no'])) return [false,'生产订单号不能为空!'];
  450. $list = OrdersProduct::where('production_no',$data['production_no'])
  451. ->select('id','production_time','production_no','out_order_no','out_order_no_time','customer_no','customer_name','table_header_mark','product_no','product_title','product_size','product_unit','dispatch_complete_quantity','finished_num','technology_material','technology_name','wood_name','process_mark','table_body_mark','out_crt_man','production_quantity')
  452. ->get()->toArray();
  453. //筛选出制单日期 分表的依据
  454. $out_order_no_time = array_unique(array_column($list,'out_order_no_time'));
  455. //制单日期
  456. $out_time = $this->checkSameQuarter($out_order_no_time);
  457. $detail = $scrapp_id = $team = $man = [];
  458. foreach ($out_time as $value){
  459. //子表搜索
  460. $models = new OrdersProductProcess(['channel' => $value]);
  461. $tmp = $models->where('production_no',$data['production_no'])
  462. ->where('del_time',0)
  463. ->where('status',4)
  464. ->select('order_product_id','scrapp_id','team_id','finished_id',DB::raw('COUNT(id) as bad_goods_num'))
  465. ->groupBy('order_product_id','scrapp_id')
  466. ->get()->toArray();//不良品数据
  467. foreach ($tmp as $v){
  468. if(! in_array($v['scrapp_id'], $scrapp_id)) $scrapp_id[] = $v['scrapp_id'];
  469. if(! in_array($v['team_id'], $team)) $team[] = $v['team_id'];
  470. if(! in_array($v['finished_id'], $man)) $man[] = $v['finished_id'];
  471. if(isset($detail[$v['order_product_id']])){
  472. foreach ($detail[$v['order_product_id']] as $k => $d){
  473. if($d['scrapp_id'] == $v['scrapp_id']){
  474. $detail[$v['order_product_id']][$k]['bad_goods_num'] += $v['bad_goods_num'];
  475. }else{
  476. $detail[$v['order_product_id']][] = $v;
  477. }
  478. }
  479. }else{
  480. $detail[$v['order_product_id']][] = $v;
  481. }
  482. }
  483. }
  484. //次品原因 班组 人员
  485. $map = Scrapp::whereIn('id',$scrapp_id)
  486. ->pluck('title','id')
  487. ->toArray();
  488. $map1 = Team::whereIn('id',$team)
  489. ->pluck('title','id')
  490. ->toArray();
  491. $map2 = Employee::whereIn('id',$man)
  492. ->pluck('emp_name','id')
  493. ->toArray();
  494. foreach ($list as $key => $value) {
  495. $list[$key]['production_time'] = $value['production_time'] ? date('Y-m-d',$value['production_time']) : '';
  496. $list[$key]['out_order_no_time'] = $value['out_order_no_time'] ? date('Y-m-d',$value['out_order_no_time']) : '';
  497. $list[$key]['rate'] = number_format(($detail[$value['id']] ?? 0) / $value['production_quantity'], 2);
  498. $del_tmp = $detail[$value['id']] ?? [];
  499. foreach ($del_tmp as $dk => $dv){
  500. $del_tmp[$dk]['rate'] = number_format($dv['bad_goods_num'] / $value['production_quantity'], 2);
  501. $del_tmp[$dk]['scrapp_name'] = $map[$dv['scrapp_id']] ?? '';
  502. $del_tmp[$dk]['team_name'] = $map1[$dv['team_id']] ?? '';
  503. $del_tmp[$dk]['man_name'] = $map2[$dv['finished_id']] ?? '';
  504. }
  505. $list[$key]['bad_goods'] = $del_tmp;
  506. }
  507. return [true, $list];
  508. }
  509. //设备统计报表
  510. public function deviceStatisticsReport($data){
  511. if(empty($data['time'][0]) || empty($data['time'][1])) return [false, '时间必须选择!'];
  512. $model = SystemL::where('time','>=',$data['time'][0])
  513. ->where('time','<',$data['time'][1]);
  514. if(! empty($data['title'])) $model->whereIn('device_name',$data['title']);
  515. $result = $model->select('device_name','time','value','data_point_name')
  516. ->get()
  517. ->toArray();
  518. if(empty($result)) return [true,[]];
  519. $device_name = array_values(array_unique(array_column($result,'device_name')));
  520. //运行时间 工作时间 故障
  521. $run_time = $process_time = $fault = [];
  522. foreach ($result as $value){
  523. if($value['data_point_name'] == SystemL::run || $value['data_point_name'] == SystemL::standBy){
  524. //运行次数
  525. if(isset($run_time[$value['device_name']])){
  526. $run_time[$value['device_name']] += 1;
  527. }else{
  528. $run_time[$value['device_name']] = 1;
  529. }
  530. }
  531. if($value['data_point_name'] == SystemL::standBy){
  532. //工作次数
  533. if(isset($process_time[$value['device_name']])){
  534. $process_time[$value['device_name']] += 1;
  535. }else{
  536. $process_time[$value['device_name']] = 1;
  537. }
  538. }
  539. if($value['data_point_name'] == SystemL::stop){
  540. //设备故障次数
  541. if(isset($fault[$value['device_name']])){
  542. $fault[$value['device_name']] += 1;
  543. }else{
  544. $fault[$value['device_name']] = 1;
  545. }
  546. }
  547. }
  548. foreach ($device_name as $key => $value){
  549. //运行次数
  550. $run_num = $run_time[$value] ?? 0;
  551. //工作次数
  552. $process_num = $process_time[$value] ?? 0;
  553. //故障次数
  554. $fault_tmp = $fault[$value] ?? 0;
  555. //运行时间
  556. $run_time_tmp = $this->calTimeReturnMin($run_num);
  557. //工作时间
  558. $process_time_tmp = $this->calTimeReturnMin($process_num);
  559. //故障时间
  560. $fault_time_tmp = $this->calTimeReturnMin($fault_tmp);
  561. //待机时间
  562. $standby_time_tmp = number_format($run_time_tmp - $process_time_tmp,2);
  563. //计划运行时间 工作时间 - 计划停机 (没有计划停机)
  564. //实际运行时间 计划运行时间 -故障停机 - 设备调整(没有设备调整
  565. $true_process_time = $process_time_tmp - $fault_time_tmp;
  566. //有效率 实际/计划运行 时间
  567. $efficient = $process_time_tmp > 0 ? number_format($true_process_time / $process_time_tmp,2) : 0;
  568. //表现性 加工数量/实际运行时间
  569. $expressive = $true_process_time > 0 ? number_format($process_num / $true_process_time,2) : 0;
  570. //质量指数 加工数量- 废品数量 / 加工数量
  571. $quality_index = $process_num > 0 ? number_format(($process_num - $fault_tmp) / $process_num,2) : 0;
  572. //OEE
  573. $oee = number_format($efficient * $expressive * $quality_index,2);
  574. $device_name[$key] = [
  575. 'device_name' => $value,
  576. 'run_time' => $run_time_tmp,
  577. 'process_time' => $process_time_tmp,
  578. 'standby_time' => $standby_time_tmp,
  579. 'process_num' => $process_num,
  580. 'fault_num' => $fault_tmp,
  581. 'oee' => $oee,
  582. // 'e' => $efficient,
  583. // 'ex' => $expressive,
  584. // 'true_process_time' => $true_process_time,
  585. // 'qua' => $quality_index
  586. ];
  587. }
  588. return [true,$device_name];
  589. }
  590. public function deviceStatisticsReportDetail($data){
  591. if(empty($data['device_name']) || empty($data['time'][0]) || empty($data['time'][0])) return [false,'参数不能为空!'];
  592. $result = SystemL::where('time','>=',$data['time'][0])
  593. ->where('time','<',$data['time'][1])
  594. ->where('device_name',$data['device_name'])
  595. ->select('device_name','time','data_point_name')
  596. ->get()->toArray();
  597. $return = [
  598. 'run' => [],
  599. 'work' => [],
  600. 'stop' => [],
  601. 'stand_by' => [],
  602. ];
  603. foreach ($result as $key => $value){
  604. $time = date('Y-m-d H:i:s',$value['time'] / 1000);
  605. $stop_time = date('Y-m-d H:i:s',$value['time'] / 1000 + rand(10,40));
  606. if($value['data_point_name'] == SystemL::run || $value['data_point_name'] == SystemL::standBy){
  607. $return['run'][] = [
  608. 'time' => $time,
  609. 'stop_time' => $stop_time
  610. ];
  611. }
  612. if($value['data_point_name'] == SystemL::standBy){
  613. $return['work'][] = [
  614. 'time' => $time,
  615. 'stop_time' => $stop_time
  616. ];
  617. }
  618. if($value['data_point_name'] == SystemL::stop){
  619. $return['stop'][] = [
  620. 'time' => $time,
  621. 'stop_time' => $stop_time
  622. ];
  623. }
  624. }
  625. return [true, $return];
  626. }
  627. /**
  628. * 数据分析图
  629. * @param $data
  630. * @return array
  631. */
  632. public function deviceStatisticsReportChart($data){
  633. if(empty($data['time'][0]) || empty($data['time'][1])) return [false, '时间必须选择!'];
  634. $result = SystemL::where('time','>=',$data['time'][0])
  635. ->where('time','<',$data['time'][1])
  636. ->where('data_point_name',SystemL::standBy)
  637. ->select('device_name','time','value')
  638. ->get()->toArray();
  639. //所有的时间
  640. $time_all = [];
  641. foreach ($result as $value){
  642. $time = date('Y-m-d',$value['time'] / 1000);
  643. //工作 运行次数
  644. if(isset($process_time[$value['device_name']][$time])){
  645. $process_time[$value['device_name']][$time] += 1;
  646. }else{
  647. $process_time[$value['device_name']][$time] = 1;
  648. }
  649. if(! in_array($time, $time_all)) $time_all[] = $time;
  650. }
  651. sort($time_all);
  652. //数据结构模型
  653. foreach (SystemL::$device as $k => $v){
  654. if(isset($process_time[$k])){
  655. foreach ($time_all as $t){
  656. if(! isset($process_time[$k][$t])){
  657. $process_time[$k][$t] = 0;
  658. }
  659. }
  660. }else{
  661. foreach ($time_all as $t){
  662. $process_time[$k][$t] = 0;
  663. }
  664. }
  665. }
  666. $return = [];
  667. foreach ($process_time as $key => $value){
  668. $tmp['title'] = $key;
  669. $tmp['list'] = [];
  670. foreach ($value as $k => $v){
  671. $tmp['list'][] = [
  672. 'time' => $k,
  673. 'num' => $v
  674. ];
  675. }
  676. $return[] = $tmp;
  677. }
  678. return [true, $return];
  679. }
  680. /**
  681. * 数据OEE分析图
  682. * @param $data
  683. * @return array
  684. */
  685. public function deviceStatisticsReportOEEChart($data){
  686. if(empty($data['time'][0]) || empty($data['time'][1])) return [false, '时间必须选择!'];
  687. //获取数据
  688. $result = SystemL::where('time','>=',$data['time'][0])
  689. ->where('time','<',$data['time'][1])
  690. ->select('device_name','time','value','data_point_name')
  691. ->get()->toArray();
  692. if(empty($result)) return [true,[]];
  693. $device_name = array_values(array_unique(array_column($result,'device_name')));
  694. $run_time = $process_time = $fault = $time_all = [];
  695. foreach ($result as $value){
  696. $time = date("Y-m-d",$value['time'] / 1000);
  697. if($value['data_point_name'] == SystemL::run || $value['data_point_name'] == SystemL::standBy){
  698. //运行次数
  699. if(isset($run_time[$value['device_name']][$time])){
  700. $run_time[$value['device_name']][$time] += 1;
  701. }else{
  702. $run_time[$value['device_name']][$time] = 1;
  703. }
  704. }
  705. if($value['data_point_name'] == SystemL::standBy){
  706. //工作次数
  707. if(isset($process_time[$value['device_name']][$time])){
  708. $process_time[$value['device_name']][$time] += 1;
  709. }else{
  710. $process_time[$value['device_name']][$time] = 1;
  711. }
  712. }
  713. if($value['data_point_name'] == SystemL::stop){
  714. //故障次数
  715. if(isset($fault[$value['device_name']][$time])){
  716. $fault[$value['device_name']][$time] += 1;
  717. }else{
  718. $fault[$value['device_name']][$time] = 1;
  719. }
  720. }
  721. if(! in_array($time, $time_all)) $time_all[] = $time;
  722. }
  723. sort($time_all);
  724. //组织模型 返回大致的数据结构
  725. $models = [];
  726. foreach (SystemL::$device as $k => $v){
  727. foreach ($time_all as $t){
  728. $models[$k][$t] = 0;
  729. }
  730. }
  731. //填充模型里的数据
  732. foreach ($device_name as $value){//设备名
  733. foreach ($time_all as $d_val){
  734. //运行次数
  735. $run_num = $run_time[$value][$d_val] ?? 0;
  736. //工作次数
  737. $process_num = $process_time[$value][$d_val] ?? 0;
  738. //故障次数
  739. $fault_tmp = $fault[$value][$d_val] ?? 0;
  740. //运行时间
  741. $run_time_tmp = $this->calTimeReturnMin($run_num);
  742. //工作时间
  743. $process_time_tmp = $this->calTimeReturnMin($process_num);
  744. //故障时间
  745. $fault_time_tmp = $this->calTimeReturnMin($fault_tmp);
  746. //计划运行时间 工作时间 - 计划停机
  747. //实际运行时间 计划运行时间 -故障停机 - 设备调整
  748. $true_process_time = $process_time_tmp - $fault_time_tmp;
  749. //有效率 实际/计划运行 时间
  750. $efficient = $process_time_tmp > 0 ? number_format($true_process_time / $process_time_tmp,2) : 0;
  751. //表现性 加工数量/实际运行时间
  752. $expressive = $true_process_time > 0 ? number_format($process_num / $true_process_time,2) : 0;
  753. //质量指数 加工数量- 废品数量 / 加工数量
  754. $quality_index = $process_num > 0 ? number_format(($process_num - $fault_tmp) / $process_num,2) : 0;
  755. //OEE
  756. $oee = number_format($efficient * $expressive * $quality_index,2);
  757. //模型里赋值
  758. if(isset($models[$value][$d_val])){
  759. $models[$value][$d_val] = $oee;
  760. }
  761. }
  762. }
  763. //返回结果
  764. $final = [];
  765. foreach ($models as $key => $value){
  766. $tmp['title'] = $key;
  767. $tmp['list'] = [];
  768. foreach ($value as $k => $v){
  769. $tmp['list'][] = [
  770. 'time' => $k,
  771. 'num' => $v
  772. ];
  773. }
  774. $final[] = $tmp;
  775. }
  776. return [true,$final];
  777. }
  778. /**
  779. * 用于计算时间
  780. * @param $minute
  781. * @return string
  782. */
  783. public function calTimeReturnMin($minute){
  784. return number_format($minute * 1.5 / 60,2);
  785. }
  786. }