FinishedOrderService.php 73 KB

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