FinishedOrderService.php 43 KB

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