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