FinishedOrderService.php 73 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577
  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. 'zj_plan_id' => $t['zj_plan_id'] ?? 0,
  444. ];
  445. }
  446. Dispatch::insert($dispatch_main);
  447. DispatchSub::insert($dispatch);
  448. $get_order_number1 = [];
  449. //获取上一次插入订单的所有数据
  450. $insert = DispatchSub::whereIn('dispatch_no', $is_need_ll)
  451. ->where('crt_time',$dispatch_time)
  452. ->where('crt_id',$user['id'])
  453. ->get()->toArray();
  454. $insert_bg = [];
  455. foreach ($insert as $value){
  456. $t = $team_man[$value['team_id']] ?? 0;
  457. $insert_bg[] = [
  458. 'id' => $value['id'],
  459. 'quantity' => $value['dispatch_quantity'],
  460. 'product_no' => $value['product_no'] ?? "",
  461. 'product_title' => $value['product_title'] ?? "",
  462. 'product_size' => $value['product_size'] ?? "",
  463. 'process_id' => $value['process_id'] ?? "",
  464. 'team_id' => $value['team_id'] ?? "",
  465. 'finished_id' => $t,
  466. ];
  467. if($value['process_id'] == 14){
  468. $return_dispatch[] = [
  469. 'id' => $value['id'],
  470. 'num' => $value['dispatch_quantity']
  471. ];
  472. }
  473. }
  474. //生成报工单
  475. $service_box = new BoxService();
  476. $first_order_number = end($report_order_number);
  477. list($status, $msg) = $service_box->createbg($insert_bg, $user, $dispatch_time, $first_order_number);
  478. if(! $status) return [false, $msg];
  479. list($report_id, $report_order_number1) = $msg;
  480. //生成质检单
  481. $service1 = new ApplyOrderService();
  482. $date = date("Ymd", $c_time);
  483. $result = [];
  484. foreach ($insert as $value){
  485. //质检
  486. $max_id += 1;
  487. $order_number = $date . str_pad($max_id,3,'0',STR_PAD_LEFT);
  488. $get_order_number1[] = $order_number;
  489. $t_report_id = $report_id[$value['id']] ?? 0;
  490. $result[] = [
  491. 'sale_orders_product_id' => $value['sale_orders_product_id'],
  492. 'order_product_id' => $value['order_product_id'],
  493. 'dispatch_sub_id' => $value['id'],
  494. 'out_order_no' => $value['out_order_no'],
  495. 'order_no' => $value['order_no'],
  496. 'customer_no' => $value['customer_no'],
  497. 'customer_name' => $value['customer_name'],
  498. 'product_no' => $value['product_no'],
  499. 'product_title' => $value['product_title'],
  500. 'product_size' => $value['product_size'],
  501. 'product_unit' => $value['product_unit'],
  502. 'technology_material' => $value['technology_material'],
  503. 'technology_name' => $value['technology_name'],
  504. 'wood_name' => $value['wood_name'],
  505. 'price' => $value['price'],
  506. 'process_mark' => $value['process_mark'],
  507. 'table_body_mark' => $value['table_body_mark'],
  508. 'table_header_mark' => $value['table_header_mark'],
  509. 'crt_id' => $user['id'],
  510. 'crt_time' => $c_time,
  511. 'team_id' => 73,
  512. 'finished_id' => $user['id'],
  513. 'equipment_id' => $value['device_id'],
  514. 'order_number' => $order_number,
  515. 'process_id' => $value['process_id'],
  516. 'quantity' => $value['dispatch_quantity'],
  517. 'report_id' => $t_report_id,
  518. 'daily_id' => $max_id,
  519. ];
  520. }
  521. ScrappCount::insert($result);
  522. if(! empty($is_need_warehouse)){
  523. //领料申请单
  524. list($status, $msg) = (new DispatchService())->createSQ($insert, $user, $dispatch_time);
  525. if(! $status) return [false, $msg];
  526. //质检入库申请单 原材料
  527. list($status, $msg) = $service1->createZJSQ($zj, $user, $is_need_warehouse, $c_time);
  528. if(! $status) return [false, $msg];
  529. }
  530. return [true, array_merge($get_order_number, $get_order_number1)];
  531. }catch (\Throwable $exception){
  532. return[false, $exception->getMessage() . $exception->getLine()];
  533. }
  534. }
  535. public function isNeedZJ($sale_orders_product_id = []){
  536. $sale_order = SaleOrdersProduct::whereIn('id', $sale_orders_product_id)
  537. ->where('order_quantity','>=', 20)
  538. ->select('id', 'order_quantity')
  539. ->get()->toArray();
  540. if(empty($sale_order)) return [false, ''];
  541. $need = [];
  542. $sale_order_id = array_column($sale_order,'id');
  543. //每个销售订单有4次的不良品创建需要
  544. foreach ($sale_order_id as $value){
  545. $need[$value] = 4;
  546. }
  547. $scrapp = ScrappCount::where("del_time",0)
  548. ->whereIn("sale_orders_product_id",$sale_order_id)
  549. ->where('scrapp_num','>',0)
  550. ->select('sale_orders_product_id','process_id')
  551. ->orderBy('process_id','asc')
  552. ->get()->toArray();
  553. $orderBy = [];
  554. foreach ($scrapp as $value){
  555. if(isset($need[$value['sale_orders_product_id']])) $need[$value['sale_orders_product_id']] -= 1;
  556. $orderBy[$value['sale_orders_product_id']][] = $value['process_id'];
  557. }
  558. $count = [];
  559. foreach ($need as $key => $value){
  560. if($value > 0) $count[$key] = $value;
  561. }
  562. //是否需要创建
  563. if(empty($count)) return [false, ''];
  564. $return = [];
  565. $process_id = [12,12,14,14];
  566. foreach ($count as $key => $value){
  567. $p_t = $orderBy[$key] ?? [];
  568. $p_t_k = count($p_t) ?? 0;
  569. if(isset($process_id[$p_t_k])){
  570. $t = $process_id[$p_t_k];
  571. $return[$key] = $t;
  572. }
  573. }
  574. return [true, $return];
  575. }
  576. //产成品入库
  577. public function U8Rdrecord10Save($package_data, $user){
  578. if(empty($package_data)) return [false, '产成品入库为空,请确认!'];
  579. try{
  580. //获取包装单信息
  581. $box = $package_data;
  582. $boxDetail = new BoxDetail(['channel'=>$box['top_order_no']]);
  583. $boxDetail = $boxDetail->where('del_time',0)
  584. ->where('order_no',$box['order_no'])
  585. ->select('id','top_id','num','ext_1','ext_2','ext_3','ext_4','ext_5','out_order_no','box_type','price')
  586. ->get()->toArray();
  587. //用友数据插入------------
  588. if(! empty($boxDetail)){
  589. $sqlServerModel = new FyySqlServerService($user);
  590. if($sqlServerModel->error) return [false, $sqlServerModel->error];
  591. $username = $sqlServerModel->getYongyouName();
  592. $box['create_name'] = $username;
  593. list($status,$msg) = $sqlServerModel->U8Rdrecord10Save($box,$boxDetail);
  594. if(! $status) return [false, $msg];
  595. }
  596. //用友数据插入------------
  597. }catch (\Exception $e){
  598. return [false,$e->getMessage() . '|' . $e->getFile() . '|' . $e->getLine()];
  599. }
  600. return [true,''];
  601. }
  602. //销售发货出库
  603. public function U8Rdrecord32Save($send_data, $out_order_no,$user){
  604. if(empty($send_data)) return [false, '销售发货出库为空,请确认!'];
  605. try{
  606. //用友数据插入------------
  607. $sqlServerModel = new FyySqlServerService($user);
  608. if($sqlServerModel->error) return [false, $sqlServerModel->error];
  609. $create_name = $sqlServerModel->getYongyouName();
  610. list($status,$msg) = $sqlServerModel->U8Rdrecord32Save($send_data,$out_order_no,$create_name);
  611. if(! $status) return [false, $msg];
  612. //用友数据插入------------
  613. }catch (\Exception $e){
  614. return [false,$e->getMessage() . '|' . $e->getFile() . '|' . $e->getLine()];
  615. }
  616. return [true,''];
  617. }
  618. public function addInJob($result,$data,$user){
  619. try{
  620. //获取数据库连接
  621. $database = $this->getConnectionName($user['zt']);
  622. //用友数据插入------------
  623. $insert_sql_server = [];
  624. foreach ($result as $key => $value){
  625. $quantity_tmp = $data['quantity'][$key];
  626. $result[$key]['quantity'] = $quantity_tmp;
  627. //工序表
  628. $process_model = new OrdersProductProcess(['channel' => date("Ymd",$value['out_order_no_time'])]);
  629. $process_model->setConnection($database);
  630. $process_id = $process_model->select('process_id')
  631. ->where('sort',$process_model->where('del_time',0)
  632. ->where('order_product_id',$value['order_product_id'])
  633. ->max('sort'))
  634. ->first();
  635. if(empty($process_id)) return [false,"未找到最后一道工序"];
  636. $process_id = $process_id->process_id;
  637. if($process_id == $value['process_id']){
  638. $insert_sql_server[] = $result[$key];
  639. }
  640. }
  641. if(! empty($insert_sql_server)){
  642. $sqlServerModel = new FyySqlServerService($user);
  643. if($sqlServerModel->error) return [false,$sqlServerModel->error];
  644. foreach ($insert_sql_server as $value){
  645. // list($status,$msg) = $sqlServerModel->U8Rdrecord10Save($value);
  646. // if(! $status) return [false,$msg];
  647. }
  648. }
  649. //用友数据插入结束----------
  650. //本地数据更新
  651. DB::beginTransaction();
  652. $waste = [];
  653. foreach ($data['waste'] as $key => $value){
  654. $waste[$key] = array_sum(array_column($value,'num'));
  655. }
  656. $time = time();
  657. foreach ($result as $key => $value){
  658. $finished_id_tmp = $data['finish_id'][$key];
  659. $team_tmp = $data['team_id'][$key];
  660. $equipment_id_tmp = $data['equipment_id'][$key];
  661. $finished_num = $value['quantity'] + $value['finished_num'];
  662. $model = new DispatchSub();
  663. $model->setConnection($database);
  664. $model->where('id',$value['id'])->update([
  665. 'finished_num' => $finished_num,
  666. 'waste_num' => $waste[$key],
  667. 'job_status' => 0,
  668. 'dispatch_quantity' => DB::raw("dispatch_quantity + {$waste[$key]}"),//派工数量增加 派工单可以继续派送
  669. ]);
  670. $insert_waste = $insert_dispatch = $scrapp = [];
  671. if(! empty($data['waste'][$key])){
  672. foreach ($data['waste'][$key] as $v){
  673. for($i = 0 ;$i < $v['num']; $i++){
  674. $insert_waste[] = [
  675. 'order_product_id' => $value['order_product_id'],
  676. 'order_no' => $value['order_no'],
  677. 'product_no' => $value['product_no'],
  678. 'product_title' => $value['product_title'],
  679. 'process_id' => $value['process_id'],
  680. 'crt_time' => $time,
  681. 'dispatch_no' => $value['dispatch_no'],
  682. 'status' => 4,//不良品
  683. 'team_id' => $team_tmp,
  684. 'finished_id' => $finished_id_tmp,
  685. 'equipment_id' => $equipment_id_tmp,
  686. 'scrapp_id' => $v['scrapp_id']
  687. ];
  688. $insert_dispatch[] = [
  689. 'order_product_id' => $value['order_product_id'],
  690. 'out_order_no' => $value['out_order_no'],
  691. 'order_no' => $value['order_no'],
  692. 'product_no' => $value['product_no'],
  693. 'product_title' => $value['product_title'],
  694. 'process_id' => $value['process_id'],
  695. 'crt_time' => $time,
  696. 'dispatch_no' => $value['dispatch_no'],
  697. 'status' => 1, //已派工
  698. 'team_id' => 0,
  699. 'finished_id' => 0,
  700. 'equipment_id' => 0,
  701. 'scrapp_id' => 0
  702. ];
  703. }
  704. $scrapp[] = [
  705. 'sale_orders_product_id' => $value['sale_orders_product_id'],
  706. 'order_product_id' => $value['order_product_id'],
  707. 'out_order_no' => $value['out_order_no'],
  708. 'order_no' => $value['order_no'],
  709. 'customer_no' => $value['customer_no'],
  710. 'customer_name' => $value['customer_name'],
  711. 'product_no' => $value['product_no'],
  712. 'product_title' => $value['product_title'],
  713. 'product_size' => $value['product_size'],
  714. 'product_unit' => $value['product_unit'],
  715. 'technology_material' => $value['technology_material'],
  716. 'technology_name' => $value['technology_name'],
  717. 'wood_name' => $value['wood_name'],
  718. 'price' => $value['price'],
  719. 'process_mark' => $value['process_mark'],
  720. 'table_body_mark' => $value['table_body_mark'],
  721. 'table_header_mark' => $value['table_header_mark'],
  722. 'crt_time' => $time,
  723. 'scrapp_num' => $v['num'],
  724. 'scrapp_id' => $v['scrapp_id']
  725. ];
  726. }
  727. }
  728. //工序表
  729. $process_model = new OrdersProductProcess(['channel' => date("Ymd",$value['out_order_no_time'])]);
  730. $process_model->setConnection($database);
  731. $process_model->where('order_product_id',$value['order_product_id'])
  732. ->where('process_id',$value['process_id'])
  733. ->where('dispatch_no',$value['dispatch_no'])
  734. ->take($value['quantity'])
  735. ->update([
  736. 'finished_time' => $time,
  737. 'status' => 2,
  738. 'finished_id' => $finished_id_tmp,
  739. 'team_id' => $team_tmp,
  740. 'equipment_id' => $equipment_id_tmp
  741. ]);
  742. if(! empty($insert_waste)) $process_model->insert($insert_waste);
  743. if(! empty($insert_dispatch)) $process_model->insert($insert_dispatch);
  744. if(! empty($scrapp)) {
  745. $scrapp_model = new ScrappCount();
  746. $scrapp_model->setConnection($database);
  747. $scrapp_model->insert($scrapp);
  748. }
  749. //生产订单数量
  750. if(! empty($waste[$key])){
  751. $num = $waste[$key];
  752. $model2 = new OrdersProduct();
  753. $model2->setConnection($database);
  754. $model2->where('id',$value['order_product_id'])->update([
  755. 'production_quantity' => DB::raw("production_quantity + {$num}"),
  756. 'scrapp_num' => DB::raw("scrapp_num + {$num}"),
  757. 'dispatch_complete_quantity' => DB::raw("dispatch_complete_quantity + {$num}"),//已派工数量增加
  758. ]);
  759. }
  760. }
  761. //反写数量
  762. // $this->writeFinishedQuantity(array_column($result,'sale_orders_product_id'),$database);
  763. // $this->writeFinishedQuantityByOrdersProductId(array_column($result,'order_product_id'),$database);
  764. DB::commit();
  765. }catch (\Exception $e){
  766. DB::rollBack();
  767. return [false,$e->getFile() . $e->getLine(). $e->getMessage()];
  768. }
  769. return [true,''];
  770. }
  771. public function del($data){
  772. if($this->isEmpty($data,'id')) return [false,'ID不能为空!'];
  773. return [true,'删除成功'];
  774. }
  775. public function orderDetail($data){
  776. if($this->isEmpty($data,'id')) return [false,'ID不能为空!'];
  777. $first = DispatchSub::where('id',$data['id'])->first();
  778. $result = [];
  779. $scrapp = ScrappCount::where('del_time',0)
  780. ->where('order_product_id',$first->order_product_id)
  781. ->select('order_product_id','scrapp_num as num', 'scrapp_id')
  782. ->get()->toArray();
  783. foreach ($scrapp as $value){
  784. if(isset($result[$value['order_product_id']])){
  785. $tmp = bcadd($result[$value['scrapp_id']],$value['num'],3);
  786. $result[$value['scrapp_id']]['num'] = $tmp;
  787. }else{
  788. $result[$value['scrapp_id']] = [
  789. 'scrapp_id' => $value['scrapp_id'],
  790. 'num' => $value['num']
  791. ];
  792. }
  793. }
  794. $result = array_values($result);
  795. // $first = DispatchSub::where('id',$data['id'])->first();
  796. // $process_model = new OrdersProductProcess(['channel' => date("Ymd",$first->out_order_no_time)]);
  797. //
  798. // $result = $process_model->where('del_time',0)
  799. // ->where('dispatch_no',$first->dispatch_no)
  800. // ->where('order_product_id',$first->order_product_id)
  801. // ->where('status',4)
  802. // ->select(DB::raw('count(id) as num'),'scrapp_id')
  803. // ->groupBy('scrapp_id')
  804. // ->get()->toArray();
  805. $map = Scrapp::whereIn('id',array_column($result,'scrapp_id'))
  806. ->pluck('title','id')
  807. ->toArray();
  808. foreach ($result as $key => $value){
  809. $result[$key]['scrapp_name'] = $map[$value['scrapp_id']] ?? '';
  810. }
  811. return [true,$result];
  812. }
  813. public function is_same_month($timestamp1,$timestamp2){
  814. // 格式化时间戳为年份和月份
  815. $year1 = date('Y', $timestamp1);
  816. $month1 = date('m', $timestamp1);
  817. $year2 = date('Y', $timestamp2);
  818. $month2 = date('m', $timestamp2);
  819. if ($year1 === $year2 && $month1 === $month2) {
  820. return true;
  821. } else {
  822. return false;
  823. }
  824. }
  825. public function orderRule($data){
  826. if(! isset($data['is_finish_all'])) return [false, '完工类型不能为空!'];
  827. if(empty($data['detail'])) return [false, '完工明细数据不能为空!'];
  828. $detail_map = $id = $detail_map2 = $waste_map = [];
  829. //班组下的人
  830. $team_man = [];
  831. $team_array = EmployeeTeamPermission::select('employee_id', 'team_id')->get()->toArray();
  832. foreach ($team_array as $v){
  833. $team_man[$v['team_id']][] = $v['employee_id'];
  834. }
  835. foreach ($data['detail'] as $value){
  836. if(empty($value['id'])) return [false,'请选择完工数据!'];
  837. $id[] = $value['id'];
  838. if(empty($value['device_id'])) return [false, '请选择设备!'];
  839. if(empty($value['team_id'])) return [false, '班组必须选择'];
  840. if(empty($team_man[$value['team_id']])) return [false, '班组下的人不能为空,请设置'];
  841. // if(empty($value['team_id']) && empty($value['finish_id'])) return [false,'班组和人员必须选择一个!'];
  842. if(! is_numeric($value['quantity']) || $value['quantity'] < 0) return [false,'完工数量不能小于0!'];
  843. if(! empty($value['waste'])){
  844. foreach ($value['waste'] as $waste){
  845. if(empty($waste['scrapp_id'])) return [false,'请选择损耗原因!'];
  846. if(! is_numeric($waste['num']) || $waste['num'] < 0) return [false,'损耗数量不能小于0!'];
  847. if(isset($waste_map[$value['id']])){
  848. $waste_map[$value['id']] += $waste['num'];
  849. }else{
  850. $waste_map[$value['id']] = $waste['num'];
  851. }
  852. }
  853. }
  854. //完工数量
  855. if(isset($detail_map[$value['id']])){
  856. $detail_map[$value['id']] += $value['quantity'];
  857. }else{
  858. $detail_map[$value['id']] = $value['quantity'];
  859. }
  860. if(! isset($detail_map2[$value['id']])){
  861. $detail_map2[$value['id']] = $value;
  862. }
  863. }
  864. //校验
  865. $result = DispatchSub::where('del_time',0)
  866. ->whereIn('id',$id)
  867. ->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')
  868. ->get()->toArray();
  869. if(empty($result)) return [false, '派工单不存在或已被删除!'];
  870. $process_map = Process::whereIn('id',array_unique(array_column($result,'process_id')))->pluck('title','id')->toArray();
  871. $search = "";$args = [];
  872. foreach ($result as $key => $value){
  873. $detail2 = $detail_map2[$value['id']] ?? [];
  874. $result[$key]['device_id'] = $detail2['device_id'] ?? 0;
  875. $result[$key]['team_id'] = $detail2['team_id'] ?? 0;
  876. $result[$key]['finish_id'] = $detail2['finish_id'] ?? 0;
  877. $result[$key]['waste_quantity'] = $waste_map[$value['id']] ?? 0;
  878. $tmp = [];
  879. if(! empty($detail2['waste'])){
  880. foreach ($detail2['waste'] as $v){
  881. if(isset($tmp[$v['id']])){
  882. $n = bcadd($v['num'],$tmp[$v['scrapp_id']],3);
  883. $tmp[$v['scrapp_id']] = $n;
  884. }else{
  885. $tmp[$v['scrapp_id']] = $v['num'];
  886. }
  887. }
  888. }
  889. $result[$key]['waste_array'] = $tmp;
  890. $q = $detail_map[$value['id']] ?? 0;
  891. $quantity_tmp = bcadd($q,$value['finished_num'],3);
  892. if($quantity_tmp > $value['dispatch_quantity']) {
  893. $process_tmp = $process_map[$value['process_id']] ?? "";
  894. return [false,'派工单:' . $value['dispatch_no']. '的工序' . $process_tmp .'完工数量不能大于派工数量'];
  895. }
  896. $result[$key]['quantity'] = $q;
  897. $search_key = $value['order_product_id'] . $value['crt_time'];
  898. if($data['is_finish_all'] && ! in_array($search_key , $args)){
  899. $search .= "(order_product_id = {$value['order_product_id']} and crt_time = {$value['crt_time']}) OR ";
  900. $args[] = $search_key;
  901. }
  902. }
  903. $search = rtrim($search,'OR ');
  904. $search = "($search)";
  905. if($data['is_finish_all']){
  906. //系统帮助完工的数据
  907. $result2 = DispatchSub::where('del_time',0)
  908. ->whereNotIn('id',$id)
  909. ->whereColumn('dispatch_quantity', '>', 'finished_num')
  910. ->whereRaw($search)
  911. ->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')
  912. ->get()->toArray();
  913. if(! empty($result2)){
  914. foreach ($result2 as $key => $value){
  915. $not_finished_num = $value['dispatch_quantity'] - $value['finished_num'];
  916. $result2[$key]['quantity'] = $not_finished_num;
  917. $result2[$key]['waste_array'] = [];
  918. $result2[$key]['waste_quantity'] = 0;
  919. $result2[$key]['device_id'] = 0;
  920. $result2[$key]['team_id'] = 0;
  921. $result2[$key]['finish_id'] = 0;
  922. }
  923. }
  924. $result = array_merge_recursive($result, $result2);
  925. }
  926. return [true, $result];
  927. }
  928. public function orderList($data){
  929. $model = FinishedOrderSub::where('del_time',0)
  930. ->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')
  931. ->orderBy('upd_time','desc')
  932. ->orderBy('id','desc');
  933. if(! empty($data['order_no'])) $model->where('order_no', 'LIKE', '%'.$data['order_no'].'%');
  934. if(! empty($data['product_title'])) $model->where('product_title', 'LIKE', '%'.$data['product_title'].'%');
  935. if(! empty($data['product_size'])) $model->where('product_size', 'LIKE', '%'.$data['product_size'].'%');
  936. if(! empty($data['technology_material'])) $model->where('technology_material', 'LIKE', '%'.$data['technology_material'].'%');
  937. if(! empty($data['technology_name'])) $model->where('technology_name', 'LIKE', '%'.$data['technology_name'].'%');
  938. if(! empty($data['wood_name'])) $model->where('wood_name', 'LIKE', '%'.$data['wood_name'].'%');
  939. if(! empty($data['process_mark'])) $model->where('process_mark', 'LIKE', '%'.$data['process_mark'].'%');
  940. if(! empty($data['table_header_mark'])) $model->where('table_header_mark', 'LIKE', '%'.$data['table_header_mark'].'%');
  941. if(! empty($data['table_body_mark'])) $model->where('table_body_mark', 'LIKE', '%'.$data['table_body_mark'].'%');
  942. if(! empty($data['dispatch_time'][0]) && ! empty($data['dispatch_time'][1])) $model->whereBetween('dispatch_time',[$data['dispatch_time'][0],$data['dispatch_time'][1]]);
  943. if(! empty($data['employee_id'])) {
  944. $team_id = Employee::from('employee as a')
  945. ->leftJoin('employee_team_permission as b','b.employee_id','a.id')
  946. ->where('a.id', $data['employee_id'])
  947. ->select('b.team_id')
  948. ->get()->toArray();
  949. $team_id = array_column($team_id,'team_id');
  950. $model->where('team_id',$team_id ?? []);
  951. }
  952. if(isset($data['status'])) $model->where('status',$data['status']);
  953. $list = $this->limit($model,'',$data);
  954. $list = $this->fillData($list);
  955. return [true,$list];
  956. }
  957. public function fillData($data){
  958. if(empty($data['data'])) return $data;
  959. $waste_map = $waste_map_2 = [];
  960. $waste = FinishedOrderScrapp::where('del_time',0)
  961. ->whereIn('finished_order_id',array_column($data['data'],'id'))
  962. ->select('finished_order_id','num','scrapp_id')
  963. ->get()->toArray();
  964. if(! empty($waste)){
  965. foreach ($waste as $value){
  966. $waste_map[$value['finished_order_id']][] = [
  967. 'num' => $value['num'],
  968. 'scrapp_id' => $value['scrapp_id']
  969. ];
  970. if(isset($waste_map_2[$value['finished_order_id']])){
  971. $waste_map_2[$value['finished_order_id']] += $value['num'];
  972. }else{
  973. $waste_map_2[$value['finished_order_id']] = $value['num'];
  974. }
  975. }
  976. }
  977. $team = EmployeeTeamPermission::from('employee_team_permission as a')
  978. ->leftJoin('employee as b','b.id','a.employee_id')
  979. ->whereIn('a.team_id',array_column($data['data'],'team_id'))
  980. ->select('b.emp_name','a.team_id')
  981. ->get()
  982. ->toArray();
  983. $team_map = [];
  984. if(! empty($team)){
  985. foreach ($team as $value){
  986. if(isset($team_map[$value['team_id']])){
  987. $team_map[$value['team_id']] .= ','. $value['emp_name'];
  988. }else{
  989. $team_map[$value['team_id']] = $value['emp_name'];
  990. }
  991. }
  992. }
  993. $process_map = Process::whereIn('id',array_column($data['data'],'process_id'))
  994. ->pluck('title','id')
  995. ->toArray();
  996. $team_maps = Team::whereIn('id',array_column($data['data'],'team_id'))
  997. ->pluck('title','id')
  998. ->toArray();
  999. $equipment_map = Equipment::whereIn('id',array_column($data['data'],'equipment_id'))
  1000. ->pluck('title','id')
  1001. ->toArray();
  1002. foreach ($data['data'] as $key => $value){
  1003. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d',$value['crt_time']) : '';
  1004. $time1 = $value['dispatch_time_start'] ? date('Y-m-d',$value['dispatch_time_start']) : '';
  1005. $time2 = $value['dispatch_time_end'] ? date('Y-m-d',$value['dispatch_time_end']) : '';
  1006. $data['data'][$key]['dispatch_plan_time'] = $time1 . ' ' . $time2;
  1007. $data['data'][$key]['dispatch_time'] = $value['dispatch_time'] ? date('Y-m-d',$value['dispatch_time']) : '';
  1008. $data['data'][$key]['team_man'] = $team_map[$value['team_id']] ?? '';
  1009. $data['data'][$key]['process_name'] = $process_map[$value['process_id']] ?? '';
  1010. $data['data'][$key]['team_name'] = $team_maps[$value['team_id']] ?? '';
  1011. $data['data'][$key]['equipment_name'] = $equipment_map[$value['equipment_id']] ?? '';
  1012. $data['data'][$key]['waste'] = $waste_map[$value['id']] ?? [];
  1013. $data['data'][$key]['waste_quantity'] = $waste_map_2[$value['id']] ?? 0;
  1014. $data['data'][$key]['not_finished_num'] = $value['dispatch_quantity'] - $value['finished_num'];
  1015. }
  1016. $data['finished_num'] = $this->getTotal($data['data'], 'finished_num');
  1017. $data['dispatch_quantity'] = $this->getTotal($data['data'], 'dispatch_quantity');
  1018. $data['waste_quantity'] = $this->getTotal($data['data'], 'waste_quantity');
  1019. $data['not_finished_num'] = $this->getTotal($data['data'], 'not_finished_num');
  1020. return $data;
  1021. }
  1022. //反写销售订单完工数量
  1023. public function writeFinishedQuantity($orders_product_id){
  1024. if(empty($orders_product_id)) return;
  1025. $sale_orders_product_id = OrdersProduct::where('del_time',0)
  1026. ->whereIn('id', $orders_product_id)
  1027. ->select('sale_orders_product_id')
  1028. ->get()->toArray();
  1029. $sale_orders_product_id = array_unique(array_column($sale_orders_product_id,'sale_orders_product_id'));
  1030. $production_order = OrdersProduct::where('del_time',0)
  1031. ->whereIn('sale_orders_product_id', $sale_orders_product_id)
  1032. ->select(DB::raw("sum(finished_num) as finished_num"),'sale_orders_product_id')
  1033. ->groupby('sale_orders_product_id')
  1034. ->pluck('finished_num','sale_orders_product_id')
  1035. ->toArray();
  1036. foreach ($production_order as $sale_orders_product_id => $finished_num){
  1037. SaleOrdersProduct::where('id',$sale_orders_product_id)->update([
  1038. 'finished_num' => $finished_num,
  1039. ]);
  1040. }
  1041. }
  1042. //反写生产订单完工数量
  1043. public function writeFinishedQuantityByOrdersProductId($order_product_id){
  1044. if(empty($order_product_id)) return;
  1045. $result = DispatchSub::where('del_time',0)
  1046. ->whereIn('order_product_id',$order_product_id)
  1047. ->select('finished_num','order_product_id','crt_time','process_id')
  1048. ->get()
  1049. ->toArray();
  1050. if(empty($result)) return;
  1051. //生产订单
  1052. $production_order = OrdersProduct::where('del_time',0)
  1053. ->whereIn('id',array_unique(array_column($result,'order_product_id')))
  1054. ->select('process_id','id','production_quantity')
  1055. ->get()
  1056. ->toArray();
  1057. $judge = [];
  1058. foreach ($result as $value){
  1059. if(isset($judge[$value['order_product_id']][$value['process_id']])){
  1060. $judge[$value['order_product_id']][$value['process_id']] += $value['finished_num'];
  1061. }else{
  1062. $judge[$value['order_product_id']][$value['process_id']] = $value['finished_num'];
  1063. }
  1064. }
  1065. foreach ($production_order as $value){
  1066. //生产订单下的所有工序派工单
  1067. $tmp = $judge[$value['id']] ?? [];
  1068. if(empty($tmp)) continue;
  1069. //生产订单下的工序种类
  1070. $ori_process = explode(',',$value['process_id']);
  1071. $ori_process_count = count($ori_process);
  1072. //生产订单下的派工单的工序种类
  1073. $process = array_keys($tmp);
  1074. $process_count = count($process);
  1075. if($ori_process_count != $process_count) continue;
  1076. //获取工序里最小的数值
  1077. $min_finished_num = min($tmp);
  1078. //更新完工数量
  1079. OrdersProduct::where('id',$value['id'])->update([
  1080. 'finished_num' => $min_finished_num
  1081. ]);
  1082. }
  1083. }
  1084. public function mobileAdd($data,$user){return [false, '接口功能已关闭'];
  1085. //数据校验以及填充
  1086. list($status,$msg,$count_arr) = $this->orderMobileRule($data);
  1087. if(!$status) return [$status,$msg];
  1088. $user = [
  1089. 'id' => $user['id'],
  1090. 'operate_time' => time(),
  1091. 'zt' => Request()->header('zt'),
  1092. ];
  1093. $redis = [
  1094. 'result' => $msg,
  1095. 'data' => $data,
  1096. 'quantity_count' => $count_arr,
  1097. ];
  1098. $job = ProcessDataJob::dispatch($redis,$user,3)->onQueue(ProcessDataJob::job_one);
  1099. if(! $job) return [false,'任务没有进入队列!'];
  1100. //错误计数
  1101. Redis::hSet('order_failures', md5(json_encode($redis)), 0);
  1102. //标记进入队列的数据
  1103. DispatchSub::whereIn('id',array_column($msg,'id'))->update([
  1104. 'job_status' => 1,
  1105. ]);
  1106. return [true,'任务已进入队列!'];
  1107. }
  1108. public function addMobileInJob($result, $data, $count_arr,$user){
  1109. try{
  1110. //获取数据库连接
  1111. $database = $this->getConnectionName($user['zt']);
  1112. //用友数据写入------------
  1113. $insert_sql_server = [];
  1114. foreach ($result as $key => $value){
  1115. $quantity_tmp = $count_arr[$value['id']];
  1116. $result[$key]['quantity'] = $quantity_tmp;
  1117. $process_model = new OrdersProductProcess(['channel' => date("Ymd",$value['out_order_no_time'])]);
  1118. $process_model->setConnection($database);
  1119. $process_id = $process_model->select('process_id')
  1120. ->where('sort',$process_model->where('del_time',0)
  1121. ->where('order_product_id',$value['order_product_id'])
  1122. ->max('sort'))
  1123. ->first();
  1124. if(empty($process_id)) return [false,"未找到最后一道工序"];
  1125. $process_id = $process_id->process_id;
  1126. if($process_id == $value['process_id']){
  1127. $insert_sql_server[] = $result[$key];
  1128. }
  1129. }
  1130. if(! empty($insert_sql_server)){
  1131. $sqlServerModel = new FyySqlServerService($user);
  1132. if($sqlServerModel->error) return [false,$sqlServerModel->error];
  1133. foreach ($insert_sql_server as $value){
  1134. // list($status,$msg) = $sqlServerModel->U8Rdrecord10Save($value);
  1135. // if(! $status) return [false,$msg];
  1136. }
  1137. }
  1138. //用友数据写入结束------------
  1139. //本地数据写入-----------
  1140. DB::beginTransaction();
  1141. $waste = $waste2 = [];
  1142. foreach ($data as $value){
  1143. if(! empty($value['waste'])){
  1144. foreach ($value['waste'] as $v){
  1145. if(isset($waste[$value['id']])){
  1146. $waste[$value['id']] += $v['num'];
  1147. $waste2[$value['order_product_id']] += $v['num'];
  1148. }else{
  1149. $waste[$value['id']] = $v['num'];
  1150. $waste2[$value['order_product_id']] = $v['num'];
  1151. }
  1152. }
  1153. }
  1154. }
  1155. $time = time();
  1156. foreach ($result as $value){
  1157. $finished_num = $value['quantity'] + $value['finished_num'];
  1158. $watste_num = $waste[$value['id']] ?? 0;
  1159. $model = new DispatchSub();
  1160. $model->setConnection($database);
  1161. $model->where('id',$value['id'])->update([
  1162. 'finished_num' => $finished_num,
  1163. 'waste_num' => $watste_num,
  1164. 'job_status' => 0,
  1165. 'dispatch_quantity' => DB::raw("dispatch_quantity + {$watste_num}"),//派工数量增加 派工单可以继续派送
  1166. ]);
  1167. //生产订单数量
  1168. if(! empty($waste2[$value['order_product_id']])){
  1169. $num = $waste2[$value['order_product_id']];
  1170. $model2 = new OrdersProduct();
  1171. $model2->setConnection($database);
  1172. $model2->where('id',$value['order_product_id'])->update([
  1173. 'production_quantity' => DB::raw("production_quantity + {$num}"),
  1174. 'scrapp_num' => DB::raw("scrapp_num + {$num}"),
  1175. 'dispatch_complete_quantity' => DB::raw("dispatch_complete_quantity + {$num}"),//已派工数量增加
  1176. ]);
  1177. }
  1178. //损耗和派工 损耗统计
  1179. $insert_waste = $insert_dispatch = $scrapp = [];
  1180. //工序
  1181. $process_model = new OrdersProductProcess(['channel' => date("Ymd",$value['out_order_no_time'])]);
  1182. $process_model->setConnection($database);
  1183. foreach ($data as $d){
  1184. if($d['id'] == $value['id']){
  1185. $process_model->where('order_product_id',$value['order_product_id'])
  1186. ->where('process_id',$value['process_id'])
  1187. ->where('dispatch_no',$value['dispatch_no'])
  1188. ->take($d['quantity'])
  1189. ->update([
  1190. 'finished_time' => $time,
  1191. 'status' => 2,
  1192. 'finished_id' => $d['finished_id'] ?? 0,
  1193. 'team_id' => $d['team_id'] ?? 0,
  1194. 'equipment_id' => $d['equipment_id'] ?? 0
  1195. ]);
  1196. if(! empty($d['waste'])){
  1197. foreach ($d['waste'] as $v){
  1198. for($i = 0 ;$i < $v['num']; $i++){
  1199. $insert_waste[] = [
  1200. 'order_product_id' => $value['order_product_id'],
  1201. 'out_order_no' => $value['out_order_no'],
  1202. 'order_no' => $value['order_no'],
  1203. 'product_no' => $value['product_no'],
  1204. 'product_title' => $value['product_title'],
  1205. 'process_id' => $value['process_id'],
  1206. 'crt_time' => $time,
  1207. 'dispatch_no' => $value['dispatch_no'],
  1208. 'status' => 4,
  1209. 'team_id' => $d['team_id'],
  1210. 'finished_id' => $d['finished_id'] ?? 0,
  1211. 'equipment_id' => $d['equipment_id'] ?? 0,
  1212. 'scrapp_id' => $v['scrapp_id'] ?? 0
  1213. ];
  1214. $insert_dispatch[] = [
  1215. 'order_product_id' => $value['order_product_id'],
  1216. 'out_order_no' => $value['out_order_no'],
  1217. 'order_no' => $value['order_no'],
  1218. 'product_no' => $value['product_no'],
  1219. 'product_title' => $value['product_title'],
  1220. 'process_id' => $value['process_id'],
  1221. 'crt_time' => $time,
  1222. 'dispatch_no' => $value['dispatch_no'],
  1223. 'status' => 1, //已派工
  1224. 'team_id' => 0,
  1225. 'finished_id' => 0,
  1226. 'equipment_id' => 0,
  1227. 'scrapp_id' => 0
  1228. ];
  1229. }
  1230. $scrapp[] = [
  1231. 'sale_orders_product_id' => $value['sale_orders_product_id'],
  1232. 'order_product_id' => $value['order_product_id'],
  1233. 'out_order_no' => $value['out_order_no'],
  1234. 'order_no' => $value['order_no'],
  1235. 'customer_no' => $value['customer_no'],
  1236. 'customer_name' => $value['customer_name'],
  1237. 'product_no' => $value['product_no'],
  1238. 'product_title' => $value['product_title'],
  1239. 'product_size' => $value['product_size'],
  1240. 'product_unit' => $value['product_unit'],
  1241. 'technology_material' => $value['technology_material'],
  1242. 'technology_name' => $value['technology_name'],
  1243. 'wood_name' => $value['wood_name'],
  1244. 'price' => $value['price'],
  1245. 'process_mark' => $value['process_mark'],
  1246. 'table_body_mark' => $value['table_body_mark'],
  1247. 'table_header_mark' => $value['table_header_mark'],
  1248. 'crt_time' => $time,
  1249. 'scrapp_num' => $v['num'],
  1250. 'scrapp_id' => $v['scrapp_id']
  1251. ];
  1252. }
  1253. }
  1254. }
  1255. }
  1256. if(! empty($insert_waste)) $process_model->insert($insert_waste);
  1257. if(! empty($insert_dispatch)) $process_model->insert($insert_dispatch);
  1258. if(! empty($scrapp)) {
  1259. $scrapp_model = new ScrappCount();
  1260. $scrapp_model->setConnection($database);
  1261. $scrapp_model->insert($scrapp);
  1262. }
  1263. }
  1264. //反写数量
  1265. // $this->writeFinishedQuantity(array_column($result,'sale_orders_product_id'),$database);
  1266. // $this->writeFinishedQuantityByOrdersProductId(array_column($result,'order_product_id'),$database);
  1267. DB::commit();
  1268. //本地数据写入结束-----------
  1269. }catch (\Exception $e){
  1270. DB::rollBack();
  1271. return [false,$e->getFile() . $e->getLine() . $e->getMessage()];
  1272. }
  1273. return [true,''];
  1274. }
  1275. public function orderMobileRule($data){
  1276. if(empty($data)) return [false,'数据不能为空!',''];
  1277. $dispatch_id = array_unique(array_column($data,'id'));
  1278. $bool = DispatchSub::whereIn('id',$dispatch_id)->where('job_status',1)->exists();
  1279. if($bool) return [false,'正在队列中,请不要重复操作!',''];
  1280. $post = $waste = [];
  1281. foreach ($data as $value){
  1282. if(! is_numeric($value['quantity']) || $value['quantity'] <= 0) return [false,'请填写正确的完工数量!',''];
  1283. if(empty($value['finished_id']) && empty($value['team_id'])) return [false,'人员和班组必须填写一项!',''];
  1284. if(isset($post[$value['id']])){
  1285. $post[$value['id']] += $value['quantity'];
  1286. }else{
  1287. $post[$value['id']] = $value['quantity'];
  1288. }
  1289. if(! empty($value['waste'])){
  1290. foreach ($value['waste'] as $v){
  1291. if(isset($waste[$value['id']])){
  1292. $waste[$value['id']] += $v['num'];
  1293. }else{
  1294. $waste[$value['id']] = $v['num'];
  1295. }
  1296. }
  1297. }
  1298. }
  1299. $result = DispatchSub::whereIn('id',$dispatch_id)
  1300. ->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')
  1301. ->orderBy('id','desc')
  1302. ->get()->toArray();
  1303. foreach ($result as $key => $value){
  1304. $tmp = $waste[$value['id']] ?? 0;
  1305. $quantity_tmp = $post[$value['id']] + $value['finished_num'] + $value['waste_num'] + $tmp;
  1306. if($quantity_tmp > $value['dispatch_quantity']) return [false,"完工数量加上损耗数量不能大于派工数量",''];
  1307. }
  1308. return [true, $result, $post];
  1309. }
  1310. public function getConnectionName($name)
  1311. {
  1312. $mysql = "mysql";
  1313. if ($name === '001') {
  1314. $mysql = "mysql_001";
  1315. } elseif ($name === '002') {
  1316. $mysql = "mysql_002";
  1317. }
  1318. return $mysql;
  1319. }
  1320. public function insertYy($order, $user){
  1321. //包装单
  1322. $package_data = json_decode($order['post'], true);
  1323. if(! empty($package_data)) {
  1324. //用友 ------产成品入库
  1325. $service = new FinishedOrderService();
  1326. list($status,$msg) = $service->U8Rdrecord10Save($package_data,$user);
  1327. if(! $status) return [false, $msg];
  1328. }
  1329. return [true, ''];
  1330. }
  1331. public function finishedOrderList($data){
  1332. $model = ReportWorkingDetail::from("report_working_detail as a")
  1333. ->leftJoin("dispatch_sub as b",'a.data_id','b.id')
  1334. ->leftjoin("report_working as c",'a.report_working_id', 'c.id')
  1335. ->where("a.del_time", 0)
  1336. ->where('b.del_time', 0)
  1337. ->where('c.del_time', 0)
  1338. ->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')
  1339. ->orderBy('a.id','desc');
  1340. if(isset($data['type'])) $model->where('b.type', $data['type']);
  1341. if(isset($data['status'])) $model->where('b.status', $data['status']);
  1342. if(isset($data['wg_status'])) $model->where('b.wg_status', $data['wg_status']);
  1343. if(! empty($data['order_no'])) $model->where('b.order_no', 'LIKE', '%'.$data['order_no'].'%');
  1344. if(! empty($data['dispatch_no'])) $model->where('b.dispatch_no', 'LIKE', '%'.$data['dispatch_no'].'%');
  1345. if(! empty($data['out_order_no'])) $model->where('b.out_order_no', 'LIKE', '%'.$data['out_order_no'].'%');
  1346. if(! empty($data['order_number'])) $model->where('c.order_number', 'LIKE', '%'.$data['order_number'].'%');
  1347. if(! empty($data['production_no'])) {
  1348. $id = OrdersProduct::where('del_time', 0)
  1349. ->where('b.production_no', 'LIKE', '%'.$data['production_no'].'%')
  1350. ->select('id')
  1351. ->get()->toArray();
  1352. $model->whereIn('order_product_id', array_column($id,'id'));
  1353. }
  1354. if(! empty($data['process_id'])) $model->where('b.process_id',$data['process_id']);
  1355. if(! empty($data['technology_material'])) $model->where('b.technology_material', 'LIKE', '%'.$data['technology_material'].'%');
  1356. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) $model->whereBetween('a.crt_time',[$data['crt_time'][0],$data['crt_time'][1]]);
  1357. if(! empty($data['dispatch_time'][0]) && ! empty($data['dispatch_time'][1])){
  1358. $model->where('b.dispatch_time_start','>=',$data['dispatch_time'][0]);
  1359. $model->where('b.dispatch_time_end','<=',$data['dispatch_time'][1]);
  1360. }
  1361. if(! empty($data['pc_number'])) $model->where('b.out_order_no', 'LIKE', '%'.$data['pc_number'].'%');
  1362. if(! empty($data['team_id'])) $model->where('b.team_id',$data['team_id']);
  1363. if(! empty($data['device_id'])) $model->where('b.device_id',$data['device_id']);
  1364. if(! empty($data['equipment_id'])) $model->where('b.device_id',$data['equipment_id']);
  1365. if(! empty($data['employee_id'])) {
  1366. $team = EmployeeTeamPermission::where('employee_id',$data['employee_id'])
  1367. ->select('team_id')
  1368. ->get()->toArray();
  1369. $model->whereIn('b.team_id',array_unique(array_column($team,'team_id')));
  1370. }
  1371. $list = $this->limit($model,'',$data);
  1372. $list = $this->fillDispatchOrderListData($list);
  1373. return [true,$list];
  1374. }
  1375. public function fillDispatchOrderListData($data){
  1376. if(empty($data['data'])) return $data;
  1377. $team_map = Team::whereIn('id',array_unique(array_column($data['data'],'team_id')))
  1378. ->pluck('title','id')
  1379. ->toArray();
  1380. $equipment_map = Equipment::whereIn('id',array_unique(array_column($data['data'],'device_id')))
  1381. ->pluck('title','id')
  1382. ->toArray();
  1383. $process_map = Process::whereIn('id',array_column($data['data'],'process_id'))
  1384. ->pluck('title','id')
  1385. ->toArray();
  1386. $orders = OrdersProduct::whereIn('id', array_column($data['data'],'order_product_id'))
  1387. ->pluck('production_no','id')
  1388. ->toArray();
  1389. foreach ($data['data'] as $key => $value){
  1390. $data['data'][$key]['wg_status_title'] = DispatchSub::$status_name[$value['wg_status']] ?? "";
  1391. $data['data'][$key]['status_title'] = DispatchSub::$status_name[$value['status']] ?? "";
  1392. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d',$value['crt_time']) : '';
  1393. $time1 = $value['dispatch_time_start'] ? date('Y-m-d',$value['dispatch_time_start']) : '';
  1394. $time2 = $value['dispatch_time_end'] ? date('Y-m-d',$value['dispatch_time_end']) : '';
  1395. $data['data'][$key]['dispatch_time'] = $time1 . ' ' . $time2;
  1396. $data['data'][$key]['process_name'] = $process_map[$value['process_id']] ?? '';
  1397. $data['data'][$key]['team_name'] = $team_map[$value['team_id']] ?? "";
  1398. $data['data'][$key]['equipment_name'] = $equipment_map[$value['device_id']] ?? "";
  1399. $data['data'][$key]['equipment_id'] = $value['device_id'];
  1400. $data['data'][$key]['un_finished_quantity'] = bcsub($value['dispatch_quantity'] , $value['finished_num'],3);
  1401. $data['data'][$key]['production_no'] = $orders[$value['order_product_id']] ?? '';
  1402. }
  1403. $total = $this->getTotal($data['data'], 'dispatch_quantity');
  1404. $data['dispatch_quantity'] = $total;
  1405. return $data;
  1406. }
  1407. }