ReportFormsService.php 46 KB

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