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');
  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['production_no']])){
  358. $list[$value['production_no']] = $value;
  359. }else{
  360. $list[$value['production_no']]['order_quantity'] += $value['order_quantity'];
  361. $list[$value['production_no']]['production_quantity'] += $value['production_quantity'];
  362. }
  363. }unset($orderList);
  364. //查询分表数据
  365. $production_no = array_column($list,'production_no');
  366. $detail = [];
  367. foreach ($out_time as $value){
  368. //子表搜索
  369. $models = new OrdersProductProcess(['channel' => $value]);
  370. $tmp = $models->whereIn('production_no',$production_no)
  371. ->where('del_time',0)
  372. ->where('status',4)
  373. ->select('production_no',DB::raw('COUNT(id) as bad_goods_num'))
  374. ->groupBy('production_no')
  375. ->get()->toArray();//不良品数据
  376. foreach ($tmp as $t){
  377. if(isset($detail[$t['production_no']])){
  378. $detail[$t['production_no']]['bad_goods_num'] += $t['bad_goods_num'];
  379. }else{
  380. $detail[$t['production_no']] = $t;
  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['production_no']] ?? 0;
  389. $list[$key]['bad_goods_num'] = $del_num;
  390. $list[$key]['rate'] = number_format($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. foreach ($out_time as $value){
  410. //子表搜索
  411. $models = new OrdersProductProcess(['channel' => $value]);
  412. $tmp = $models->where('production_no',$data['production_no'])
  413. ->where('del_time',0)
  414. ->where('status',4)
  415. ->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"))
  416. ->groupBy('order_product_id')
  417. ->get()
  418. ->toArray();//不良品数据
  419. foreach ($tmp as $v){
  420. if(isset($detail[$v['order_product_id']])){
  421. $detail[$v['order_product_id']] += $v['bad_goods_num'];
  422. }else{
  423. $detail[$v['order_product_id']] = $v['bad_goods_num'];
  424. }
  425. if(isset($detail_scrapp[$v['order_product_id']])){
  426. $detail_scrapp[$v['order_product_id']] .= "," . $v['scrapp_ids'];
  427. }else{
  428. $detail_scrapp[$v['order_product_id']] = $v['scrapp_ids'];
  429. }
  430. }
  431. }
  432. $scrapp_map = Scrapp::where('del_time',0)->pluck('title','id')->toArray();
  433. foreach ($list as $key => $value) {
  434. $list[$key]['production_time'] = $value['production_time'] ? date('Y-m-d',$value['production_time']) : '';
  435. $list[$key]['out_order_no_time'] = $value['out_order_no_time'] ? date('Y-m-d',$value['out_order_no_time']) : '';
  436. $list[$key]['bad_goods_num'] = $detail[$value['id']] ?? 0;
  437. $list[$key]['rate'] = number_format($list[$key]['bad_goods_num'] / $value['production_quantity'], 2);
  438. $scrapp = $detail_scrapp[$value['id']] ?? '';
  439. $tmp_str = '';
  440. if(! empty($scrapp)){
  441. $tmp = explode(',',$scrapp);
  442. foreach ($tmp as $vv){
  443. $tmp_str .= ($scrapp_map[$vv] ? $scrapp_map[$vv] . ',' : '');
  444. }
  445. }
  446. $list[$key]['scrapp_name'] = rtrim($tmp_str,',');
  447. }
  448. return [true, $list];
  449. }
  450. /**
  451. * 不良品原因
  452. * @param $data
  453. * @return array
  454. */
  455. public function badGoodsReasonReport($data){
  456. if(empty($data['production_time'][0]) || empty($data['production_time'][1])) return [false, '生产订单时间必须选择!'];
  457. //检索条件 生产订单主表----------------
  458. $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');
  459. $model->whereBetween('production_time',[$data['production_time'][0],$data['production_time'][1]]);
  460. if(! empty($data['production_no'])) $model->where('production_no', 'LIKE', '%'.$data['production_no'].'%');
  461. 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]]);
  462. if(! empty($data['out_order_no'])) $model->where('out_order_no', 'LIKE', '%'.$data['out_order_no'].'%');
  463. if(! empty($data['customer_no'])) $model->where('customer_no', 'LIKE', '%'.$data['customer_no'].'%');
  464. if(! empty($data['customer_name'])) $model->where('customer_name', 'LIKE', '%'.$data['customer_name'].'%');
  465. if(! empty($data['out_crt_man'])) $model->where('out_crt_man', 'LIKE', '%'.$data['out_crt_man'].'%');
  466. $orderList = $model->get()->toArray();
  467. //生产订单主表----------------
  468. //筛选出制单日期 分表的依据
  469. $out_order_no_time = array_unique(array_column($orderList,'out_order_no_time'));
  470. //制单日期
  471. $out_time = $this->checkSameQuarter($out_order_no_time);
  472. //分组以后的订单列表
  473. $list = [];
  474. foreach ($orderList as $value){
  475. if(! isset($list[$value['production_no']])){
  476. $list[$value['production_no']] = $value;
  477. }else{
  478. $list[$value['production_no']]['order_quantity'] += $value['order_quantity'];
  479. $list[$value['production_no']]['production_quantity'] += $value['production_quantity'];
  480. }
  481. }unset($orderList);
  482. //查询分表数据
  483. $production_no = array_column($list,'production_no');
  484. $detail = [];
  485. foreach ($out_time as $value){
  486. //子表搜索
  487. $models = new OrdersProductProcess(['channel' => $value]);
  488. $tmp = $models->whereIn('production_no',$production_no)
  489. ->where('del_time',0)
  490. ->where('status',4)
  491. ->select('production_no',DB::raw('COUNT(id) as bad_goods_num'))
  492. ->groupBy('production_no')
  493. ->get()->toArray();//不良品数据
  494. foreach ($tmp as $t){
  495. if(isset($detail[$t['production_no']])){
  496. $detail[$t['production_no']]['bad_goods_num'] += $t['bad_goods_num'];
  497. }else{
  498. $detail[$t['production_no']] = $t;
  499. }
  500. }
  501. }
  502. //返回统计数据
  503. foreach ($list as $key => $value) {
  504. $list[$key]['production_time'] = $value['production_time'] ? date('Y-m-d',$value['production_time']) : '';
  505. $list[$key]['out_order_no_time'] = $value['out_order_no_time'] ? date('Y-m-d',$value['out_order_no_time']) : '';
  506. $del_num = $detail[$value['production_no']] ?? 0;
  507. $list[$key]['bad_goods_num'] = $del_num;
  508. $list[$key]['rate'] = number_format($del_num / $value['production_quantity'], 2);
  509. }
  510. return [true,array_values($list)];
  511. }
  512. //不良品原因 详情
  513. public function badGoodsReasonReportDetail($data){
  514. if(empty($data['production_no'])) return [false,'生产订单号不能为空!'];
  515. $list = OrdersProduct::where('production_no',$data['production_no'])
  516. ->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')
  517. ->get()->toArray();
  518. //筛选出制单日期 分表的依据
  519. $out_order_no_time = array_unique(array_column($list,'out_order_no_time'));
  520. //制单日期
  521. $out_time = $this->checkSameQuarter($out_order_no_time);
  522. $detail = $scrapp_id = $team = $man = [];
  523. foreach ($out_time as $value){
  524. //子表搜索
  525. $models = new OrdersProductProcess(['channel' => $value]);
  526. $tmp = $models->where('production_no',$data['production_no'])
  527. ->where('del_time',0)
  528. ->where('status',4)
  529. ->select('order_product_id','scrapp_id','team_id','finished_id',DB::raw('COUNT(id) as bad_goods_num'))
  530. ->groupBy('order_product_id','scrapp_id')
  531. ->get()->toArray();//不良品数据
  532. foreach ($tmp as $v){
  533. if(! in_array($v['scrapp_id'], $scrapp_id)) $scrapp_id[] = $v['scrapp_id'];
  534. if(! in_array($v['team_id'], $team)) $team[] = $v['team_id'];
  535. if(! in_array($v['finished_id'], $man)) $man[] = $v['finished_id'];
  536. if(isset($detail[$v['order_product_id']])){
  537. foreach ($detail[$v['order_product_id']] as $k => $d){
  538. if($d['scrapp_id'] == $v['scrapp_id']){
  539. $detail[$v['order_product_id']][$k]['bad_goods_num'] += $v['bad_goods_num'];
  540. }else{
  541. $detail[$v['order_product_id']][] = $v;
  542. }
  543. }
  544. }else{
  545. $detail[$v['order_product_id']][] = $v;
  546. }
  547. }
  548. }
  549. //次品原因 班组 人员
  550. $map = Scrapp::whereIn('id',$scrapp_id)
  551. ->pluck('title','id')
  552. ->toArray();
  553. $map1 = Team::whereIn('id',$team)
  554. ->pluck('title','id')
  555. ->toArray();
  556. $map2 = Employee::whereIn('id',$man)
  557. ->pluck('emp_name','id')
  558. ->toArray();
  559. foreach ($list as $key => $value) {
  560. $list[$key]['production_time'] = $value['production_time'] ? date('Y-m-d',$value['production_time']) : '';
  561. $list[$key]['out_order_no_time'] = $value['out_order_no_time'] ? date('Y-m-d',$value['out_order_no_time']) : '';
  562. $list[$key]['rate'] = number_format(($detail[$value['id']] ?? 0) / $value['production_quantity'], 2);
  563. $del_tmp = $detail[$value['id']] ?? [];
  564. foreach ($del_tmp as $dk => $dv){
  565. $del_tmp[$dk]['rate'] = number_format($dv['bad_goods_num'] / $value['production_quantity'], 2);
  566. $del_tmp[$dk]['scrapp_name'] = $map[$dv['scrapp_id']] ?? '';
  567. $del_tmp[$dk]['team_name'] = $map1[$dv['team_id']] ?? '';
  568. $del_tmp[$dk]['man_name'] = $map2[$dv['finished_id']] ?? '';
  569. }
  570. $list[$key]['bad_goods'] = $del_tmp;
  571. }
  572. return [true, $list];
  573. }
  574. //设备统计报表
  575. public function deviceStatisticsReport($data){
  576. if(empty($data['time'][0]) || empty($data['time'][1])) return [false, '时间必须选择!'];
  577. $day = $this->returnDays($data['time']);
  578. if($day > 10) return [false, '设备数据查询时间仅支持范围区间在10天内'];
  579. $model = SystemL::where('time','>=',$data['time'][0])
  580. ->where('time','<',$data['time'][1]);
  581. if(! empty($data['title'])) $model->whereIn('device_name',$data['title']);
  582. $result = $model->select('device_name','time','value','data_point_name')
  583. ->get()
  584. ->toArray();
  585. if(empty($result)) return [true,[]];
  586. $device_name = array_values(array_unique(array_column($result,'device_name')));
  587. //运行时间 工作时间 故障
  588. $run_time = $process_time = $fault = [];
  589. foreach ($result as $value){
  590. if($value['data_point_name'] == SystemL::run || $value['data_point_name'] == SystemL::standBy){
  591. //运行次数
  592. if(isset($run_time[$value['device_name']])){
  593. $run_time[$value['device_name']] += 1;
  594. }else{
  595. $run_time[$value['device_name']] = 1;
  596. }
  597. }
  598. if($value['data_point_name'] == SystemL::standBy){
  599. //工作次数
  600. if(isset($process_time[$value['device_name']])){
  601. $process_time[$value['device_name']] += 1;
  602. }else{
  603. $process_time[$value['device_name']] = 1;
  604. }
  605. }
  606. if($value['data_point_name'] == SystemL::stop){
  607. //设备故障次数
  608. if(isset($fault[$value['device_name']])){
  609. $fault[$value['device_name']] += 1;
  610. }else{
  611. $fault[$value['device_name']] = 1;
  612. }
  613. }
  614. }
  615. foreach ($device_name as $key => $value){
  616. //运行次数
  617. $run_num = $run_time[$value] ?? 0;
  618. //工作次数
  619. $process_num = $process_time[$value] ?? 0;
  620. //故障次数
  621. $fault_tmp = $fault[$value] ?? 0;
  622. //运行时间
  623. $run_time_tmp = $this->calTimeReturnMin($run_num);
  624. //工作时间
  625. $process_time_tmp = $this->calTimeReturnMin($process_num);
  626. //故障时间
  627. $fault_time_tmp = $this->calTimeReturnMin($fault_tmp);
  628. //待机时间
  629. $standby_time_tmp = number_format($run_time_tmp - $process_time_tmp,2);
  630. //计划运行时间 工作时间 - 计划停机 (没有计划停机)
  631. //实际运行时间 计划运行时间 -故障停机 - 设备调整(没有设备调整
  632. $true_process_time = $process_time_tmp - $fault_time_tmp;
  633. //有效率 实际/计划运行 时间
  634. $efficient = $process_time_tmp > 0 ? number_format($true_process_time / $process_time_tmp,2) : 0;
  635. //表现性 加工数量/实际运行时间
  636. $expressive = $true_process_time > 0 ? number_format($process_num / $true_process_time,2) : 0;
  637. //质量指数 加工数量- 废品数量 / 加工数量
  638. $quality_index = $process_num > 0 ? number_format(($process_num - $fault_tmp) / $process_num,2) : 0;
  639. //OEE
  640. $oee = number_format($efficient * $expressive * $quality_index,2);
  641. $device_name[$key] = [
  642. 'device_name' => $value,
  643. 'run_time' => $run_time_tmp,
  644. 'process_time' => $process_time_tmp,
  645. 'standby_time' => $standby_time_tmp,
  646. 'process_num' => $process_num,
  647. 'fault_num' => $fault_tmp,
  648. 'oee' => $oee,
  649. // 'e' => $efficient,
  650. // 'ex' => $expressive,
  651. // 'true_process_time' => $true_process_time,
  652. // 'qua' => $quality_index
  653. ];
  654. }
  655. return [true,$device_name];
  656. }
  657. public function deviceStatisticsReportDetail($data){
  658. if(empty($data['device_name']) || empty($data['time'][0]) || empty($data['time'][0])) return [false,'参数不能为空!'];
  659. $day = $this->returnDays($data['time']);
  660. if($day > 10) return [false, '设备数据查询时间仅支持范围区间在10天内'];
  661. $result = SystemL::where('time','>=',$data['time'][0])
  662. ->where('time','<',$data['time'][1])
  663. ->where('device_name',$data['device_name'])
  664. ->select('device_name','time','data_point_name')
  665. ->get()->toArray();
  666. $return = [
  667. 'run' => [],
  668. 'work' => [],
  669. 'stop' => [],
  670. 'stand_by' => [],
  671. ];
  672. foreach ($result as $key => $value){
  673. $time = date('Y-m-d H:i:s',$value['time'] / 1000);
  674. $stop_time = date('Y-m-d H:i:s',$value['time'] / 1000 + rand(10,40));
  675. if($value['data_point_name'] == SystemL::run || $value['data_point_name'] == SystemL::standBy){
  676. $return['run'][] = [
  677. 'time' => $time,
  678. 'stop_time' => $stop_time
  679. ];
  680. }
  681. if($value['data_point_name'] == SystemL::standBy){
  682. $return['work'][] = [
  683. 'time' => $time,
  684. 'stop_time' => $stop_time
  685. ];
  686. }
  687. if($value['data_point_name'] == SystemL::stop){
  688. $return['stop'][] = [
  689. 'time' => $time,
  690. 'stop_time' => $stop_time
  691. ];
  692. }
  693. }
  694. return [true, $return];
  695. }
  696. /**
  697. * 数据分析图
  698. * @param $data
  699. * @return array
  700. */
  701. public function deviceStatisticsReportChart($data){
  702. if(empty($data['time'][0]) || empty($data['time'][1])) return [false, '时间必须选择!'];
  703. $day = $this->returnDays($data['time'], false);
  704. if($day > 31) return [false, '查询时间仅支持范围区间在31天内'];
  705. $process_time = [];
  706. $result = SystemL::where('time','>=',$data['time'][0])
  707. ->where('time','<',$data['time'][1])
  708. ->where('data_point_name',SystemL::standBy)
  709. ->select('device_name','time','value')
  710. ->get()->toArray();
  711. //所有的时间
  712. $time_all = [];
  713. foreach ($result as $value){
  714. $time = date('Y-m-d',$value['time'] / 1000);
  715. //工作 运行次数
  716. if(isset($process_time[$value['device_name']][$time])){
  717. $process_time[$value['device_name']][$time] += 1;
  718. }else{
  719. $process_time[$value['device_name']][$time] = 1;
  720. }
  721. if(! in_array($time, $time_all)) $time_all[] = $time;
  722. }
  723. sort($time_all);
  724. //数据结构模型
  725. foreach (SystemL::$device as $k => $v){
  726. if(isset($process_time[$k])){
  727. foreach ($time_all as $t){
  728. if(! isset($process_time[$k][$t])){
  729. $process_time[$k][$t] = 0;
  730. }
  731. }
  732. }else{
  733. foreach ($time_all as $t){
  734. $process_time[$k][$t] = 0;
  735. }
  736. }
  737. }
  738. $return = [];
  739. foreach ($process_time as $key => $value){
  740. $tmp['title'] = $key;
  741. $tmp['list'] = [];
  742. foreach ($value as $k => $v){
  743. $tmp['list'][] = [
  744. 'time' => $k,
  745. 'num' => $v
  746. ];
  747. }
  748. $return[] = $tmp;
  749. }
  750. return [true, $return];
  751. }
  752. /**
  753. * 数据OEE分析图
  754. * @param $data
  755. * @return array
  756. */
  757. public function deviceStatisticsReportOEEChart($data){
  758. if(empty($data['time'][0]) || empty($data['time'][1])) return [false, '时间必须选择!'];
  759. $day = $this->returnDays($data['time'], false);
  760. if($day > 31) return [false, '查询时间仅支持范围区间在31天内'];
  761. //获取数据
  762. $result = SystemL::where('time','>=',$data['time'][0])
  763. ->where('time','<',$data['time'][1])
  764. ->select('device_name','time','value','data_point_name')
  765. ->get()->toArray();
  766. if(empty($result)) return [true,[]];
  767. $device_name = array_values(array_unique(array_column($result,'device_name')));
  768. $run_time = $process_time = $fault = $time_all = [];
  769. foreach ($result as $value){
  770. $time = date("Y-m-d",$value['time'] / 1000);
  771. if($value['data_point_name'] == SystemL::run || $value['data_point_name'] == SystemL::standBy){
  772. //运行次数
  773. if(isset($run_time[$value['device_name']][$time])){
  774. $run_time[$value['device_name']][$time] += 1;
  775. }else{
  776. $run_time[$value['device_name']][$time] = 1;
  777. }
  778. }
  779. if($value['data_point_name'] == SystemL::standBy){
  780. //工作次数
  781. if(isset($process_time[$value['device_name']][$time])){
  782. $process_time[$value['device_name']][$time] += 1;
  783. }else{
  784. $process_time[$value['device_name']][$time] = 1;
  785. }
  786. }
  787. if($value['data_point_name'] == SystemL::stop){
  788. //故障次数
  789. if(isset($fault[$value['device_name']][$time])){
  790. $fault[$value['device_name']][$time] += 1;
  791. }else{
  792. $fault[$value['device_name']][$time] = 1;
  793. }
  794. }
  795. if(! in_array($time, $time_all)) $time_all[] = $time;
  796. }
  797. sort($time_all);
  798. //组织模型 返回大致的数据结构
  799. $models = [];
  800. foreach (SystemL::$device as $k => $v){
  801. foreach ($time_all as $t){
  802. $models[$k][$t] = 0;
  803. }
  804. }
  805. //填充模型里的数据
  806. foreach ($device_name as $value){//设备名
  807. foreach ($time_all as $d_val){
  808. //运行次数
  809. $run_num = $run_time[$value][$d_val] ?? 0;
  810. //工作次数
  811. $process_num = $process_time[$value][$d_val] ?? 0;
  812. //故障次数
  813. $fault_tmp = $fault[$value][$d_val] ?? 0;
  814. //运行时间
  815. $run_time_tmp = $this->calTimeReturnMin($run_num);
  816. //工作时间
  817. $process_time_tmp = $this->calTimeReturnMin($process_num);
  818. //故障时间
  819. $fault_time_tmp = $this->calTimeReturnMin($fault_tmp);
  820. //计划运行时间 工作时间 - 计划停机
  821. //实际运行时间 计划运行时间 -故障停机 - 设备调整
  822. $true_process_time = $process_time_tmp - $fault_time_tmp;
  823. //有效率 实际/计划运行 时间
  824. $efficient = $process_time_tmp > 0 ? number_format($true_process_time / $process_time_tmp,2) : 0;
  825. //表现性 加工数量/实际运行时间
  826. $expressive = $true_process_time > 0 ? number_format($process_num / $true_process_time,2) : 0;
  827. //质量指数 加工数量- 废品数量 / 加工数量
  828. $quality_index = $process_num > 0 ? number_format(($process_num - $fault_tmp) / $process_num,2) : 0;
  829. //OEE
  830. $oee = number_format($efficient * $expressive * $quality_index,2);
  831. //模型里赋值
  832. if(isset($models[$value][$d_val])){
  833. $models[$value][$d_val] = $oee;
  834. }
  835. }
  836. }
  837. //返回结果
  838. $final = [];
  839. foreach ($models as $key => $value){
  840. $tmp['title'] = $key;
  841. $tmp['list'] = [];
  842. foreach ($value as $k => $v){
  843. $tmp['list'][] = [
  844. 'time' => $k,
  845. 'num' => $v
  846. ];
  847. }
  848. $final[] = $tmp;
  849. }
  850. return [true,$final];
  851. }
  852. /**
  853. * 用于计算时间
  854. * @param $minute
  855. * @return string
  856. */
  857. public function calTimeReturnMin($minute){
  858. return number_format($minute * 1.5 / 60,2);
  859. }
  860. /**
  861. * 报工
  862. * @param $data
  863. * @return array
  864. */
  865. public function statisticsReportWorkingChart($data){
  866. if(empty($data['crt_time'][0]) || empty($data['crt_time'][1])) return [false, '报工单生成时间必须选择!'];
  867. //人员
  868. $finished_id = $data['finished_id'] ?? [];
  869. //工序
  870. $process_id = $data['process_id'] ?? [];
  871. $result = ReportWorkingDetail::where('del_time',0)
  872. ->where('crt_time',">=",$data['crt_time'][0])
  873. ->where('crt_time',"<=",$data['crt_time'][1])
  874. ->when(! empty($finished_id), function ($query) use ($finished_id) {
  875. return $query->whereIn('finished_id', $finished_id);
  876. })
  877. ->when(! empty($process_id), function ($query) use ($process_id) {
  878. return $query->whereIn('process_id', $process_id);
  879. })
  880. ->get()->toArray();
  881. if(empty($result)) return [true, []];
  882. $emp_map = Employee::whereIn('id',array_unique(array_column($result,'finished_id')))
  883. ->pluck('emp_name','id')
  884. ->toArray();
  885. $process_map = Process::whereIn('id',array_unique(array_column($result,'process_id')))
  886. ->pluck('title','id')
  887. ->toArray();
  888. //派工单
  889. $d = DispatchSub::where('del_time',0)
  890. ->whereIn('id', array_unique(array_column($result,'data_id')))
  891. ->select('id', 'dispatch_no as order_no', 'product_title','technology_name','product_no','product_unit','price')
  892. ->get()->toArray();
  893. $d_tmp = array_column($d,null,'id');
  894. $return = [];
  895. foreach ($result as $value){
  896. $tmp = $d_tmp[$value['data_id']] ?? [];
  897. if(empty($tmp)) continue;
  898. $string = $value['finished_id'] . $value['process_id'];
  899. if(isset($return[$string])){
  900. $quantity = bcadd($value['quantity'], $return[$string]['quantity'], 3);
  901. $return[$string]['quantity'] = $quantity;
  902. if(! in_array($value['id'], $return[$string]['report_id'])){
  903. $id = array_merge_recursive($return[$string]['report_id'], [$value['id']]);
  904. $return[$string]['report_id'] = $id;
  905. }
  906. }else{
  907. $return[$string] = [
  908. 'finished_id' => $value['finished_id'],
  909. 'finished_title' => $emp_map[$value['finished_id']] ?? "",
  910. 'process_id' => $value['process_id'],
  911. 'process_title' => $process_map[$value['process_id']] ?? "",
  912. 'quantity' => $value['quantity'],
  913. 'report_id' => [$value['id']],
  914. ];
  915. }
  916. }
  917. return [true, array_values($return)];
  918. }
  919. public function statisticsReportWorkingChartDetail($data){
  920. if(empty($data['report_id'])) return [false, '报工单数据必须选择!'];
  921. $result = ReportWorkingDetail::where('del_time',0)
  922. ->whereIn('id', $data['report_id'])
  923. ->get()->toArray();
  924. if(empty($result)) return [false, '报工单数据不存在或已被删除'];
  925. //派工单
  926. $d = DispatchSub::where('del_time',0)
  927. ->whereIn('id', array_unique(array_column($result,'data_id')))
  928. ->select('id', 'dispatch_no as order_no', 'product_title','technology_name','product_no','product_unit','price')
  929. ->get()->toArray();
  930. $d_tmp = array_column($d,null,'id');
  931. $return = [];
  932. foreach ($result as $value){
  933. $tmp = $d_tmp[$value['data_id']] ?? [];
  934. if(empty($tmp)) continue;
  935. $string = $tmp['id'] . $tmp['product_no'] . $tmp['technology_name'];
  936. if(isset($return[$string])){
  937. $return[$string]['quantity'] += $value['quantity'];
  938. }else{
  939. $return[$string] = [
  940. 'order_no' => $tmp['order_no'] ?? "",
  941. 'product_title' => $tmp["product_title"] ?? "",
  942. 'product_no' => $tmp["product_no"] ?? "",
  943. 'product_unit' => $tmp["product_unit"] ?? "",
  944. 'technology_name' => $tmp['technology_name'] ?? "",
  945. 'wood_name' => $tmp['wood_name'] ?? "",
  946. 'quantity' => $value['quantity'],
  947. ];
  948. }
  949. }
  950. return [true, array_values($return)];
  951. }
  952. }