FinishedOrderService.php 45 KB

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