ReportFormsService.php 41 KB

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