FinishedOrderService.php 72 KB

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