FinishedOrderService.php 45 KB

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