ReportFormsService.php 42 KB

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