ReportFormsService.php 37 KB

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