FinishedOrderService.php 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483
  1. <?php
  2. namespace App\Service;
  3. use App\Jobs\ProcessDataJob;
  4. use App\Model\Box;
  5. use App\Model\BoxDetail;
  6. use App\Model\Dispatch;
  7. use App\Model\DispatchSub;
  8. use App\Model\Employee;
  9. use App\Model\EmployeeTeamPermission;
  10. use App\Model\Equipment;
  11. use App\Model\FinishedOrder;
  12. use App\Model\FinishedOrderScrapp;
  13. use App\Model\FinishedOrderSub;
  14. use App\Model\OrdersProduct;
  15. use App\Model\OrdersProductProcess;
  16. use App\Model\Process;
  17. use App\Model\ReportWorking;
  18. use App\Model\ReportWorkingDetail;
  19. use App\Model\SaleOrdersProduct;
  20. use App\Model\Scrapp;
  21. use App\Model\ScrappCount;
  22. use App\Model\Team;
  23. use App\Service\Box\BoxService;
  24. use Illuminate\Support\Facades\DB;
  25. use Illuminate\Support\Facades\Redis;
  26. class FinishedOrderService extends Service
  27. {
  28. public function edit($data){}
  29. public function getZj($data, $user){
  30. if(empty($data['id'])) return [false, '完工单ID不能为空'];
  31. if(empty($data['process_id'])) return [false, '工序ID不能为空'];
  32. $old = ScrappCount::where('del_time',0)
  33. ->where('dispatch_sub_id', $data['id'])
  34. ->where('process_id', $data['process_id'])
  35. ->select('scrapp_id','scrapp_num as num','process_id')
  36. ->get()->toArray();
  37. $return = [];
  38. foreach ($old as $value){
  39. $key = $value['scrapp_id'] . $value['process_id'];
  40. if(isset($return[$key])){
  41. $return[$key]['num'] += $value['num'];
  42. }else{
  43. $return[$key] = [
  44. 'scrapp_id' => $value['scrapp_id'],
  45. 'num' => $value['num'],
  46. 'process_id' => $value['process_id'],
  47. ];
  48. }
  49. }
  50. return [true, ['save' => array_values($return)]];
  51. }
  52. public function editZj($data, $user){
  53. list($status, $msg) = $this->editZjRule($data);
  54. if(! $status) return [false, $msg];
  55. $order = $msg;
  56. try{
  57. DB::beginTransaction();
  58. $time = time();
  59. //更新次品数量
  60. $num = 0;
  61. if(! empty($data['waste'])){
  62. foreach ($data['waste'] as $value){
  63. $num += $value['num'];
  64. }
  65. }
  66. DispatchSub::where('id',$order['id'])->update([
  67. 'waste_num' => $num,
  68. ]);
  69. $insert = [];
  70. foreach ($data['waste'] as $value){
  71. $insert[] = [
  72. 'sale_orders_product_id' => $order['sale_orders_product_id'],
  73. 'order_product_id' => $order['order_product_id'],
  74. 'dispatch_sub_id' => $order['id'],
  75. 'out_order_no' => $order['out_order_no'],
  76. 'order_no' => $order['order_no'],
  77. 'customer_no' => $order['customer_no'],
  78. 'customer_name' => $order['customer_name'],
  79. 'product_no' => $order['product_no'],
  80. 'product_title' => $order['product_title'],
  81. 'product_size' => $order['product_size'],
  82. 'product_unit' => $order['product_unit'],
  83. 'technology_material' => $order['technology_material'],
  84. 'technology_name' => $order['technology_name'],
  85. 'wood_name' => $order['wood_name'],
  86. 'price' => $order['price'],
  87. 'process_mark' => $order['process_mark'],
  88. 'table_body_mark' => $order['table_body_mark'],
  89. 'table_header_mark' => $order['table_header_mark'],
  90. 'crt_id' => $user['id'],
  91. 'crt_time' => $time,
  92. 'scrapp_num' => $value['num'],
  93. 'scrapp_id' => $value['scrapp_id'],
  94. 'team_id' => $data['team_id'],
  95. 'finished_id' => 0,
  96. 'equipment_id' => $data['equipment_id'],
  97. 'order_number' => $order['id'] . $time . $order['process_id'],
  98. 'process_id' => $order['process_id'],
  99. 'quantity' => $order['finished_num'],
  100. ];
  101. }
  102. //质检单 不良品单
  103. ScrappCount::where('del_time',0)
  104. ->where('dispatch_sub_id', $order['id'])
  105. ->where('process_id',$order['process_id'])
  106. ->update(['del_time' => $time]);
  107. if(! empty($insert)) ScrappCount::insert($insert);
  108. DB::commit();
  109. }catch (\Throwable $exception){
  110. DB::rollBack();
  111. return [false, $exception->getMessage()];
  112. }
  113. return [true, ''];
  114. }
  115. public function editZjRule($data){
  116. if(empty($data['id'])) return [false, '完工ID不能为空'];
  117. $order = DispatchSub::where('del_time',0)
  118. ->where('id',$data['id'])
  119. ->first();
  120. if(empty($order)) return [false, '完工单不存在或已被删除'];
  121. $order = $order->toArray();
  122. if(empty($data['team_id'])) return [false, '质检班组不能为空'];
  123. // if(empty($value['finished_id'])) return [false, '质检人员不能为空'];
  124. if(empty($data['equipment_id'])) return [false, '设备不能为空'];
  125. // if(empty($data['waste'])) return [false, '质检信息不能为空'];
  126. // if(! empty($data['waste'])){
  127. // $num = 0;
  128. // foreach ($data['waste'] as $value){
  129. // if(empty($value['num'])) return [false, '质检损耗数量不能为空'];
  130. // if(empty($value['scrapp_id'])) return [false, '质检损耗原因不能为空'];
  131. // $num += $value['num'];
  132. // }
  133. //
  134. // if($num > $order['finished_num']) return [false, '质检总数不能超过完工数量'];
  135. // }
  136. return [true, $order];
  137. }
  138. public function setOrderNO(){
  139. $str = date('Ymd',time());
  140. $order_number = FinishedOrder::where('finished_no','Like','%'. $str . '%')
  141. ->max('finished_no');
  142. if(empty($order_number)){
  143. $number = str_pad(1,3,'0',STR_PAD_LEFT);
  144. $number = $str . $number;
  145. }else{
  146. $tmp = substr($order_number, -3);
  147. $tmp = $tmp + 1;
  148. //超过99999
  149. if(strlen($tmp) > 3) return '';
  150. $number = str_pad($tmp,3,'0',STR_PAD_LEFT);
  151. $number = $str . $number;
  152. }
  153. return $number;
  154. }
  155. public function add($data,$user){return [false, '接口功能已关闭'];
  156. //数据校验以及填充
  157. list($status,$msg) = $this->orderRule($data);
  158. if(!$status) return [$status,$msg];
  159. //数据源
  160. $result = $msg;
  161. $time = time();
  162. try{
  163. DB::beginTransaction();
  164. $em = new EmployeeService();
  165. $auto = $em->is_auto($user, "wg_auto");
  166. //班组下的人
  167. $team_man = [];
  168. $team_array = EmployeeTeamPermission::select('employee_id', 'team_id')->get()->toArray();
  169. foreach ($team_array as $v){
  170. $team_man[$v['team_id']][] = $v['employee_id'];
  171. }
  172. $last_update = $last_update2 = $str_tmp = [];
  173. foreach ($result as $value){
  174. $finished_num = $value['quantity'] + $value['finished_num'];
  175. $waste_num = $value['waste_quantity'] + $value['waste_num'];
  176. DispatchSub::where('id',$value['id'])->update([
  177. 'finished_num' => $finished_num,
  178. 'waste_num' => $waste_num,
  179. 'wg_status' => $auto,
  180. ]);
  181. $tmp = [
  182. 'id' => $value['id'],
  183. 'quantity' => $value['quantity'],
  184. 'product_no' => $value['product_no'] ?? "",
  185. 'product_title' => $value['product_title'] ?? "",
  186. 'product_size' => $value['product_size'] ?? "",
  187. 'product_unit' => $value['product_unit'] ?? "",
  188. ];
  189. $str = $value['order_product_id'] . $value['crt_time'] . $value['product_no'] . $value['technology_name'];
  190. if(! in_array($str, $str_tmp)){
  191. $last_update[] = $tmp;
  192. $str_tmp[] = $str;
  193. }
  194. //班组下的人
  195. $man = $team_man[$value['team_id']] ?? [];
  196. $f_m = 0;
  197. if(! in_array($value['finish_id'], $man) && $value['finish_id'] > 0) {
  198. $man[] = $value['finish_id'];
  199. $f_m = 1;
  200. }
  201. $tmp['process_id'] = $value['process_id'];
  202. foreach ($man as $m_v){
  203. if($f_m && $m_v == $value['finish_id']){
  204. $tmp['team_id'] = 0;
  205. }else{
  206. $tmp['team_id'] = $value['team_id'];
  207. }
  208. $tmp['finished_id'] = $m_v;
  209. $last_update2[] = $tmp;
  210. }
  211. $insert_waste = $scrapp = [];
  212. if(! empty($value['waste_array'])){
  213. foreach ($value['waste_array'] as $k => $v){
  214. // $num = $v['num'] * 1000;
  215. // for($i = 0 ;$i < $num; $i++){
  216. // $insert_waste[] = [
  217. // 'order_product_id' => $value['order_product_id'],
  218. // 'order_no' => $value['order_no'],
  219. // 'product_no' => $value['product_no'],
  220. // 'product_title' => $value['product_title'],
  221. // 'process_id' => $value['process_id'],
  222. // 'crt_time' => $time,
  223. // 'dispatch_no' => $value['dispatch_no'],
  224. // 'status' => 4,//不良品
  225. // 'team_id' => $value['team_id'],
  226. // 'finished_id' => $value['finish_id'],
  227. // 'equipment_id' => $value['device_id'],
  228. // 'scrapp_id' => $v['scrapp_id']
  229. // ];
  230. // }
  231. $scrapp[] = [
  232. 'sale_orders_product_id' => $value['sale_orders_product_id'],
  233. 'order_product_id' => $value['order_product_id'],
  234. 'dispatch_sub_id' => $value['id'],
  235. 'out_order_no' => $value['out_order_no'],
  236. 'order_no' => $value['order_no'],
  237. 'customer_no' => $value['customer_no'],
  238. 'customer_name' => $value['customer_name'],
  239. 'product_no' => $value['product_no'],
  240. 'product_title' => $value['product_title'],
  241. 'product_size' => $value['product_size'],
  242. 'product_unit' => $value['product_unit'],
  243. 'technology_material' => $value['technology_material'],
  244. 'technology_name' => $value['technology_name'],
  245. 'wood_name' => $value['wood_name'],
  246. 'price' => $value['price'],
  247. 'process_mark' => $value['process_mark'],
  248. 'table_body_mark' => $value['table_body_mark'],
  249. 'table_header_mark' => $value['table_header_mark'],
  250. 'crt_id' => 1,
  251. 'crt_time' => $time,
  252. 'scrapp_num' => $v,
  253. 'scrapp_id' => $k,
  254. 'team_id' => $value['team_id'] ?? 0,
  255. 'finished_id' => $value['finish_id'] ?? 0,
  256. 'equipment_id' => $value['device_id'] ?? 0,
  257. 'order_number' => $value['id'] . $time . $value['process_id'],
  258. 'process_id' => $value['process_id'] ?? 0,
  259. 'quantity' => $value['quantity'],
  260. ];
  261. }
  262. }
  263. $quantity = $value['quantity'] * 1000;
  264. //工序表
  265. $process_model = new OrdersProductProcess(['channel' => date("Ymd",$value['out_order_no_time'])]);
  266. $process_model->where('order_product_id',$value['order_product_id'])
  267. ->where('process_id',$value['process_id'])
  268. ->where('dispatch_no',$value['dispatch_no'])
  269. ->where('status', 1)
  270. ->take($quantity)
  271. ->update([
  272. 'finished_time' => $time,
  273. 'status' => 2,
  274. 'finished_id' => $value['finish_id'] ?? 0,
  275. 'team_id' => $value['team_id'] ?? 0,
  276. 'equipment_id' => $value['device_id'] ?? 0,
  277. ]);
  278. if(! empty($insert_waste)) $process_model->insert($insert_waste);
  279. if(! empty($scrapp)) {
  280. $scrapp_model = new ScrappCount();
  281. $scrapp_model->insert($scrapp);
  282. }
  283. //
  284. // if(! empty($value['waste_quantity'])){
  285. // $num = $value['waste_quantity'];
  286. // OrdersProduct::where('id',$value['order_product_id'])->update([
  287. // 'scrapp_num' => DB::raw("scrapp_num + {$num}"),
  288. // ]);
  289. // }
  290. }
  291. //反写数量
  292. //生产订单
  293. $id = array_column($result,'order_product_id');
  294. $this->writeFinishedQuantityByOrdersProductId($id);
  295. //销售订单
  296. $this->writeFinishedQuantity($id);
  297. //生成完工入库申请单
  298. $service = new BoxService();
  299. list($status, $msg) = $service->createWGSQ($last_update, $user);
  300. if(! $status) return [false, $msg];
  301. //生成报工单
  302. list($status, $msg) = $this->createbg($last_update2, $user);
  303. if(! $status) return [false, $msg];
  304. DB::commit();
  305. }catch (\Exception $e){
  306. DB::rollBack();
  307. return [false,$e->getFile() . $e->getLine(). $e->getMessage()];
  308. }
  309. return [true, ''];
  310. }
  311. public function createbg($insert, $user,$dispatch_time = 0){
  312. //报工单
  313. $em = new EmployeeService();
  314. $auto = $em->is_auto($user, "wgbg_auto");
  315. $order_number = (new ApplyOrderService())->setOrderNO2($dispatch_time);
  316. if(empty($order_number)) return [false, '报工单编号生成失败'];
  317. try {
  318. DB::beginTransaction();
  319. if(empty($dispatch_time)){
  320. $time = time();
  321. }else{
  322. $time = $dispatch_time;
  323. }
  324. $model = new ReportWorking();
  325. $model->order_number = $order_number;
  326. $model->report_time = $time;
  327. $model->mark = $data['mark']?? "";
  328. $model->crt_id = $user['id'];
  329. $model->status = $auto;
  330. $model->save();
  331. $id = $model->id;
  332. $detail_insert = [];
  333. foreach ($insert as $v){
  334. $detail_insert[] = [
  335. 'report_working_id' => $id,
  336. 'data_id' => $v['id'],
  337. 'quantity' => $v['quantity'] ?? 0,
  338. 'finished_id' => $v['finished_id'] ?? 0,
  339. 'process_id' => $v['process_id'] ?? 0,
  340. 'team_id' => $v['team_id'] ?? 0,
  341. 'crt_time' => $time,
  342. ];
  343. }
  344. ReportWorkingDetail::insert($detail_insert);
  345. DB::commit();
  346. }catch (\Throwable $exception){
  347. DB::rollBack();
  348. return [false, $exception->getFile() . $exception->getMessage() . $exception->getLine()];
  349. }
  350. return [true, $id];
  351. }
  352. public function createZJFirst($report_id, $insert){
  353. foreach ($insert as $key => $value) {
  354. $t_report_id = $report_id[$value['dispatch_sub_id']] ?? 0;
  355. $insert[$key]['report_id'] = $t_report_id;
  356. }
  357. ScrappCount::insert($insert);
  358. return [true, ''];
  359. }
  360. public function createZJSecond($get_order_number, $user, $dispatch_time, $c_time,$max_id,$team_man,$report_order_number){
  361. try {
  362. $time = time();
  363. //获取新增的单据
  364. $result = ScrappCount::whereIn('order_number',$get_order_number)
  365. ->where('result','>',0)
  366. ->select('scrapp_num as num','id','dispatch_sub_id','warehouse_no','warehouse_title','result','product_no')
  367. ->get()->toArray();
  368. if(empty($result)) return [true, $get_order_number];
  369. $dispatchList = DispatchSub::whereIn('id', array_column($result,'dispatch_sub_id'))->get()->toArray();
  370. $dispatch_map = array_column($dispatchList,null,'id');
  371. //是否自动审核
  372. $em = new EmployeeService();
  373. $auto = $em->is_auto($user, "pg_auto");
  374. $dispatch_no = (new DispatchService())->setOrderNO($dispatch_time);
  375. if(! $dispatch_no) return [false,'返工派工单号生成失败!'];
  376. $prefix = substr($dispatch_no, 0, -3);
  377. $startNum = (int)substr($dispatch_no, -3);
  378. $is_first = true;
  379. $dispatch_main = $dispatch = $is_need_ll = $is_need_warehouse = $zj = [];
  380. foreach ($result as $value){
  381. //派工单
  382. $t = $dispatch_map[$value['dispatch_sub_id']] ?? [];
  383. if(! $is_first){
  384. $startNum += 1;
  385. $number = str_pad($startNum,3,'0',STR_PAD_LEFT);
  386. $number = $prefix . $number;
  387. $dispatch_no_t = $number;
  388. }else{
  389. $dispatch_no_t = $dispatch_no;
  390. $is_first = false;
  391. }
  392. if($value['result'] == ReportWorking::type_one){
  393. //重新入生产流程 需要生成返工派工单 报工单 质检单
  394. }else{
  395. //入仓库 那么需要生成生成返工派工单 报工单 质检单 质检入库申请单 生成领料申请单
  396. $is_need_warehouse[] = [
  397. 'warehouse_no' => $value['warehouse_no'],
  398. 'warehouse_title' => $value['warehouse_title'],
  399. ];
  400. $zj[] = [
  401. 'id' => $value['id'],
  402. 'quantity' => $value['num'],
  403. 'product_no' => $value['product_no'],
  404. ];
  405. }
  406. $is_need_ll[] = $dispatch_no_t;
  407. $dispatch_main[] = ['dispatch_no' => $dispatch_no_t,'crt_time' => $time];
  408. $dispatch[] = [
  409. 'order_product_id' => $t['order_product_id'],
  410. 'order_no' => $t['order_no'],
  411. 'product_no' => $t['product_no'],
  412. 'product_title' => $t['product_title'],
  413. 'product_size' => $t['product_size'],
  414. 'product_unit' => $t['product_unit'],
  415. 'production_quantity' => $t['production_quantity'],
  416. 'dispatch_quantity' => $value['num'],
  417. // 'finished_num' => $value['num'],
  418. 'wg_status' => 1,
  419. 'technology_material' => $t['technology_material'],
  420. 'technology_name' => $t['technology_name'],
  421. 'wood_name' => $t['wood_name'],
  422. 'process_mark' => $t['process_mark'],
  423. 'table_body_mark' => $t['table_body_mark'],
  424. 'table_header_mark' => $t['table_header_mark'],
  425. 'crt_time' => $dispatch_time,
  426. 'status' => $auto,
  427. 'dispatch_no' => $dispatch_no_t,
  428. 'process_id' => $t['process_id'],
  429. 'dispatch_time_start' => $t['dispatch_time_start'],
  430. 'dispatch_time_end' => $t['dispatch_time_end'],
  431. 'crt_id' => $user['id'],
  432. 'sale_orders_product_id' => $t['sale_orders_product_id'],
  433. 'out_order_no' => $t['out_order_no'],
  434. 'out_order_no_time' => $t['out_order_no_time'],
  435. 'price' => $t['price'],
  436. 'customer_no' => $t['customer_no'],
  437. 'customer_name' => $t['customer_name'],
  438. 'pre_shipment_time' => $t['pre_shipment_time'],
  439. 'team_id' => $t['team_id'],
  440. 'device_id' => $t['device_id'],
  441. 'type' => 1,
  442. ];
  443. }
  444. Dispatch::insert($dispatch_main);
  445. DispatchSub::insert($dispatch);
  446. $get_order_number1 = [];
  447. //获取上一次插入订单的所有数据
  448. $insert = DispatchSub::whereIn('dispatch_no', $is_need_ll)
  449. ->where('crt_time',$dispatch_time)
  450. ->where('crt_id',$user['id'])
  451. ->get()->toArray();
  452. $insert_bg = [];
  453. foreach ($insert as $value){
  454. $t = $team_man[$value['team_id']] ?? 0;
  455. $insert_bg[] = [
  456. 'id' => $value['id'],
  457. 'quantity' => $value['dispatch_quantity'],
  458. 'product_no' => $value['product_no'] ?? "",
  459. 'product_title' => $value['product_title'] ?? "",
  460. 'product_size' => $value['product_size'] ?? "",
  461. 'process_id' => $value['process_id'] ?? "",
  462. 'team_id' => $value['team_id'] ?? "",
  463. 'finished_id' => $t,
  464. ];
  465. }
  466. //生成报工单
  467. $service_box = new BoxService();
  468. $first_order_number = end($report_order_number);
  469. list($status, $msg) = $service_box->createbg($insert_bg, $user, $dispatch_time, $first_order_number);
  470. if(! $status) return [false, $msg];
  471. list($report_id, $report_order_number1) = $msg;
  472. //生成质检单
  473. $service1 = new ApplyOrderService();
  474. $date = date("Ymd", $c_time);
  475. $result = [];
  476. foreach ($insert as $value){
  477. //质检
  478. $max_id += 1;
  479. $order_number = $date . str_pad($max_id,3,'0',STR_PAD_LEFT);
  480. $get_order_number1[] = $order_number;
  481. $t_report_id = $report_id[$value['id']] ?? 0;
  482. $result[] = [
  483. 'sale_orders_product_id' => $value['sale_orders_product_id'],
  484. 'order_product_id' => $value['order_product_id'],
  485. 'dispatch_sub_id' => $value['id'],
  486. 'out_order_no' => $value['out_order_no'],
  487. 'order_no' => $value['order_no'],
  488. 'customer_no' => $value['customer_no'],
  489. 'customer_name' => $value['customer_name'],
  490. 'product_no' => $value['product_no'],
  491. 'product_title' => $value['product_title'],
  492. 'product_size' => $value['product_size'],
  493. 'product_unit' => $value['product_unit'],
  494. 'technology_material' => $value['technology_material'],
  495. 'technology_name' => $value['technology_name'],
  496. 'wood_name' => $value['wood_name'],
  497. 'price' => $value['price'],
  498. 'process_mark' => $value['process_mark'],
  499. 'table_body_mark' => $value['table_body_mark'],
  500. 'table_header_mark' => $value['table_header_mark'],
  501. 'crt_id' => $user['id'],
  502. 'crt_time' => $c_time,
  503. 'team_id' => 73,
  504. 'finished_id' => $user['id'],
  505. 'equipment_id' => $value['device_id'],
  506. 'order_number' => $order_number,
  507. 'process_id' => $value['process_id'],
  508. 'quantity' => $value['dispatch_quantity'],
  509. 'report_id' => $t_report_id,
  510. 'daily_id' => $max_id,
  511. ];
  512. }
  513. ScrappCount::insert($result);
  514. if(! empty($is_need_warehouse)){
  515. //领料申请单
  516. list($status, $msg) = (new DispatchService())->createSQ($insert, $user, $dispatch_time);
  517. if(! $status) return [false, $msg];
  518. //质检入库申请单 原材料
  519. list($status, $msg) = $service1->createZJSQ($zj, $user, $is_need_warehouse, $c_time);
  520. if(! $status) return [false, $msg];
  521. }
  522. return [true, array_merge($get_order_number, $get_order_number1)];
  523. }catch (\Throwable $exception){
  524. return[false, $exception->getMessage() . $exception->getLine()];
  525. }
  526. }
  527. public function isNeedZJ($sale_orders_product_id = []){
  528. $sale_order = SaleOrdersProduct::whereIn('id', $sale_orders_product_id)
  529. ->where('order_quantity','>=', 20)
  530. ->select('id', 'order_quantity')
  531. ->get()->toArray();
  532. if(empty($sale_order)) return [false, ''];
  533. $need = [];
  534. $sale_order_id = array_column($sale_order,'id');
  535. //每个销售订单有4次的不良品创建需要
  536. foreach ($sale_order_id as $value){
  537. $need[$value] = 4;
  538. }
  539. $scrapp = ScrappCount::where("del_time",0)
  540. ->whereIn("sale_orders_product_id",$sale_order_id)
  541. ->where('scrapp_num','>',0)
  542. ->select('sale_orders_product_id','process_id')
  543. ->orderBy('process_id','asc')
  544. ->get()->toArray();
  545. $orderBy = [];
  546. foreach ($scrapp as $value){
  547. if(isset($need[$value['sale_orders_product_id']])) $need[$value['sale_orders_product_id']] -= 1;
  548. $orderBy[$value['sale_orders_product_id']][] = $value['process_id'];
  549. }
  550. $count = [];
  551. foreach ($need as $key => $value){
  552. if($value > 0) $count[$key] = $value;
  553. }
  554. //是否需要创建
  555. if(empty($count)) return [false, ''];
  556. $return = [];
  557. $process_id = [12,12,14,14];
  558. foreach ($count as $key => $value){
  559. $p_t = $orderBy[$key] ?? [];
  560. $p_t_k = count($p_t) ?? 0;
  561. if(isset($process_id[$p_t_k])){
  562. $t = $process_id[$p_t_k];
  563. $return[$key] = $t;
  564. }
  565. }
  566. return [true, $return];
  567. }
  568. //产成品入库
  569. public function U8Rdrecord10Save($package_data, $user){
  570. if(empty($package_data)) return [false, '产成品入库为空,请确认!'];
  571. try{
  572. //获取包装单信息
  573. $box = $package_data;
  574. $boxDetail = new BoxDetail(['channel'=>$box['top_order_no']]);
  575. $boxDetail = $boxDetail->where('del_time',0)
  576. ->where('order_no',$box['order_no'])
  577. ->select('id','top_id','num','ext_1','ext_2','ext_3','ext_4','ext_5','out_order_no','box_type','price')
  578. ->get()->toArray();
  579. //用友数据插入------------
  580. if(! empty($boxDetail)){
  581. $sqlServerModel = new FyySqlServerService($user);
  582. if($sqlServerModel->error) return [false, $sqlServerModel->error];
  583. $username = $sqlServerModel->getYongyouName();
  584. $box['create_name'] = $username;
  585. list($status,$msg) = $sqlServerModel->U8Rdrecord10Save($box,$boxDetail);
  586. if(! $status) return [false, $msg];
  587. }
  588. //用友数据插入------------
  589. }catch (\Exception $e){
  590. return [false,$e->getMessage() . '|' . $e->getFile() . '|' . $e->getLine()];
  591. }
  592. return [true,''];
  593. }
  594. //销售发货出库
  595. public function U8Rdrecord32Save($send_data, $user){
  596. if(empty($send_data)) return [false, '销售发货出库为空,请确认!'];
  597. try{
  598. //用友数据插入------------
  599. $sqlServerModel = new FyySqlServerService($user);
  600. if($sqlServerModel->error) return [false, $sqlServerModel->error];
  601. $create_name = $sqlServerModel->getYongyouName();
  602. list($status,$msg) = $sqlServerModel->U8Rdrecord32Save($send_data,$create_name);
  603. if(! $status) return [false, $msg];
  604. //用友数据插入------------
  605. }catch (\Exception $e){
  606. return [false,$e->getMessage() . '|' . $e->getFile() . '|' . $e->getLine()];
  607. }
  608. return [true,''];
  609. }
  610. public function addInJob($result,$data,$user){
  611. try{
  612. //获取数据库连接
  613. $database = $this->getConnectionName($user['zt']);
  614. //用友数据插入------------
  615. $insert_sql_server = [];
  616. foreach ($result as $key => $value){
  617. $quantity_tmp = $data['quantity'][$key];
  618. $result[$key]['quantity'] = $quantity_tmp;
  619. //工序表
  620. $process_model = new OrdersProductProcess(['channel' => date("Ymd",$value['out_order_no_time'])]);
  621. $process_model->setConnection($database);
  622. $process_id = $process_model->select('process_id')
  623. ->where('sort',$process_model->where('del_time',0)
  624. ->where('order_product_id',$value['order_product_id'])
  625. ->max('sort'))
  626. ->first();
  627. if(empty($process_id)) return [false,"未找到最后一道工序"];
  628. $process_id = $process_id->process_id;
  629. if($process_id == $value['process_id']){
  630. $insert_sql_server[] = $result[$key];
  631. }
  632. }
  633. if(! empty($insert_sql_server)){
  634. $sqlServerModel = new FyySqlServerService($user);
  635. if($sqlServerModel->error) return [false,$sqlServerModel->error];
  636. foreach ($insert_sql_server as $value){
  637. // list($status,$msg) = $sqlServerModel->U8Rdrecord10Save($value);
  638. // if(! $status) return [false,$msg];
  639. }
  640. }
  641. //用友数据插入结束----------
  642. //本地数据更新
  643. DB::beginTransaction();
  644. $waste = [];
  645. foreach ($data['waste'] as $key => $value){
  646. $waste[$key] = array_sum(array_column($value,'num'));
  647. }
  648. $time = time();
  649. foreach ($result as $key => $value){
  650. $finished_id_tmp = $data['finish_id'][$key];
  651. $team_tmp = $data['team_id'][$key];
  652. $equipment_id_tmp = $data['equipment_id'][$key];
  653. $finished_num = $value['quantity'] + $value['finished_num'];
  654. $model = new DispatchSub();
  655. $model->setConnection($database);
  656. $model->where('id',$value['id'])->update([
  657. 'finished_num' => $finished_num,
  658. 'waste_num' => $waste[$key],
  659. 'job_status' => 0,
  660. 'dispatch_quantity' => DB::raw("dispatch_quantity + {$waste[$key]}"),//派工数量增加 派工单可以继续派送
  661. ]);
  662. $insert_waste = $insert_dispatch = $scrapp = [];
  663. if(! empty($data['waste'][$key])){
  664. foreach ($data['waste'][$key] as $v){
  665. for($i = 0 ;$i < $v['num']; $i++){
  666. $insert_waste[] = [
  667. 'order_product_id' => $value['order_product_id'],
  668. 'order_no' => $value['order_no'],
  669. 'product_no' => $value['product_no'],
  670. 'product_title' => $value['product_title'],
  671. 'process_id' => $value['process_id'],
  672. 'crt_time' => $time,
  673. 'dispatch_no' => $value['dispatch_no'],
  674. 'status' => 4,//不良品
  675. 'team_id' => $team_tmp,
  676. 'finished_id' => $finished_id_tmp,
  677. 'equipment_id' => $equipment_id_tmp,
  678. 'scrapp_id' => $v['scrapp_id']
  679. ];
  680. $insert_dispatch[] = [
  681. 'order_product_id' => $value['order_product_id'],
  682. 'out_order_no' => $value['out_order_no'],
  683. 'order_no' => $value['order_no'],
  684. 'product_no' => $value['product_no'],
  685. 'product_title' => $value['product_title'],
  686. 'process_id' => $value['process_id'],
  687. 'crt_time' => $time,
  688. 'dispatch_no' => $value['dispatch_no'],
  689. 'status' => 1, //已派工
  690. 'team_id' => 0,
  691. 'finished_id' => 0,
  692. 'equipment_id' => 0,
  693. 'scrapp_id' => 0
  694. ];
  695. }
  696. $scrapp[] = [
  697. 'sale_orders_product_id' => $value['sale_orders_product_id'],
  698. 'order_product_id' => $value['order_product_id'],
  699. 'out_order_no' => $value['out_order_no'],
  700. 'order_no' => $value['order_no'],
  701. 'customer_no' => $value['customer_no'],
  702. 'customer_name' => $value['customer_name'],
  703. 'product_no' => $value['product_no'],
  704. 'product_title' => $value['product_title'],
  705. 'product_size' => $value['product_size'],
  706. 'product_unit' => $value['product_unit'],
  707. 'technology_material' => $value['technology_material'],
  708. 'technology_name' => $value['technology_name'],
  709. 'wood_name' => $value['wood_name'],
  710. 'price' => $value['price'],
  711. 'process_mark' => $value['process_mark'],
  712. 'table_body_mark' => $value['table_body_mark'],
  713. 'table_header_mark' => $value['table_header_mark'],
  714. 'crt_time' => $time,
  715. 'scrapp_num' => $v['num'],
  716. 'scrapp_id' => $v['scrapp_id']
  717. ];
  718. }
  719. }
  720. //工序表
  721. $process_model = new OrdersProductProcess(['channel' => date("Ymd",$value['out_order_no_time'])]);
  722. $process_model->setConnection($database);
  723. $process_model->where('order_product_id',$value['order_product_id'])
  724. ->where('process_id',$value['process_id'])
  725. ->where('dispatch_no',$value['dispatch_no'])
  726. ->take($value['quantity'])
  727. ->update([
  728. 'finished_time' => $time,
  729. 'status' => 2,
  730. 'finished_id' => $finished_id_tmp,
  731. 'team_id' => $team_tmp,
  732. 'equipment_id' => $equipment_id_tmp
  733. ]);
  734. if(! empty($insert_waste)) $process_model->insert($insert_waste);
  735. if(! empty($insert_dispatch)) $process_model->insert($insert_dispatch);
  736. if(! empty($scrapp)) {
  737. $scrapp_model = new ScrappCount();
  738. $scrapp_model->setConnection($database);
  739. $scrapp_model->insert($scrapp);
  740. }
  741. //生产订单数量
  742. if(! empty($waste[$key])){
  743. $num = $waste[$key];
  744. $model2 = new OrdersProduct();
  745. $model2->setConnection($database);
  746. $model2->where('id',$value['order_product_id'])->update([
  747. 'production_quantity' => DB::raw("production_quantity + {$num}"),
  748. 'scrapp_num' => DB::raw("scrapp_num + {$num}"),
  749. 'dispatch_complete_quantity' => DB::raw("dispatch_complete_quantity + {$num}"),//已派工数量增加
  750. ]);
  751. }
  752. }
  753. //反写数量
  754. // $this->writeFinishedQuantity(array_column($result,'sale_orders_product_id'),$database);
  755. // $this->writeFinishedQuantityByOrdersProductId(array_column($result,'order_product_id'),$database);
  756. DB::commit();
  757. }catch (\Exception $e){
  758. DB::rollBack();
  759. return [false,$e->getFile() . $e->getLine(). $e->getMessage()];
  760. }
  761. return [true,''];
  762. }
  763. public function del($data){
  764. if($this->isEmpty($data,'id')) return [false,'ID不能为空!'];
  765. return [true,'删除成功'];
  766. }
  767. public function orderDetail($data){
  768. if($this->isEmpty($data,'id')) return [false,'ID不能为空!'];
  769. $first = DispatchSub::where('id',$data['id'])->first();
  770. $result = [];
  771. $scrapp = ScrappCount::where('del_time',0)
  772. ->where('order_product_id',$first->order_product_id)
  773. ->select('order_product_id','scrapp_num as num', 'scrapp_id')
  774. ->get()->toArray();
  775. foreach ($scrapp as $value){
  776. if(isset($result[$value['order_product_id']])){
  777. $tmp = bcadd($result[$value['scrapp_id']],$value['num'],3);
  778. $result[$value['scrapp_id']]['num'] = $tmp;
  779. }else{
  780. $result[$value['scrapp_id']] = [
  781. 'scrapp_id' => $value['scrapp_id'],
  782. 'num' => $value['num']
  783. ];
  784. }
  785. }
  786. $result = array_values($result);
  787. // $first = DispatchSub::where('id',$data['id'])->first();
  788. // $process_model = new OrdersProductProcess(['channel' => date("Ymd",$first->out_order_no_time)]);
  789. //
  790. // $result = $process_model->where('del_time',0)
  791. // ->where('dispatch_no',$first->dispatch_no)
  792. // ->where('order_product_id',$first->order_product_id)
  793. // ->where('status',4)
  794. // ->select(DB::raw('count(id) as num'),'scrapp_id')
  795. // ->groupBy('scrapp_id')
  796. // ->get()->toArray();
  797. $map = Scrapp::whereIn('id',array_column($result,'scrapp_id'))
  798. ->pluck('title','id')
  799. ->toArray();
  800. foreach ($result as $key => $value){
  801. $result[$key]['scrapp_name'] = $map[$value['scrapp_id']] ?? '';
  802. }
  803. return [true,$result];
  804. }
  805. public function is_same_month($timestamp1,$timestamp2){
  806. // 格式化时间戳为年份和月份
  807. $year1 = date('Y', $timestamp1);
  808. $month1 = date('m', $timestamp1);
  809. $year2 = date('Y', $timestamp2);
  810. $month2 = date('m', $timestamp2);
  811. if ($year1 === $year2 && $month1 === $month2) {
  812. return true;
  813. } else {
  814. return false;
  815. }
  816. }
  817. public function orderRule($data){
  818. if(! isset($data['is_finish_all'])) return [false, '完工类型不能为空!'];
  819. if(empty($data['detail'])) return [false, '完工明细数据不能为空!'];
  820. $detail_map = $id = $detail_map2 = $waste_map = [];
  821. //班组下的人
  822. $team_man = [];
  823. $team_array = EmployeeTeamPermission::select('employee_id', 'team_id')->get()->toArray();
  824. foreach ($team_array as $v){
  825. $team_man[$v['team_id']][] = $v['employee_id'];
  826. }
  827. foreach ($data['detail'] as $value){
  828. if(empty($value['id'])) return [false,'请选择完工数据!'];
  829. $id[] = $value['id'];
  830. if(empty($value['device_id'])) return [false, '请选择设备!'];
  831. if(empty($value['team_id'])) return [false, '班组必须选择'];
  832. if(empty($team_man[$value['team_id']])) return [false, '班组下的人不能为空,请设置'];
  833. // if(empty($value['team_id']) && empty($value['finish_id'])) return [false,'班组和人员必须选择一个!'];
  834. if(! is_numeric($value['quantity']) || $value['quantity'] < 0) return [false,'完工数量不能小于0!'];
  835. if(! empty($value['waste'])){
  836. foreach ($value['waste'] as $waste){
  837. if(empty($waste['scrapp_id'])) return [false,'请选择损耗原因!'];
  838. if(! is_numeric($waste['num']) || $waste['num'] < 0) return [false,'损耗数量不能小于0!'];
  839. if(isset($waste_map[$value['id']])){
  840. $waste_map[$value['id']] += $waste['num'];
  841. }else{
  842. $waste_map[$value['id']] = $waste['num'];
  843. }
  844. }
  845. }
  846. //完工数量
  847. if(isset($detail_map[$value['id']])){
  848. $detail_map[$value['id']] += $value['quantity'];
  849. }else{
  850. $detail_map[$value['id']] = $value['quantity'];
  851. }
  852. if(! isset($detail_map2[$value['id']])){
  853. $detail_map2[$value['id']] = $value;
  854. }
  855. }
  856. //校验
  857. $result = DispatchSub::where('del_time',0)
  858. ->whereIn('id',$id)
  859. ->select('id','finished_num','dispatch_quantity','out_order_no_time','process_id','dispatch_no','order_product_id','sale_orders_product_id','order_no','product_no','product_title','price','customer_name','technology_material','technology_name','wood_name','customer_no','out_order_no','waste_num','product_size','product_unit','process_mark','table_body_mark','table_header_mark','sale_orders_product_id','crt_time')
  860. ->get()->toArray();
  861. if(empty($result)) return [false, '派工单不存在或已被删除!'];
  862. $process_map = Process::whereIn('id',array_unique(array_column($result,'process_id')))->pluck('title','id')->toArray();
  863. $search = "";$args = [];
  864. foreach ($result as $key => $value){
  865. $detail2 = $detail_map2[$value['id']] ?? [];
  866. $result[$key]['device_id'] = $detail2['device_id'] ?? 0;
  867. $result[$key]['team_id'] = $detail2['team_id'] ?? 0;
  868. $result[$key]['finish_id'] = $detail2['finish_id'] ?? 0;
  869. $result[$key]['waste_quantity'] = $waste_map[$value['id']] ?? 0;
  870. $tmp = [];
  871. if(! empty($detail2['waste'])){
  872. foreach ($detail2['waste'] as $v){
  873. if(isset($tmp[$v['id']])){
  874. $n = bcadd($v['num'],$tmp[$v['scrapp_id']],3);
  875. $tmp[$v['scrapp_id']] = $n;
  876. }else{
  877. $tmp[$v['scrapp_id']] = $v['num'];
  878. }
  879. }
  880. }
  881. $result[$key]['waste_array'] = $tmp;
  882. $q = $detail_map[$value['id']] ?? 0;
  883. $quantity_tmp = bcadd($q,$value['finished_num'],3);
  884. if($quantity_tmp > $value['dispatch_quantity']) {
  885. $process_tmp = $process_map[$value['process_id']] ?? "";
  886. return [false,'派工单:' . $value['dispatch_no']. '的工序' . $process_tmp .'完工数量不能大于派工数量'];
  887. }
  888. $result[$key]['quantity'] = $q;
  889. $search_key = $value['order_product_id'] . $value['crt_time'];
  890. if($data['is_finish_all'] && ! in_array($search_key , $args)){
  891. $search .= "(order_product_id = {$value['order_product_id']} and crt_time = {$value['crt_time']}) OR ";
  892. $args[] = $search_key;
  893. }
  894. }
  895. $search = rtrim($search,'OR ');
  896. $search = "($search)";
  897. if($data['is_finish_all']){
  898. //系统帮助完工的数据
  899. $result2 = DispatchSub::where('del_time',0)
  900. ->whereNotIn('id',$id)
  901. ->whereColumn('dispatch_quantity', '>', 'finished_num')
  902. ->whereRaw($search)
  903. ->select('id','finished_num','dispatch_quantity','out_order_no_time','process_id','dispatch_no','order_product_id','sale_orders_product_id','order_no','product_no','product_title','price','customer_name','technology_material','technology_name','wood_name','customer_no','out_order_no','waste_num','product_size','product_unit','process_mark','table_body_mark','table_header_mark','sale_orders_product_id','crt_time')
  904. ->get()->toArray();
  905. if(! empty($result2)){
  906. foreach ($result2 as $key => $value){
  907. $not_finished_num = $value['dispatch_quantity'] - $value['finished_num'];
  908. $result2[$key]['quantity'] = $not_finished_num;
  909. $result2[$key]['waste_array'] = [];
  910. $result2[$key]['waste_quantity'] = 0;
  911. $result2[$key]['device_id'] = 0;
  912. $result2[$key]['team_id'] = 0;
  913. $result2[$key]['finish_id'] = 0;
  914. }
  915. }
  916. $result = array_merge_recursive($result, $result2);
  917. }
  918. return [true, $result];
  919. }
  920. public function orderList($data){
  921. $model = FinishedOrderSub::where('del_time',0)
  922. ->select('id','order_no','table_header_mark','product_no','product_title','product_size','product_unit','dispatch_quantity','technology_material','technology_name','wood_name','process_mark','table_body_mark','dispatch_quantity','finished_num','status','crt_id','process_id','equipment_id','team_id','dispatch_time_start','dispatch_time_end','dispatch_time','crt_time','dispatch_no')
  923. ->orderBy('upd_time','desc')
  924. ->orderBy('id','desc');
  925. if(! empty($data['order_no'])) $model->where('order_no', 'LIKE', '%'.$data['order_no'].'%');
  926. if(! empty($data['product_title'])) $model->where('product_title', 'LIKE', '%'.$data['product_title'].'%');
  927. if(! empty($data['product_size'])) $model->where('product_size', 'LIKE', '%'.$data['product_size'].'%');
  928. if(! empty($data['technology_material'])) $model->where('technology_material', 'LIKE', '%'.$data['technology_material'].'%');
  929. if(! empty($data['technology_name'])) $model->where('technology_name', 'LIKE', '%'.$data['technology_name'].'%');
  930. if(! empty($data['wood_name'])) $model->where('wood_name', 'LIKE', '%'.$data['wood_name'].'%');
  931. if(! empty($data['process_mark'])) $model->where('process_mark', 'LIKE', '%'.$data['process_mark'].'%');
  932. if(! empty($data['table_header_mark'])) $model->where('table_header_mark', 'LIKE', '%'.$data['table_header_mark'].'%');
  933. if(! empty($data['table_body_mark'])) $model->where('table_body_mark', 'LIKE', '%'.$data['table_body_mark'].'%');
  934. if(! empty($data['dispatch_time'][0]) && ! empty($data['dispatch_time'][1])) $model->whereBetween('dispatch_time',[$data['dispatch_time'][0],$data['dispatch_time'][1]]);
  935. if(! empty($data['employee_id'])) {
  936. $team_id = Employee::from('employee as a')
  937. ->leftJoin('employee_team_permission as b','b.employee_id','a.id')
  938. ->where('a.id', $data['employee_id'])
  939. ->select('b.team_id')
  940. ->get()->toArray();
  941. $team_id = array_column($team_id,'team_id');
  942. $model->where('team_id',$team_id ?? []);
  943. }
  944. if(isset($data['status'])) $model->where('status',$data['status']);
  945. $list = $this->limit($model,'',$data);
  946. $list = $this->fillData($list);
  947. return [true,$list];
  948. }
  949. public function fillData($data){
  950. if(empty($data['data'])) return $data;
  951. $waste_map = $waste_map_2 = [];
  952. $waste = FinishedOrderScrapp::where('del_time',0)
  953. ->whereIn('finished_order_id',array_column($data['data'],'id'))
  954. ->select('finished_order_id','num','scrapp_id')
  955. ->get()->toArray();
  956. if(! empty($waste)){
  957. foreach ($waste as $value){
  958. $waste_map[$value['finished_order_id']][] = [
  959. 'num' => $value['num'],
  960. 'scrapp_id' => $value['scrapp_id']
  961. ];
  962. if(isset($waste_map_2[$value['finished_order_id']])){
  963. $waste_map_2[$value['finished_order_id']] += $value['num'];
  964. }else{
  965. $waste_map_2[$value['finished_order_id']] = $value['num'];
  966. }
  967. }
  968. }
  969. $team = EmployeeTeamPermission::from('employee_team_permission as a')
  970. ->leftJoin('employee as b','b.id','a.employee_id')
  971. ->whereIn('a.team_id',array_column($data['data'],'team_id'))
  972. ->select('b.emp_name','a.team_id')
  973. ->get()
  974. ->toArray();
  975. $team_map = [];
  976. if(! empty($team)){
  977. foreach ($team as $value){
  978. if(isset($team_map[$value['team_id']])){
  979. $team_map[$value['team_id']] .= ','. $value['emp_name'];
  980. }else{
  981. $team_map[$value['team_id']] = $value['emp_name'];
  982. }
  983. }
  984. }
  985. $process_map = Process::whereIn('id',array_column($data['data'],'process_id'))
  986. ->pluck('title','id')
  987. ->toArray();
  988. $team_maps = Team::whereIn('id',array_column($data['data'],'team_id'))
  989. ->pluck('title','id')
  990. ->toArray();
  991. $equipment_map = Equipment::whereIn('id',array_column($data['data'],'equipment_id'))
  992. ->pluck('title','id')
  993. ->toArray();
  994. foreach ($data['data'] as $key => $value){
  995. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d',$value['crt_time']) : '';
  996. $time1 = $value['dispatch_time_start'] ? date('Y-m-d',$value['dispatch_time_start']) : '';
  997. $time2 = $value['dispatch_time_end'] ? date('Y-m-d',$value['dispatch_time_end']) : '';
  998. $data['data'][$key]['dispatch_plan_time'] = $time1 . ' ' . $time2;
  999. $data['data'][$key]['dispatch_time'] = $value['dispatch_time'] ? date('Y-m-d',$value['dispatch_time']) : '';
  1000. $data['data'][$key]['team_man'] = $team_map[$value['team_id']] ?? '';
  1001. $data['data'][$key]['process_name'] = $process_map[$value['process_id']] ?? '';
  1002. $data['data'][$key]['team_name'] = $team_maps[$value['team_id']] ?? '';
  1003. $data['data'][$key]['equipment_name'] = $equipment_map[$value['equipment_id']] ?? '';
  1004. $data['data'][$key]['waste'] = $waste_map[$value['id']] ?? [];
  1005. $data['data'][$key]['waste_quantity'] = $waste_map_2[$value['id']] ?? 0;
  1006. $data['data'][$key]['not_finished_num'] = $value['dispatch_quantity'] - $value['finished_num'];
  1007. }
  1008. $data['finished_num'] = $this->getTotal($data['data'], 'finished_num');
  1009. $data['dispatch_quantity'] = $this->getTotal($data['data'], 'dispatch_quantity');
  1010. $data['waste_quantity'] = $this->getTotal($data['data'], 'waste_quantity');
  1011. $data['not_finished_num'] = $this->getTotal($data['data'], 'not_finished_num');
  1012. return $data;
  1013. }
  1014. //反写销售订单完工数量
  1015. public function writeFinishedQuantity($orders_product_id){
  1016. if(empty($orders_product_id)) return;
  1017. $sale_orders_product_id = OrdersProduct::where('del_time',0)
  1018. ->whereIn('id', $orders_product_id)
  1019. ->select('sale_orders_product_id')
  1020. ->get()->toArray();
  1021. $sale_orders_product_id = array_unique(array_column($sale_orders_product_id,'sale_orders_product_id'));
  1022. $production_order = OrdersProduct::where('del_time',0)
  1023. ->whereIn('sale_orders_product_id', $sale_orders_product_id)
  1024. ->select(DB::raw("sum(finished_num) as finished_num"),'sale_orders_product_id')
  1025. ->groupby('sale_orders_product_id')
  1026. ->pluck('finished_num','sale_orders_product_id')
  1027. ->toArray();
  1028. foreach ($production_order as $sale_orders_product_id => $finished_num){
  1029. SaleOrdersProduct::where('id',$sale_orders_product_id)->update([
  1030. 'finished_num' => $finished_num,
  1031. ]);
  1032. }
  1033. }
  1034. //反写生产订单完工数量
  1035. public function writeFinishedQuantityByOrdersProductId($order_product_id){
  1036. if(empty($order_product_id)) return;
  1037. $result = DispatchSub::where('del_time',0)
  1038. ->whereIn('order_product_id',$order_product_id)
  1039. ->select('finished_num','order_product_id','crt_time','process_id')
  1040. ->get()
  1041. ->toArray();
  1042. if(empty($result)) return;
  1043. //生产订单
  1044. $production_order = OrdersProduct::where('del_time',0)
  1045. ->whereIn('id',array_unique(array_column($result,'order_product_id')))
  1046. ->select('process_id','id','production_quantity')
  1047. ->get()
  1048. ->toArray();
  1049. $judge = [];
  1050. foreach ($result as $value){
  1051. if(isset($judge[$value['order_product_id']][$value['process_id']])){
  1052. $judge[$value['order_product_id']][$value['process_id']] += $value['finished_num'];
  1053. }else{
  1054. $judge[$value['order_product_id']][$value['process_id']] = $value['finished_num'];
  1055. }
  1056. }
  1057. foreach ($production_order as $value){
  1058. //生产订单下的所有工序派工单
  1059. $tmp = $judge[$value['id']] ?? [];
  1060. if(empty($tmp)) continue;
  1061. //生产订单下的工序种类
  1062. $ori_process = explode(',',$value['process_id']);
  1063. $ori_process_count = count($ori_process);
  1064. //生产订单下的派工单的工序种类
  1065. $process = array_keys($tmp);
  1066. $process_count = count($process);
  1067. if($ori_process_count != $process_count) continue;
  1068. //获取工序里最小的数值
  1069. $min_finished_num = min($tmp);
  1070. //更新完工数量
  1071. OrdersProduct::where('id',$value['id'])->update([
  1072. 'finished_num' => $min_finished_num
  1073. ]);
  1074. }
  1075. }
  1076. public function mobileAdd($data,$user){return [false, '接口功能已关闭'];
  1077. //数据校验以及填充
  1078. list($status,$msg,$count_arr) = $this->orderMobileRule($data);
  1079. if(!$status) return [$status,$msg];
  1080. $user = [
  1081. 'id' => $user['id'],
  1082. 'operate_time' => time(),
  1083. 'zt' => Request()->header('zt'),
  1084. ];
  1085. $redis = [
  1086. 'result' => $msg,
  1087. 'data' => $data,
  1088. 'quantity_count' => $count_arr,
  1089. ];
  1090. $job = ProcessDataJob::dispatch($redis,$user,3)->onQueue(ProcessDataJob::job_one);
  1091. if(! $job) return [false,'任务没有进入队列!'];
  1092. //错误计数
  1093. Redis::hSet('order_failures', md5(json_encode($redis)), 0);
  1094. //标记进入队列的数据
  1095. DispatchSub::whereIn('id',array_column($msg,'id'))->update([
  1096. 'job_status' => 1,
  1097. ]);
  1098. return [true,'任务已进入队列!'];
  1099. }
  1100. public function addMobileInJob($result, $data, $count_arr,$user){
  1101. try{
  1102. //获取数据库连接
  1103. $database = $this->getConnectionName($user['zt']);
  1104. //用友数据写入------------
  1105. $insert_sql_server = [];
  1106. foreach ($result as $key => $value){
  1107. $quantity_tmp = $count_arr[$value['id']];
  1108. $result[$key]['quantity'] = $quantity_tmp;
  1109. $process_model = new OrdersProductProcess(['channel' => date("Ymd",$value['out_order_no_time'])]);
  1110. $process_model->setConnection($database);
  1111. $process_id = $process_model->select('process_id')
  1112. ->where('sort',$process_model->where('del_time',0)
  1113. ->where('order_product_id',$value['order_product_id'])
  1114. ->max('sort'))
  1115. ->first();
  1116. if(empty($process_id)) return [false,"未找到最后一道工序"];
  1117. $process_id = $process_id->process_id;
  1118. if($process_id == $value['process_id']){
  1119. $insert_sql_server[] = $result[$key];
  1120. }
  1121. }
  1122. if(! empty($insert_sql_server)){
  1123. $sqlServerModel = new FyySqlServerService($user);
  1124. if($sqlServerModel->error) return [false,$sqlServerModel->error];
  1125. foreach ($insert_sql_server as $value){
  1126. // list($status,$msg) = $sqlServerModel->U8Rdrecord10Save($value);
  1127. // if(! $status) return [false,$msg];
  1128. }
  1129. }
  1130. //用友数据写入结束------------
  1131. //本地数据写入-----------
  1132. DB::beginTransaction();
  1133. $waste = $waste2 = [];
  1134. foreach ($data as $value){
  1135. if(! empty($value['waste'])){
  1136. foreach ($value['waste'] as $v){
  1137. if(isset($waste[$value['id']])){
  1138. $waste[$value['id']] += $v['num'];
  1139. $waste2[$value['order_product_id']] += $v['num'];
  1140. }else{
  1141. $waste[$value['id']] = $v['num'];
  1142. $waste2[$value['order_product_id']] = $v['num'];
  1143. }
  1144. }
  1145. }
  1146. }
  1147. $time = time();
  1148. foreach ($result as $value){
  1149. $finished_num = $value['quantity'] + $value['finished_num'];
  1150. $watste_num = $waste[$value['id']] ?? 0;
  1151. $model = new DispatchSub();
  1152. $model->setConnection($database);
  1153. $model->where('id',$value['id'])->update([
  1154. 'finished_num' => $finished_num,
  1155. 'waste_num' => $watste_num,
  1156. 'job_status' => 0,
  1157. 'dispatch_quantity' => DB::raw("dispatch_quantity + {$watste_num}"),//派工数量增加 派工单可以继续派送
  1158. ]);
  1159. //生产订单数量
  1160. if(! empty($waste2[$value['order_product_id']])){
  1161. $num = $waste2[$value['order_product_id']];
  1162. $model2 = new OrdersProduct();
  1163. $model2->setConnection($database);
  1164. $model2->where('id',$value['order_product_id'])->update([
  1165. 'production_quantity' => DB::raw("production_quantity + {$num}"),
  1166. 'scrapp_num' => DB::raw("scrapp_num + {$num}"),
  1167. 'dispatch_complete_quantity' => DB::raw("dispatch_complete_quantity + {$num}"),//已派工数量增加
  1168. ]);
  1169. }
  1170. //损耗和派工 损耗统计
  1171. $insert_waste = $insert_dispatch = $scrapp = [];
  1172. //工序
  1173. $process_model = new OrdersProductProcess(['channel' => date("Ymd",$value['out_order_no_time'])]);
  1174. $process_model->setConnection($database);
  1175. foreach ($data as $d){
  1176. if($d['id'] == $value['id']){
  1177. $process_model->where('order_product_id',$value['order_product_id'])
  1178. ->where('process_id',$value['process_id'])
  1179. ->where('dispatch_no',$value['dispatch_no'])
  1180. ->take($d['quantity'])
  1181. ->update([
  1182. 'finished_time' => $time,
  1183. 'status' => 2,
  1184. 'finished_id' => $d['finished_id'] ?? 0,
  1185. 'team_id' => $d['team_id'] ?? 0,
  1186. 'equipment_id' => $d['equipment_id'] ?? 0
  1187. ]);
  1188. if(! empty($d['waste'])){
  1189. foreach ($d['waste'] as $v){
  1190. for($i = 0 ;$i < $v['num']; $i++){
  1191. $insert_waste[] = [
  1192. 'order_product_id' => $value['order_product_id'],
  1193. 'out_order_no' => $value['out_order_no'],
  1194. 'order_no' => $value['order_no'],
  1195. 'product_no' => $value['product_no'],
  1196. 'product_title' => $value['product_title'],
  1197. 'process_id' => $value['process_id'],
  1198. 'crt_time' => $time,
  1199. 'dispatch_no' => $value['dispatch_no'],
  1200. 'status' => 4,
  1201. 'team_id' => $d['team_id'],
  1202. 'finished_id' => $d['finished_id'] ?? 0,
  1203. 'equipment_id' => $d['equipment_id'] ?? 0,
  1204. 'scrapp_id' => $v['scrapp_id'] ?? 0
  1205. ];
  1206. $insert_dispatch[] = [
  1207. 'order_product_id' => $value['order_product_id'],
  1208. 'out_order_no' => $value['out_order_no'],
  1209. 'order_no' => $value['order_no'],
  1210. 'product_no' => $value['product_no'],
  1211. 'product_title' => $value['product_title'],
  1212. 'process_id' => $value['process_id'],
  1213. 'crt_time' => $time,
  1214. 'dispatch_no' => $value['dispatch_no'],
  1215. 'status' => 1, //已派工
  1216. 'team_id' => 0,
  1217. 'finished_id' => 0,
  1218. 'equipment_id' => 0,
  1219. 'scrapp_id' => 0
  1220. ];
  1221. }
  1222. $scrapp[] = [
  1223. 'sale_orders_product_id' => $value['sale_orders_product_id'],
  1224. 'order_product_id' => $value['order_product_id'],
  1225. 'out_order_no' => $value['out_order_no'],
  1226. 'order_no' => $value['order_no'],
  1227. 'customer_no' => $value['customer_no'],
  1228. 'customer_name' => $value['customer_name'],
  1229. 'product_no' => $value['product_no'],
  1230. 'product_title' => $value['product_title'],
  1231. 'product_size' => $value['product_size'],
  1232. 'product_unit' => $value['product_unit'],
  1233. 'technology_material' => $value['technology_material'],
  1234. 'technology_name' => $value['technology_name'],
  1235. 'wood_name' => $value['wood_name'],
  1236. 'price' => $value['price'],
  1237. 'process_mark' => $value['process_mark'],
  1238. 'table_body_mark' => $value['table_body_mark'],
  1239. 'table_header_mark' => $value['table_header_mark'],
  1240. 'crt_time' => $time,
  1241. 'scrapp_num' => $v['num'],
  1242. 'scrapp_id' => $v['scrapp_id']
  1243. ];
  1244. }
  1245. }
  1246. }
  1247. }
  1248. if(! empty($insert_waste)) $process_model->insert($insert_waste);
  1249. if(! empty($insert_dispatch)) $process_model->insert($insert_dispatch);
  1250. if(! empty($scrapp)) {
  1251. $scrapp_model = new ScrappCount();
  1252. $scrapp_model->setConnection($database);
  1253. $scrapp_model->insert($scrapp);
  1254. }
  1255. }
  1256. //反写数量
  1257. // $this->writeFinishedQuantity(array_column($result,'sale_orders_product_id'),$database);
  1258. // $this->writeFinishedQuantityByOrdersProductId(array_column($result,'order_product_id'),$database);
  1259. DB::commit();
  1260. //本地数据写入结束-----------
  1261. }catch (\Exception $e){
  1262. DB::rollBack();
  1263. return [false,$e->getFile() . $e->getLine() . $e->getMessage()];
  1264. }
  1265. return [true,''];
  1266. }
  1267. public function orderMobileRule($data){
  1268. if(empty($data)) return [false,'数据不能为空!',''];
  1269. $dispatch_id = array_unique(array_column($data,'id'));
  1270. $bool = DispatchSub::whereIn('id',$dispatch_id)->where('job_status',1)->exists();
  1271. if($bool) return [false,'正在队列中,请不要重复操作!',''];
  1272. $post = $waste = [];
  1273. foreach ($data as $value){
  1274. if(! is_numeric($value['quantity']) || $value['quantity'] <= 0) return [false,'请填写正确的完工数量!',''];
  1275. if(empty($value['finished_id']) && empty($value['team_id'])) return [false,'人员和班组必须填写一项!',''];
  1276. if(isset($post[$value['id']])){
  1277. $post[$value['id']] += $value['quantity'];
  1278. }else{
  1279. $post[$value['id']] = $value['quantity'];
  1280. }
  1281. if(! empty($value['waste'])){
  1282. foreach ($value['waste'] as $v){
  1283. if(isset($waste[$value['id']])){
  1284. $waste[$value['id']] += $v['num'];
  1285. }else{
  1286. $waste[$value['id']] = $v['num'];
  1287. }
  1288. }
  1289. }
  1290. }
  1291. $result = DispatchSub::whereIn('id',$dispatch_id)
  1292. ->select('id','finished_num','dispatch_quantity','out_order_no_time','process_id','dispatch_no','order_product_id','sale_orders_product_id','order_no','product_no','product_title','price','customer_name','technology_material','technology_name','wood_name','customer_no','out_order_no','waste_num','product_size','product_unit','process_mark','table_body_mark','table_header_mark','sale_orders_product_id')
  1293. ->orderBy('id','desc')
  1294. ->get()->toArray();
  1295. foreach ($result as $key => $value){
  1296. $tmp = $waste[$value['id']] ?? 0;
  1297. $quantity_tmp = $post[$value['id']] + $value['finished_num'] + $value['waste_num'] + $tmp;
  1298. if($quantity_tmp > $value['dispatch_quantity']) return [false,"完工数量加上损耗数量不能大于派工数量",''];
  1299. }
  1300. return [true, $result, $post];
  1301. }
  1302. public function getConnectionName($name)
  1303. {
  1304. $mysql = "mysql";
  1305. if ($name === '001') {
  1306. $mysql = "mysql_001";
  1307. } elseif ($name === '002') {
  1308. $mysql = "mysql_002";
  1309. }
  1310. return $mysql;
  1311. }
  1312. public function insertYy($order, $user){
  1313. //包装单
  1314. $package_data = json_decode($order['post'], true);
  1315. if(! empty($package_data)) {
  1316. //用友 ------产成品入库
  1317. $service = new FinishedOrderService();
  1318. list($status,$msg) = $service->U8Rdrecord10Save($package_data,$user);
  1319. if(! $status) return [false, $msg];
  1320. }
  1321. return [true, ''];
  1322. }
  1323. }