FinishedOrderService.php 46 KB

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