ReportFormsService.php 51 KB

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