FinishedOrderService.php 56 KB

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