ReportFormsService.php 36 KB

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