FinishedOrderService.php 44 KB

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