FinishedOrderService.php 44 KB

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