ReportFormsService.php 36 KB

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