ScrappService.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  1. <?php
  2. namespace App\Service;
  3. use App\Model\DispatchSub;
  4. use App\Model\Employee;
  5. use App\Model\EmployeeTeamPermission;
  6. use App\Model\Equipment;
  7. use App\Model\OrdersProduct;
  8. use App\Model\Process;
  9. use App\Model\ReportWorking;
  10. use App\Model\ReportWorkingDetail;
  11. use App\Model\SaleOrdersProduct;
  12. use App\Model\Scrapp;
  13. use App\Model\ScrappCount;
  14. use App\Model\Team;
  15. use App\Model\Zj;
  16. use App\Model\ZjPlan;
  17. use Illuminate\Support\Facades\DB;
  18. /**
  19. * 报废原因
  20. * @package App\Models
  21. */
  22. class ScrappService extends Service
  23. {
  24. public function scrappEdit($data){
  25. list($status,$msg) = $this->scrappRule($data,false);
  26. if(!$status) return [$status,$msg];
  27. $update = $msg['data'][0];
  28. Scrapp::where('id',$data['id'])->update($update);
  29. return [true,'保存成功!'];
  30. }
  31. public function scrappAdd($data){
  32. list($status,$msg) = $this->scrappRule($data);
  33. if(!$status) return [$status,$msg];
  34. Scrapp::insert($msg['data']);
  35. return [true,'保存成功!'];
  36. }
  37. public function scrappDel($data){
  38. if($this->isEmpty($data,'id')) return [false,'ID必须!'];
  39. Scrapp::whereIn('id',$data['id'])->update([
  40. 'del_time' => time()
  41. ]);
  42. return [true,'删除成功'];
  43. }
  44. public function scrappList($data){
  45. $model = Scrapp::where('del_time',0)
  46. ->select('*');
  47. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  48. $list = $this->limit($model,'',$data);
  49. return [200,$list];
  50. }
  51. public function zjdaList($data){
  52. $model = Zj::where('del_time',0)
  53. ->select('*');
  54. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  55. $list = $this->limit($model,'',$data);
  56. return [200,$list];
  57. }
  58. public function zjPlanList($data){
  59. $model = ZjPlan::where('del_time',0)
  60. ->select('*');
  61. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  62. $list = $this->limit($model,'',$data);
  63. $map = Zj::where('del_time',0)->pluck('title','id')->toArray();
  64. foreach ($list['data'] as $key => $value){
  65. $zj_plan_title = "";
  66. $tmp = explode(',', $value['zj_id']);
  67. foreach ($tmp as $v){
  68. $t = $map[$v];
  69. if(empty($t)) continue;
  70. $zj_plan_title .= $t . ",";
  71. }
  72. $list['data'][$key]['zj_plan_title'] = rtrim($zj_plan_title,',');
  73. }
  74. return [200,$list];
  75. }
  76. public function scrappRule($data,$is_add = true){
  77. if($this->isEmpty($data,'data')) return [false,'数据不能为空!'];
  78. $title = array_column($data['data'],'title');
  79. $title = array_map(function($val) {
  80. return $val !== null ? $val : 0;
  81. }, $title);
  82. $title_count = array_count_values($title);
  83. foreach ($title as $value){
  84. if(empty($value)) return [false,'名称不能为空!'];
  85. if($title_count[$value] > 1) return [false,'名称不能重复'];
  86. }
  87. foreach ($data['data'] as $key => $value){
  88. $data['data'][$key]['upd_time'] = time();
  89. if($is_add){
  90. $bool = Scrapp::whereRaw("title = '{$value['title']}'")
  91. ->where('del_time',0)
  92. ->exists();
  93. $data['data'][$key]['crt_time'] = time();
  94. }else{
  95. if($this->isEmpty($data,'id')) return [false,'id不能为空!'];
  96. $bool = Scrapp::whereRaw("title = '{$value['title']}'")
  97. ->where('id','<>',$data['id'])
  98. ->where('del_time',0)
  99. ->exists();
  100. }
  101. if($bool) return [false,'名称不能重复'];
  102. }
  103. return [true,$data];
  104. }
  105. //质检单
  106. public function zjList($data, $user){
  107. $process = Process::where('del_time',0)
  108. ->where('is_need_remain',1)
  109. ->pluck('id')
  110. ->toArray();
  111. $model = ScrappCount::where('del_time',0)
  112. ->whereIn('process_id', $process)
  113. ->select('*')
  114. ->orderBy('order_number','desc');
  115. if(! empty($data['sq'])) $model->where('result',ReportWorking::type_two);
  116. if(! empty($data['order_number'])) $model->where('order_number', 'LIKE', '%'.$data['order_number'].'%');
  117. if(! empty($data['dispatch_no'])) {
  118. $dispatch_id = DispatchSub::where('del_time',0)
  119. ->where('dispatch_no', 'LIKE', '%'.$data['dispatch_no'].'%')
  120. ->select('id')
  121. ->get()->toArray();
  122. $dispatch_id = array_column($dispatch_id,'id');
  123. $model->whereIn('dispatch_sub_id', $dispatch_id);
  124. }
  125. if(! empty($data['sale_order_number'])) {
  126. $sales_id = SaleOrdersProduct::where('del_time',0)
  127. ->where('out_order_no', 'LIKE', '%'.$data['sale_order_number'].'%')
  128. ->select('id')
  129. ->get()->toArray();
  130. $sales_id = array_column($sales_id,'id');
  131. $model->whereIn('sale_orders_product_id', $sales_id);
  132. }
  133. if(! empty($data['team_id'])) $model->where('team_id', $data['team_id']);
  134. if(! empty($data['team_man_id'])) {
  135. $team_id = EmployeeTeamPermission::where('employee_id',$data['team_man_id'])
  136. ->select('team_id')
  137. ->get()->toArray();
  138. $model->whereIn('team_id', array_unique(array_column($team_id,'team_id')));
  139. }
  140. if(! empty($data['finished_id'])) $model->where('finished_id', $data['finished_id']);
  141. if(! empty($data['equipment_id'])) $model->where('equipment_id', $data['equipment_id']);
  142. if(! empty($data['process_id'])) $model->where('process_id', $data['process_id']);
  143. $list = $this->limit($model,'',$data);
  144. $list = $this->fillZjList($list, $data, $user);
  145. return [true,$list];
  146. }
  147. public function fillZjList($data, $erg, $user){
  148. if(empty($data['data'])) return $data;
  149. $team_id = array_unique(array_column($data['data'],'team_id'));
  150. $team_maps = Team::whereIn('id',$team_id)
  151. ->pluck('title','id')
  152. ->toArray();
  153. $team_man = EmployeeTeamPermission::whereIn('team_id',$team_id)
  154. ->select('team_id','employee_id')
  155. ->get()->toArray();
  156. $emp_map = Employee::whereIn('id',array_merge_recursive(array_column($data['data'],'finished_id'),array_column($team_man,'employee_id')))
  157. ->pluck('emp_name','id')
  158. ->toArray();
  159. $team_man_maps = [];
  160. foreach ($team_man as $value){
  161. $t = $emp_map[$value['employee_id']] ?? "";
  162. if(empty($t)) continue;
  163. if(isset($team_man_maps[$value['team_id']])){
  164. $team_man_maps[$value['team_id']] .= ',' . $t;
  165. }else{
  166. $team_man_maps[$value['team_id']] = $t;
  167. }
  168. }
  169. $equipment_map = Equipment::whereIn('id',array_column($data['data'],'equipment_id'))
  170. ->pluck('title','id')
  171. ->toArray();
  172. $process_map = Process::whereIn('id',array_column($data['data'],'process_id'))
  173. ->pluck('title','id')
  174. ->toArray();
  175. $sales_number = SaleOrdersProduct::whereIn('id',array_unique(array_column($data['data'],'sale_orders_product_id')))
  176. ->pluck('out_order_no','id')
  177. ->toArray();
  178. $dispatch_no = ReportWorking::whereIn('id',array_column($data['data'], 'report_id'))
  179. ->pluck('order_number','id')
  180. ->toArray();
  181. $return = [];
  182. if(! empty($erg['material'])){
  183. $product_no = array_unique(array_column($data['data'],'product_no'));
  184. $service = new FyyOrderService();
  185. list($status, $msg) = $service->getProductDataFromSqlServer(['product_no' => $product_no], $user);
  186. if($status) $return = $msg;
  187. }
  188. foreach ($data['data'] as $key => $value){
  189. $data['data'][$key]['material'] = $return[$value['product_no']] ?? [];
  190. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date("Y-m-d", $value['crt_time']) : '';
  191. $data['data'][$key]['finished_title'] = $emp_map[$value['finished_id']] ?? '';
  192. $data['data'][$key]['team_title'] = $team_maps[$value['team_id']] ?? '';
  193. $data['data'][$key]['team_man'] = $team_man_maps[$value['team_id']] ?? '';
  194. $data['data'][$key]['equipment_title'] = $equipment_map[$value['equipment_id']] ?? '';
  195. $data['data'][$key]['process_title'] = $process_map[$value['process_id']] ?? '';
  196. $data['data'][$key]['sale_order_number'] = $sales_number[$value['sale_orders_product_id']] ?? "";
  197. $dispatch_t = $dispatch_no[$value['report_id']] ?? "";
  198. $data['data'][$key]['dispatch_no'] = $dispatch_t;
  199. // $data['data'][$key]['crt_time'] = date("Y-m-d",strtotime(substr($dispatch_t,0,8)));
  200. }
  201. return $data;
  202. }
  203. public function zjDetail($data, $user){
  204. if(empty($data['order_number'])) return [false, '请选择质检单数据'];
  205. $result = ScrappCount::where('del_time',0)
  206. ->where('order_number', $data['order_number'])
  207. ->get()->toArray();
  208. if(empty($result)) return [false, '质检单不存在或已被删除'];
  209. $first = $result[0] ?? [];
  210. $order['order_number'] = $first['order_number'];
  211. $order['process_title'] = Process::where('id',$first['process_id'])->value("title");
  212. $order['team_title'] = Team::where('id',$first['team_id'])->value("title");
  213. $order['finished_title'] = Employee::where('id',$first['finished_id'])->value("emp_name");
  214. $order['equipment_title'] = Equipment::where('id',$first['equipment_id'])->value("title");
  215. $dispatch = ReportWorking::where('id',$first['report_id'])->first();
  216. $dispatch = empty($dispatch) ? [] : $dispatch->toArray();
  217. $zj_plan_id = DispatchSub::where('id', $first['dispatch_sub_id'])->value('zj_plan_id');
  218. $order['dispatch_no'] = $dispatch['order_number'] ?? "";
  219. $order['crt_time'] = date("Y-m-d", $first['crt_time']);
  220. $team_man = EmployeeTeamPermission::where('team_id',$first['team_id'])
  221. ->select('team_id','employee_id')
  222. ->get()->toArray();
  223. $emp_map = Employee::whereIn('id',array_column($team_man,'employee_id'))
  224. ->pluck('emp_name','id')
  225. ->toArray();
  226. $team_man_maps = "";
  227. foreach ($team_man as $value){
  228. $t = $emp_map[$value['employee_id']] ?? "";
  229. if(empty($t)) continue;
  230. if(! empty($team_man_maps)){
  231. $team_man_maps .= ',' . $t;
  232. }else{
  233. $team_man_maps = $t;
  234. }
  235. }
  236. $order['team_man'] = $team_man_maps;
  237. $scrapp_quantity = array_sum(array_column($result, 'scrapp_num')); // 不良品数量
  238. $detail = [
  239. 'product_no' => $first['product_no'],
  240. 'product_title' => $first['product_title'],
  241. 'product_size' => $first['product_size'],
  242. 'product_unit' => $first['product_unit'],
  243. 'technology_name' => $first['technology_name'],
  244. 'production_quantity' => $dispatch['production_quantity'] ?? 0, // 生产数量
  245. 'dispatch_quantity' => $dispatch['dispatch_quantity'] ?? 0, // 派工数量
  246. 'quantity' => $first['quantity'], // 完工数量
  247. 'scrapp_quantity' => $scrapp_quantity, // 不良品数量
  248. 'zj_quantity' => $first['quantity'] + $scrapp_quantity, // 质检数量
  249. 'hg_quantity' => $first['quantity'], // 合格数量
  250. ];
  251. $order['detail'][] = $detail;
  252. $table1 = $table2 = [];
  253. if(! empty($zj_plan_id)){
  254. $plan = ZjPlan::where('id',$zj_plan_id)->first();
  255. if(! empty($plan)){
  256. $plan = $plan->toArray();
  257. $map = Zj::where('del_time',0)->pluck('title','id')->toArray();
  258. $tmp = explode(',', $plan['zj_id']);
  259. foreach ($tmp as $value){
  260. $table1[] = [
  261. 'zj_title' => $map[$value] ?? "",
  262. 'number' => $first['quantity'],
  263. 'mark' => ''
  264. ];
  265. }
  266. }
  267. }
  268. if(! empty($first['scrapp_id'])){
  269. $scrapp = Scrapp::where('id', $first['scrapp_id'])
  270. ->pluck('title','id')
  271. ->toArray();
  272. $table2[] = [
  273. 'zj_title' => $scrapp[$first['scrapp_id']] ?? "", // 不良品原因
  274. 'number' => $scrapp_quantity,
  275. 'mark' => ''
  276. ];
  277. }
  278. $order['table1'] = $table1;
  279. $order['table2'] = $table2;
  280. return [true, $order];
  281. }
  282. //不良品
  283. public function blpList($data, $user){
  284. $model = ScrappCount::where('del_time',0)
  285. ->where('scrapp_num','>',0)
  286. ->select('id','dispatch_sub_id','crt_time','team_id','finished_id','equipment_id','order_number','process_id','sale_orders_product_id','report_id')
  287. ->orderBy('dispatch_sub_id','desc')
  288. ->groupBy('order_number');
  289. if(! empty($data['order_number'])) $model->where('order_number', 'LIKE', '%'.$data['order_number'].'%');
  290. if(! empty($data['dispatch_no'])) {
  291. $dispatch_id = DispatchSub::where('del_time',0)
  292. ->where('dispatch_no', 'LIKE', '%'.$data['dispatch_no'].'%')
  293. ->select('id')
  294. ->get()->toArray();
  295. $dispatch_id = array_column($dispatch_id,'id');
  296. $model->whereIn('dispatch_sub_id', $dispatch_id);
  297. }
  298. if(! empty($data['sale_order_number'])) {
  299. $sales_id = SaleOrdersProduct::where('del_time',0)
  300. ->where('out_order_no', 'LIKE', '%'.$data['sale_order_number'].'%')
  301. ->select('id')
  302. ->get()->toArray();
  303. $sales_id = array_column($sales_id,'id');
  304. $model->whereIn('sale_orders_product_id', $sales_id);
  305. }
  306. if(! empty($data['team_id'])) $model->where('team_id', $data['team_id']);
  307. if(! empty($data['finished_id'])) $model->where('finished_id', $data['finished_id']);
  308. if(! empty($data['team_man_id'])) {
  309. $team_id = EmployeeTeamPermission::where('employee_id',$data['team_man_id'])
  310. ->select('team_id')
  311. ->get()->toArray();
  312. $model->whereIn('team_id', array_unique(array_column($team_id,'team_id')));
  313. }
  314. if(! empty($data['equipment_id'])) $model->where('equipment_id', $data['equipment_id']);
  315. if(! empty($data['process_id'])) $model->where('process_id', $data['process_id']);
  316. $list = $this->limit($model,'',$data);
  317. $list = $this->fillBLPList($list);
  318. return [true,$list];
  319. }
  320. public function fillBLPList($data){
  321. if(empty($data['data'])) return $data;
  322. $team_id = array_unique(array_column($data['data'],'team_id'));
  323. $team_maps = Team::whereIn('id',$team_id)
  324. ->pluck('title','id')
  325. ->toArray();
  326. $team_man = EmployeeTeamPermission::whereIn('team_id',$team_id)
  327. ->select('team_id','employee_id')
  328. ->get()->toArray();
  329. $emp_map = Employee::whereIn('id',array_merge_recursive(array_column($data['data'],'finished_id'),array_column($team_man,'employee_id')))
  330. ->pluck('emp_name','id')
  331. ->toArray();
  332. $team_man_maps = [];
  333. foreach ($team_man as $value){
  334. $t = $emp_map[$value['employee_id']] ?? "";
  335. if(empty($t)) continue;
  336. if(isset($team_man_maps[$value['team_id']])){
  337. $team_man_maps[$value['team_id']] .= ',' . $t;
  338. }else{
  339. $team_man_maps[$value['team_id']] = $t;
  340. }
  341. }
  342. $equipment_map = Equipment::whereIn('id',array_column($data['data'],'equipment_id'))
  343. ->pluck('title','id')
  344. ->toArray();
  345. $process_map = Process::whereIn('id',array_column($data['data'],'process_id'))
  346. ->pluck('title','id')
  347. ->toArray();
  348. $sales_number = SaleOrdersProduct::whereIn('id',array_unique(array_column($data['data'],'sale_orders_product_id')))
  349. ->pluck('out_order_no','id')
  350. ->toArray();
  351. $dispatch_no = ReportWorking::whereIn('id',array_column($data['data'], 'report_id'))
  352. ->pluck('order_number','id')
  353. ->toArray();
  354. foreach ($data['data'] as $key => $value){
  355. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date("Y-m-d", $value['crt_time']) : '';
  356. $data['data'][$key]['finished_title'] = $emp_map[$value['finished_id']] ?? '';
  357. $data['data'][$key]['team_title'] = $team_maps[$value['team_id']] ?? '';
  358. $data['data'][$key]['team_man'] = $team_man_maps[$value['team_id']] ?? '';
  359. $data['data'][$key]['equipment_title'] = $equipment_map[$value['equipment_id']] ?? '';
  360. $data['data'][$key]['process_title'] = $process_map[$value['process_id']] ?? '';
  361. $data['data'][$key]['sale_order_number'] = $sales_number[$value['sale_orders_product_id']] ?? "";
  362. $dispatch_t = $dispatch_no[$value['report_id']] ?? "";
  363. $data['data'][$key]['dispatch_no'] = $dispatch_t;
  364. // $data['data'][$key]['crt_time'] = date("Y-m-d",strtotime(substr($dispatch_t,0,8)));
  365. }
  366. return $data;
  367. }
  368. public function blpDetail($data, $user){
  369. if(empty($data['order_number'])) return [false, '请选择质检单数据'];
  370. $result = ScrappCount::where('del_time',0)
  371. ->where('order_number', $data['order_number'])
  372. ->get()->toArray();
  373. if(empty($result)) return [false, '不良品单不存在或已被删除'];
  374. $first = $result[0] ?? [];
  375. $order['order_number'] = $first['order_number'];
  376. $order['process_title'] = Process::where('id',$first['process_id'])->value("title");
  377. $order['team_title'] = Team::where('id',$first['team_id'])->value("title");
  378. $order['finished_title'] = Employee::where('id',$first['finished_id'])->value("emp_name");
  379. $order['equipment_title'] = Equipment::where('id',$first['equipment_id'])->value("title");
  380. $dispatch = ReportWorking::where('id',$first['report_id'])->first();
  381. $dispatch = empty($dispatch) ? [] : $dispatch->toArray();
  382. $order['dispatch_no'] = $dispatch['order_number'] ?? "";
  383. $order['crt_time'] = date("Y-m-d",$first['crt_time']);
  384. $team_man = EmployeeTeamPermission::where('team_id',$first['team_id'])
  385. ->select('team_id','employee_id')
  386. ->get()->toArray();
  387. $emp_map = Employee::whereIn('id',array_column($team_man,'employee_id'))
  388. ->pluck('emp_name','id')
  389. ->toArray();
  390. $team_man_maps = "";
  391. foreach ($team_man as $value){
  392. $t = $emp_map[$value['employee_id']] ?? "";
  393. if(empty($t)) continue;
  394. if(! empty($team_man_maps)){
  395. $team_man_maps .= ',' . $t;
  396. }else{
  397. $team_man_maps = $t;
  398. }
  399. }
  400. $order['team_man'] = $team_man_maps;
  401. $scrapp = Scrapp::whereIn('id', array_unique(array_column($result,'scrapp_id')))
  402. ->pluck('title','id')
  403. ->toArray();
  404. $detail = [];
  405. foreach ($result as $value){
  406. $tmp = [
  407. 'product_no' => $first['product_no'],
  408. 'product_title' => $first['product_title'],
  409. 'product_size' => $first['product_size'],
  410. 'product_unit' => $first['product_unit'],
  411. 'technology_name' => $first['technology_name'],
  412. 'scrapp_quantity' => $value['scrapp_num'], // 不良品数量
  413. 'scrapp_title' => $scrapp[$value['scrapp_id']] ?? "", // 不良品原因
  414. ];
  415. $detail[] = $tmp;
  416. }
  417. $order['detail'] = $detail;
  418. return [true, $order];
  419. }
  420. public function processFlowList($data, $user){
  421. $model = OrdersProduct::where('del_time',0)
  422. ->select('id as order_number','production_no','out_order_no','production_time','customer_name','product_title','technology_name','product_size','product_unit','dispatch_complete_quantity as quantity','id')
  423. ->orderBy('id','desc');
  424. if(! empty($data['out_order_no'])) $model->where('out_order_no', 'LIKE', '%'.$data['out_order_no'].'%');
  425. if(! empty($data['production_no'])) $model->where('production_no', 'LIKE', '%'.$data['production_no'].'%');
  426. if(! empty($data['customer_name'])) $model->where('customer_name', 'LIKE', '%'.$data['customer_name'].'%');
  427. if(! empty($data['product_title'])) $model->where('product_title', 'LIKE', '%'.$data['product_title'].'%');
  428. if(! empty($data['product_size'])) $model->where('product_size', 'LIKE', '%'.$data['product_size'].'%');
  429. if(! empty($data['order_number'])) {
  430. $order_number = str_replace('FC','',$data['order_number']);
  431. $model->where('id', 'LIKE', '%'.$order_number.'%');
  432. }
  433. if(! empty($data['technology_name'])) $model->where('technology_name', 'LIKE', '%'.$data['technology_name'].'%');
  434. if(! empty($data['wood_name'])) $model->where('wood_name', 'LIKE', '%'.$data['wood_name'].'%');
  435. if(! empty($data['process_mark'])) $model->where('process_mark', 'LIKE', '%'.$data['process_mark'].'%');
  436. if(! empty($data['table_header_mark'])) $model->where('table_header_mark', 'LIKE', '%'.$data['table_header_mark'].'%');
  437. if(! empty($data['table_body_mark'])) $model->where('table_body_mark', 'LIKE', '%'.$data['table_body_mark'].'%');
  438. if(! empty($data['out_checker_man'])) $model->where('out_checker_man', 'LIKE', '%'.$data['out_checker_man'].'%');
  439. if(! empty($data['out_crt_man'])) $model->where('out_crt_man', 'LIKE', '%'.$data['out_crt_man'].'%');
  440. if(! empty($data['out_checker_time'][0]) && ! empty($data['out_checker_time'][1])) $model->whereBetween('out_checker_time',[$data['out_checker_time'][0],$data['out_checker_time'][1]]);
  441. 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]]);
  442. if(! empty($data['production_time'][0]) && ! empty($data['production_time'][1])) $model->whereBetween('production_time',[$data['production_time'][0],$data['production_time'][1]]);
  443. $list = $this->limit($model,'',$data);
  444. $list = $this->fillData($list, $user);
  445. return [200,$list];
  446. }
  447. public function fillData($data, $user){
  448. if(empty($data['data'])) return $data;
  449. foreach ($data['data'] as $key => $value){
  450. $data['data'][$key]['order_number'] = "FC". $value['order_number'];
  451. $data['data'][$key]['title'] = "工艺路线(默认)";
  452. $data['data'][$key]['production_time'] = $value['production_time'] ? date('Y-m-d',$value['production_time']) : '';
  453. }
  454. return $data;
  455. }
  456. public function processFlowDetail($data, $user){
  457. if(empty($data['order_number'])) return [false, '工艺流程卡单号不能为空'];
  458. $order_number = str_replace('FC','',$data['order_number']);
  459. $order = OrdersProduct::where('del_time',0)
  460. ->where('id',$order_number)
  461. ->select('id as order_number','production_no','out_order_no','production_time','customer_name','product_title','technology_name','product_size','product_unit','dispatch_complete_quantity as quantity', 'production_quantity')
  462. ->first();
  463. if(empty($order)) return [false, '工艺流程卡不存在或已被删除'];
  464. $order = $order->toArray();
  465. $id = $order['order_number'];
  466. $order['order_number'] = "FC". $order['order_number'];
  467. $order['not_quantity'] = bcsub($order['production_quantity'], $order['quantity'],3);
  468. $order['title'] = "工艺路线(默认)";
  469. $order['production_time'] = $order['production_time'] ? date('Y-m-d', $order['production_time']) : '';
  470. $dispatch = DispatchSub::where('del_time',0)
  471. ->where('order_product_id', $id)
  472. ->select('process_id','dispatch_quantity','finished_num','finished_num as bg_num','finished_num as hg_num','waste_num','team_id','device_id','dispatch_time_start','dispatch_time_end')
  473. ->get()->toArray();
  474. $team_map = Team::whereIn('id',array_unique(array_column($dispatch,'team_id')))
  475. ->pluck('title','id')
  476. ->toArray();
  477. $equipment_map = Equipment::whereIn('id',array_unique(array_column($dispatch,'device_id')))
  478. ->pluck('title','id')
  479. ->toArray();
  480. $process_map = Process::whereIn('id',array_column($dispatch,'process_id'))
  481. ->pluck('title','id')
  482. ->toArray();
  483. foreach ($dispatch as $key => $value){
  484. $time1 = $value['dispatch_time_start'] ? date('Y-m-d',$value['dispatch_time_start']) : '';
  485. $time2 = $value['dispatch_time_end'] ? date('Y-m-d',$value['dispatch_time_end']) : '';
  486. $dispatch[$key]['dispatch_time'] = $time1 . ' ' . $time2;
  487. $dispatch[$key]['process_name'] = $process_map[$value['process_id']] ?? '';
  488. $dispatch[$key]['team_name'] = $team_map[$value['team_id']] ?? "";
  489. $dispatch[$key]['device_name'] = $equipment_map[$value['device_id']] ?? "";
  490. }
  491. $order['table'] = $dispatch;
  492. return [true, $order];
  493. }
  494. public function bgOrZj($data, $user){
  495. // $order_number = (new ApplyOrderService())->setOrderNO2(0);dd($order_number);
  496. // // 1. 获取所有需要更新的记录(按 id 升序)
  497. // $records = ReportWorking::where('del_time', 0)
  498. // ->where('id', '>=', 10151)
  499. // ->orderBy('id') // 关键:按 id 排序
  500. // ->get();
  501. // // 2. 设置起始 order_number(你可以根据需要修改)
  502. // $startNumber = 202510101000;
  503. //
  504. // $updateData = [];
  505. //
  506. // foreach ($records as $index => $record) {
  507. // $newOrderNumber = $startNumber + $index;
  508. //
  509. // // 收集要更新的数据
  510. // $updateData[] = [
  511. // 'id' => $record->id,
  512. // 'order_number' => (string)$newOrderNumber, // 转为字符串保持类型一致
  513. // ];
  514. // }
  515. // foreach ($updateData as $value){
  516. // ReportWorking::where('id', $value['id'])->update(['order_number' => $value['order_number']]);
  517. // }
  518. // dd(1);
  519. if(empty($user['role'])) return [false, '当前账号暂未配置角色,无法确认操作报工或质检'];
  520. $role = $user['role'][0];
  521. if(! in_array($role,[9,12,13,14,15,16,17])) return [false, '当前账号角色信息错误'];
  522. if(empty($data['order_number'])) return [false, '工艺流程卡号不能为空'];
  523. $order_number = str_replace('FC','',$data['order_number']);
  524. $order = OrdersProduct::where('del_time',0)
  525. ->where('id',$order_number)
  526. ->select('id')
  527. ->first();
  528. if(empty($order)) return [false, '工艺流程卡不存在或已被删除'];
  529. $order = $order->toArray();
  530. $map = [
  531. 9 => 12, //清洗脱水
  532. 12 => 14, //注塑成型
  533. 15 => 11, //破碎分离
  534. 16 => 13, //挤出造粒
  535. 17 => 15, //混合搅拌
  536. 13 => 12,
  537. 14 => 14,
  538. ];
  539. $process_id = $map[$role];
  540. if(in_array($role, [9,12,15,16,17])){
  541. $type = 1;
  542. $dispatch_no = DispatchSub::where('del_time',0)
  543. ->where('order_product_id',$order['id'])
  544. ->where('process_id', $process_id)
  545. ->value('dispatch_no');
  546. list($status, $msg) = (new DispatchService())->dispatchOrderList(['dispatch_no' => $dispatch_no]);
  547. }else{
  548. $type = 2;
  549. $crt_time = DispatchSub::where('del_time',0)
  550. ->where('order_product_id',$order['id'])
  551. ->where('process_id', $process_id)
  552. ->value('crt_time');
  553. $crt_time = $crt_time ? $crt_time->timestamp : 0;
  554. $dispatch_id = DispatchSub::where('del_time',0)
  555. ->where('order_product_id',$order['id'])
  556. ->where('crt_time',$crt_time)
  557. ->pluck('id')->toArray();
  558. $report = ReportWorkingDetail::from('report_working_detail as a')
  559. ->join('scrapp_count as b','b.report_id','a.report_working_id')
  560. ->where('a.del_time',0)
  561. ->where('b.del_time',0)
  562. ->whereIn('a.data_id',$dispatch_id)
  563. ->where('a.process_id',$process_id)
  564. ->whereRaw('a.quantity > (b.quantity + b.scrapp_num)')
  565. ->select('a.report_working_id')
  566. ->first();
  567. if(empty($report)){
  568. $report = ReportWorkingDetail::from('report_working_detail as a')
  569. ->join('scrapp_count as b','b.report_id','a.report_working_id')
  570. ->where('a.del_time',0)
  571. ->where('b.del_time',0)
  572. ->whereIn('a.data_id',$dispatch_id)
  573. ->where('a.process_id',$process_id)
  574. ->select('a.report_working_id')
  575. ->first();
  576. }
  577. if(empty($report)){
  578. $status = true;
  579. $msg = (object)[];
  580. }else{
  581. $report = $report->toArray();
  582. list($status, $msg) = (new ApplyOrderService())->reportWorkingDetail(['id' => $report['report_working_id']]);
  583. }
  584. }
  585. return [$status, ['type' => $type, 'order' => $msg]];
  586. }
  587. }