ReportFormsService.php 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813
  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. //压机上升 运行
  455. //小车前进 工作
  456. //待机 运行-工作
  457. $run_time = $process_time = $fault = [];
  458. foreach ($result as $value){
  459. //运行
  460. if($value['data_point_name'] == SystemL::run){
  461. if(isset($run_time[$value['device_name']])){
  462. $run_time[$value['device_name']] += 1;
  463. }else{
  464. $run_time[$value['device_name']] = 1;
  465. }
  466. }
  467. if($value['data_point_name'] == SystemL::work){
  468. //加工 工作 运行次数
  469. if(isset($process_time[$value['device_name']])){
  470. $process_time[$value['device_name']] += 1;
  471. }else{
  472. $process_time[$value['device_name']] = 1;
  473. }
  474. }
  475. if($value['data_point_name'] == SystemL::stop){
  476. //设备故障次数
  477. if(isset($fault[$value['device_name']])){
  478. $fault[$value['device_name']] += 1;
  479. }else{
  480. $fault[$value['device_name']] = 1;
  481. }
  482. }
  483. }
  484. foreach ($device_name as $key => $value){
  485. //工作(加工)次数
  486. $process_num = $process_time[$value] ?? 0;
  487. //运行时间
  488. $run_time_tmp = number_format(($run_time[$value] ?? 0) * 1.5 / 60,2);
  489. //工作/加工 时间
  490. $process_time_tmp = number_format($process_num * 1.5 / 60,2);
  491. //故障次数
  492. $fault_tmp = $fault[$value] ?? 0;
  493. //故障时间
  494. $fault_time_tmp = number_format($fault_tmp * 1.5 / 60,2);
  495. //计划运行时间 工作时间 - 计划停机 目前就是 $process_time_tmp
  496. //实际运行时间 计划运行时间 -故障停机 - 设备调整
  497. $true_process_time = $process_time_tmp - $fault_time_tmp;
  498. //有效率 实际/计划运行 时间
  499. $efficient = $process_time_tmp > 0 ? number_format($true_process_time / $process_time_tmp,2) : 0;
  500. //表现性 加工数量/实际运行实际
  501. $expressive = $true_process_time > 0 ? number_format($process_num / $true_process_time,2) : 0;
  502. //质量指数 加工数量- 废品数量 / 加工数量
  503. $quality_index = $process_num > 0 ? ($process_num - $fault_tmp) / $process_num : 0;
  504. $device_name[$key] = [
  505. 'device_name' => $value,
  506. 'run_time' => $run_time_tmp,
  507. 'process_time' => $process_time_tmp,
  508. 'standby_time' => number_format($run_time_tmp - $process_time_tmp,2),
  509. 'process_num' => $process_num,
  510. 'fault_num' => $fault_tmp,
  511. // 'e' => $efficient,
  512. // 'ex' => $expressive,
  513. // 'true_process_time' => $true_process_time,
  514. // 'qua' => $quality_index,
  515. 'oee' => number_format($efficient * $expressive * $quality_index,2)
  516. ];
  517. }
  518. return [true,$device_name];
  519. }
  520. public function deviceStatisticsReportDetail($data){
  521. if(empty($data['device_name']) || empty($data['time'][0]) || empty($data['time'][0])) return [false,'参数不能为空!'];
  522. $result = SystemL::where('time','>=',$data['time'][0])
  523. ->where('time','<',$data['time'][1])
  524. ->where('device_name',$data['device_name'])
  525. ->select('device_name','time','data_point_name')
  526. ->get()->toArray();
  527. date_default_timezone_set("PRC");
  528. $return = [
  529. 'run' => [],
  530. 'work' => [],
  531. 'stop' => [],
  532. 'stand_by' => [],
  533. ];
  534. foreach ($result as $key => $value){
  535. $time = date('Y-m-d H:i:s',$value['time'] / 1000);
  536. $stop_time = date('Y-m-d H:i:s',strtotime($value['time'] / 1000)+rand(10,40));
  537. if($value['data_point_name'] == SystemL::run){
  538. $return['run'][] = [
  539. 'time' => $time,
  540. 'stop_time' => $stop_time
  541. ];
  542. }
  543. if($value['data_point_name'] == SystemL::work){
  544. $return['work'][] = [
  545. 'time' => $time,
  546. 'stop_time' => $stop_time
  547. ];
  548. }
  549. if($value['data_point_name'] == SystemL::stop){
  550. $return['stop'][] = [
  551. 'time' => $time,
  552. 'stop_time' => $stop_time
  553. ];
  554. }
  555. if($value['data_point_name'] == SystemL::standBy){
  556. $return['stand_by'][] = [
  557. 'time' => $time,
  558. 'stop_time' => $stop_time
  559. ];
  560. }
  561. }
  562. return [true, $return];
  563. }
  564. //数据分析图
  565. public function deviceStatisticsReportChart($data){
  566. if(empty($data['time'][0]) || empty($data['time'][1])) return [false, '时间必须选择!'];
  567. $result = SystemL::where('time','>=',$data['time'][0])
  568. ->where('time','<',$data['time'][1])
  569. ->where('data_point_name',SystemL::work)
  570. ->select('device_name','time','value')
  571. ->get()->toArray();
  572. date_default_timezone_set("PRC");
  573. $time_all = [];
  574. foreach ($result as $value){
  575. $time = date('Y-m-d',$value['time'] / 1000);
  576. //工作 运行次数
  577. if(isset($process_time[$value['device_name']][$time])){
  578. $process_time[$value['device_name']][$time] += 1;
  579. }else{
  580. $process_time[$value['device_name']][$time] = 1;
  581. }
  582. if(! in_array($time, $time_all)) $time_all[] = $time;
  583. }
  584. sort($time_all);
  585. foreach (SystemL::$device as $k => $v){
  586. if(isset($process_time[$k])){
  587. foreach ($time_all as $t){
  588. if(! isset($process_time[$k][$t])){
  589. $process_time[$k][$t] = 0;
  590. }
  591. }
  592. }else{
  593. foreach ($time_all as $t){
  594. $process_time[$k][$t] = 0;
  595. }
  596. }
  597. }
  598. $return = [];
  599. foreach ($process_time as $key => $value){
  600. $tmp['title'] = $key;
  601. $tmp['list'] = [];
  602. foreach ($value as $k => $v){
  603. $tmp['list'][] = [
  604. 'time' => $k,
  605. 'num' => $v
  606. ];
  607. }
  608. $return[] = $tmp;
  609. }
  610. return [true, $return];
  611. }
  612. //数据OEE分析图
  613. public function deviceStatisticsReportOEEChart($data){
  614. if(empty($data['time'][0]) || empty($data['time'][1])) return [false, '时间必须选择!'];
  615. //获取数据
  616. $result = SystemL::where('time','>=',$data['time'][0])
  617. ->where('time','<',$data['time'][1])
  618. ->select('device_name','time','value','data_point_name')
  619. ->get()->toArray();
  620. if(empty($result)) return [true,[]];
  621. $device_name = array_values(array_unique(array_column($result,'device_name')));
  622. date_default_timezone_set("PRC");
  623. $run_time = $process_time = $fault = $time_all = [];
  624. foreach ($result as $value){
  625. $time = date("Y-m-d",$value['time'] / 1000);
  626. //运行
  627. if($value['data_point_name'] == SystemL::run){
  628. if(isset($run_time[$value['device_name']][$time])){
  629. $run_time[$value['device_name']][$time] += 1;
  630. }else{
  631. $run_time[$value['device_name']][$time] = 1;
  632. }
  633. }
  634. if($value['data_point_name'] == SystemL::work){
  635. //加工 工作 运行次数
  636. if(isset($process_time[$value['device_name']][$time])){
  637. $process_time[$value['device_name']][$time] += 1;
  638. }else{
  639. $process_time[$value['device_name']][$time] = 1;
  640. }
  641. }
  642. if($value['data_point_name'] == SystemL::stop){
  643. //设备故障次数
  644. if(isset($fault[$value['device_name']][$time])){
  645. $fault[$value['device_name']][$time] += 1;
  646. }else{
  647. $fault[$value['device_name']][$time] = 1;
  648. }
  649. }
  650. if(! in_array($time, $time_all)) $time_all[] = $time;
  651. }
  652. sort($time_all);
  653. //组织模型 返回大致的数据结构
  654. $models = [];
  655. foreach (SystemL::$device as $k => $v){
  656. foreach ($time_all as $t){
  657. $models[$k][$t] = 0;
  658. }
  659. }
  660. //填充模型里的数据
  661. foreach ($device_name as $value){//设备名
  662. foreach ($time_all as $d_val){
  663. //工作(加工)次数
  664. $process_num = $process_time[$value][$d_val] ?? 0;
  665. //运行时间
  666. $run_time_tmp = number_format(($run_time[$value][$d_val] ?? 0) * 1.5 / 60,2);
  667. //工作/加工 时间
  668. $process_time_tmp = number_format($process_num * 1.5 / 60,2);
  669. //故障次数
  670. $fault_tmp = $fault[$value][$d_val] ?? 0;
  671. //故障时间
  672. $fault_time_tmp = number_format($fault_tmp * 1.5 / 60,2);
  673. //计划运行时间 工作时间 - 计划停机 目前就是 $process_time_tmp
  674. //实际运行时间 计划运行时间 -故障停机 - 设备调整
  675. $true_process_time = $process_time_tmp - $fault_time_tmp;
  676. //有效率 实际/计划运行 时间
  677. $efficient = $process_time_tmp > 0 ? number_format($true_process_time / $process_time_tmp,2) : 0;
  678. //表现性 加工数量/实际运行实际
  679. $expressive = $true_process_time > 0 ? number_format($process_num / $true_process_time,2) : 0;
  680. //质量指数 加工数量- 废品数量 / 加工数量
  681. $quality_index = $process_num > 0 ? ($process_num - $fault_tmp) / $process_num : 0;
  682. if(isset($models[$value][$d_val])){
  683. $models[$value][$d_val] = number_format($efficient * $expressive * $quality_index,2);
  684. }
  685. // $return[$value][] = [
  686. // 'time' => $d_val,
  687. // 'device_name' => $d_val,
  688. // 'run_time' => $run_time_tmp,
  689. // 'process_time' => $process_time_tmp,
  690. // 'standby_time' => number_format($run_time_tmp - $process_time_tmp,2),
  691. // 'process_num' => $process_num,
  692. // 'fault_num' => $fault_tmp,
  693. // 'e' => $efficient,
  694. // 'ex' => $expressive,
  695. // 'true_process_time' => $true_process_time,
  696. // 'qua' => $quality_index,
  697. // 'oee' => number_format($efficient * $expressive * $quality_index,2)
  698. // ];
  699. }
  700. }
  701. //返回结果
  702. $final = [];
  703. foreach ($models as $key => $value){
  704. $tmp['title'] = $key;
  705. $tmp['list'] = [];
  706. foreach ($value as $k => $v){
  707. $tmp['list'][] = [
  708. 'time' => $k,
  709. 'num' => $v
  710. ];
  711. }
  712. $final[] = $tmp;
  713. }
  714. return [true,$final];
  715. }
  716. }