1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003 |
- <?php
- namespace App\Service;
- use App\Jobs\ProcessDataJob;
- use App\Model\Box;
- use App\Model\BoxDetail;
- use App\Model\DispatchSub;
- use App\Model\Employee;
- use App\Model\EmployeeTeamPermission;
- use App\Model\Equipment;
- use App\Model\FinishedOrder;
- use App\Model\FinishedOrderScrapp;
- use App\Model\FinishedOrderSub;
- use App\Model\OrdersProduct;
- use App\Model\OrdersProductProcess;
- use App\Model\Process;
- use App\Model\SaleOrdersProduct;
- use App\Model\Scrapp;
- use App\Model\ScrappCount;
- use App\Model\Team;
- use App\Service\Box\BoxService;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Redis;
- class FinishedOrderService extends Service
- {
- public function edit($data){}
- public function setOrderNO(){
- $str = date('Ymd',time());
- $order_number = FinishedOrder::where('finished_no','Like','%'. $str . '%')
- ->max('finished_no');
- if(empty($order_number)){
- $number = str_pad(1,3,'0',STR_PAD_LEFT);
- $number = $str . $number;
- }else{
- $tmp = substr($order_number, -3);
- $tmp = $tmp + 1;
- //超过99999
- if(strlen($tmp) > 3) return '';
- $number = str_pad($tmp,3,'0',STR_PAD_LEFT);
- $number = $str . $number;
- }
- return $number;
- }
- public function add($data,$user){
- //数据校验以及填充
- list($status,$msg) = $this->orderRule($data);
- if(!$status) return [$status,$msg];
- //数据源
- $result = $msg;
- $time = time();
- try{
- DB::beginTransaction();
- $em = new EmployeeService();
- $auto = $em->is_auto($user, "wg_auto");
- $last_update = $tmp = [];
- foreach ($result as $value){
- $finished_num = $value['quantity'] + $value['finished_num'];
- $waste_num = $value['waste_quantity'] + $value['waste_num'];
- DispatchSub::where('id',$value['id'])->update([
- 'finished_num' => $finished_num,
- 'waste_num' => $waste_num,
- 'wg_status' => $auto,
- ]);
- $str = $value['order_product_id'] . $value['crt_time'] . $value['product_no'] . $value['technology_name'];
- if(! in_array($str, $tmp)){
- $last_update[] = [
- 'id' => $value['id'],
- 'quantity' => $value['quantity'],
- 'product_no' => $value['product_no'] ?? "",
- 'product_title' => $value['product_title'] ?? "",
- 'product_size' => $value['product_size'] ?? "",
- 'product_unit' => $value['product_unit'] ?? "",
- ];
- $tmp[] = $str;
- }
- $insert_waste = $scrapp = [];
- if(! empty($value['waste_array'])){
- foreach ($value['waste_array'] as $v){
- $num = $v['num'] * 1000;
- for($i = 0 ;$i < $num; $i++){
- $insert_waste[] = [
- 'order_product_id' => $value['order_product_id'],
- 'order_no' => $value['order_no'],
- 'product_no' => $value['product_no'],
- 'product_title' => $value['product_title'],
- 'process_id' => $value['process_id'],
- 'crt_time' => $time,
- 'dispatch_no' => $value['dispatch_no'],
- 'status' => 4,//不良品
- 'team_id' => $value['team_id'],
- 'finished_id' => $value['finish_id'],
- 'equipment_id' => $value['device_id'],
- 'scrapp_id' => $v['scrapp_id']
- ];
- }
- $scrapp[] = [
- 'sale_orders_product_id' => $value['sale_orders_product_id'],
- 'order_product_id' => $value['order_product_id'],
- 'out_order_no' => $value['out_order_no'],
- 'order_no' => $value['order_no'],
- 'customer_no' => $value['customer_no'],
- 'customer_name' => $value['customer_name'],
- 'product_no' => $value['product_no'],
- 'product_title' => $value['product_title'],
- 'product_size' => $value['product_size'],
- 'product_unit' => $value['product_unit'],
- 'technology_material' => $value['technology_material'],
- 'technology_name' => $value['technology_name'],
- 'wood_name' => $value['wood_name'],
- 'price' => $value['price'],
- 'process_mark' => $value['process_mark'],
- 'table_body_mark' => $value['table_body_mark'],
- 'table_header_mark' => $value['table_header_mark'],
- 'crt_time' => $time,
- 'scrapp_num' => $v['num'],
- 'scrapp_id' => $v['scrapp_id'],
- 'team_id' => $value['team_id'],
- 'finished_id' => $value['finish_id'],
- 'equipment_id' => $value['device_id'],
- 'order_number' => $value['id'] . $time . $value['process_id'],
- 'process_id' => $value['process_id'],
- 'quantity' => $value['quantity'],
- ];
- }
- }
- $quantity = $value['quantity'] * 1000;
- //工序表
- $process_model = new OrdersProductProcess(['channel' => date("Ymd",$value['out_order_no_time'])]);
- $process_model->where('order_product_id',$value['order_product_id'])
- ->where('process_id',$value['process_id'])
- ->where('dispatch_no',$value['dispatch_no'])
- ->where('status', 1)
- ->take($quantity)
- ->update([
- 'finished_time' => $time,
- 'status' => 2,
- 'finished_id' => $value['finish_id'],
- 'team_id' => $value['team_id'],
- 'equipment_id' => $value['device_id'],
- ]);
- if(! empty($insert_waste)) $process_model->insert($insert_waste);
- if(! empty($scrapp)) {
- $scrapp_model = new ScrappCount();
- $scrapp_model->insert($scrapp);
- }
- // todo 暂时注释
- // if(! empty($value['waste_quantity'])){
- // $num = $value['waste_quantity'];
- // OrdersProduct::where('id',$value['order_product_id'])->update([
- // 'scrapp_num' => DB::raw("scrapp_num + {$num}"),
- // ]);
- // }
- }
- //反写数量
- //生产订单
- $id = array_column($result,'order_product_id');
- $this->writeFinishedQuantityByOrdersProductId($id);
- //销售订单
- $this->writeFinishedQuantity($id);
- //生成完工入库申请单
- $service = new BoxService();
- list($status, $msg) = $service->createWGSQ($last_update, $user);
- if(! $status) return [false, $msg];
- DB::commit();
- }catch (\Exception $e){
- DB::rollBack();
- return [false,$e->getFile() . $e->getLine(). $e->getMessage()];
- }
- return [true, ''];
- }
- //产成品入库
- public function U8Rdrecord10Save($package_data, $user){
- if(empty($package_data)) return [false, '产成品入库为空,请确认!'];
- try{
- //获取包装单信息
- $box = $package_data;
- $boxDetail = new BoxDetail(['channel'=>$box['top_order_no']]);
- $boxDetail = $boxDetail->where('del_time',0)
- ->where('order_no',$box['order_no'])
- ->select('id','top_id','num','ext_1','ext_2','ext_3','ext_4','ext_5','out_order_no','box_type','price')
- ->get()->toArray();
- //用友数据插入------------
- if(! empty($boxDetail)){
- $sqlServerModel = new FyySqlServerService($user);
- if($sqlServerModel->error) return [false, $sqlServerModel->error];
- $username = $sqlServerModel->getYongyouName();
- $box['create_name'] = $username;
- list($status,$msg) = $sqlServerModel->U8Rdrecord10Save($box,$boxDetail);
- if(! $status) return [false, $msg];
- }
- //用友数据插入------------
- }catch (\Exception $e){
- return [false,$e->getMessage() . '|' . $e->getFile() . '|' . $e->getLine()];
- }
- return [true,''];
- }
- //销售发货出库
- public function U8Rdrecord32Save($send_data, $user){
- if(empty($send_data)) return [false, '销售发货出库为空,请确认!'];
- try{
- //用友数据插入------------
- $sqlServerModel = new FyySqlServerService($user);
- if($sqlServerModel->error) return [false, $sqlServerModel->error];
- $create_name = $sqlServerModel->getYongyouName();
- list($status,$msg) = $sqlServerModel->U8Rdrecord32Save($send_data,$create_name);
- if(! $status) return [false, $msg];
- //用友数据插入------------
- }catch (\Exception $e){
- return [false,$e->getMessage() . '|' . $e->getFile() . '|' . $e->getLine()];
- }
- return [true,''];
- }
- public function addInJob($result,$data,$user){
- try{
- //获取数据库连接
- $database = $this->getConnectionName($user['zt']);
- //用友数据插入------------
- $insert_sql_server = [];
- foreach ($result as $key => $value){
- $quantity_tmp = $data['quantity'][$key];
- $result[$key]['quantity'] = $quantity_tmp;
- //工序表
- $process_model = new OrdersProductProcess(['channel' => date("Ymd",$value['out_order_no_time'])]);
- $process_model->setConnection($database);
- $process_id = $process_model->select('process_id')
- ->where('sort',$process_model->where('del_time',0)
- ->where('order_product_id',$value['order_product_id'])
- ->max('sort'))
- ->first();
- if(empty($process_id)) return [false,"未找到最后一道工序"];
- $process_id = $process_id->process_id;
- if($process_id == $value['process_id']){
- $insert_sql_server[] = $result[$key];
- }
- }
- if(! empty($insert_sql_server)){
- $sqlServerModel = new FyySqlServerService($user);
- if($sqlServerModel->error) return [false,$sqlServerModel->error];
- foreach ($insert_sql_server as $value){
- // list($status,$msg) = $sqlServerModel->U8Rdrecord10Save($value);
- // if(! $status) return [false,$msg];
- }
- }
- //用友数据插入结束----------
- //本地数据更新
- DB::beginTransaction();
- $waste = [];
- foreach ($data['waste'] as $key => $value){
- $waste[$key] = array_sum(array_column($value,'num'));
- }
- $time = time();
- foreach ($result as $key => $value){
- $finished_id_tmp = $data['finish_id'][$key];
- $team_tmp = $data['team_id'][$key];
- $equipment_id_tmp = $data['equipment_id'][$key];
- $finished_num = $value['quantity'] + $value['finished_num'];
- $model = new DispatchSub();
- $model->setConnection($database);
- $model->where('id',$value['id'])->update([
- 'finished_num' => $finished_num,
- 'waste_num' => $waste[$key],
- 'job_status' => 0,
- 'dispatch_quantity' => DB::raw("dispatch_quantity + {$waste[$key]}"),//派工数量增加 派工单可以继续派送
- ]);
- $insert_waste = $insert_dispatch = $scrapp = [];
- if(! empty($data['waste'][$key])){
- foreach ($data['waste'][$key] as $v){
- for($i = 0 ;$i < $v['num']; $i++){
- $insert_waste[] = [
- 'order_product_id' => $value['order_product_id'],
- 'order_no' => $value['order_no'],
- 'product_no' => $value['product_no'],
- 'product_title' => $value['product_title'],
- 'process_id' => $value['process_id'],
- 'crt_time' => $time,
- 'dispatch_no' => $value['dispatch_no'],
- 'status' => 4,//不良品
- 'team_id' => $team_tmp,
- 'finished_id' => $finished_id_tmp,
- 'equipment_id' => $equipment_id_tmp,
- 'scrapp_id' => $v['scrapp_id']
- ];
- $insert_dispatch[] = [
- 'order_product_id' => $value['order_product_id'],
- 'out_order_no' => $value['out_order_no'],
- 'order_no' => $value['order_no'],
- 'product_no' => $value['product_no'],
- 'product_title' => $value['product_title'],
- 'process_id' => $value['process_id'],
- 'crt_time' => $time,
- 'dispatch_no' => $value['dispatch_no'],
- 'status' => 1, //已派工
- 'team_id' => 0,
- 'finished_id' => 0,
- 'equipment_id' => 0,
- 'scrapp_id' => 0
- ];
- }
- $scrapp[] = [
- 'sale_orders_product_id' => $value['sale_orders_product_id'],
- 'order_product_id' => $value['order_product_id'],
- 'out_order_no' => $value['out_order_no'],
- 'order_no' => $value['order_no'],
- 'customer_no' => $value['customer_no'],
- 'customer_name' => $value['customer_name'],
- 'product_no' => $value['product_no'],
- 'product_title' => $value['product_title'],
- 'product_size' => $value['product_size'],
- 'product_unit' => $value['product_unit'],
- 'technology_material' => $value['technology_material'],
- 'technology_name' => $value['technology_name'],
- 'wood_name' => $value['wood_name'],
- 'price' => $value['price'],
- 'process_mark' => $value['process_mark'],
- 'table_body_mark' => $value['table_body_mark'],
- 'table_header_mark' => $value['table_header_mark'],
- 'crt_time' => $time,
- 'scrapp_num' => $v['num'],
- 'scrapp_id' => $v['scrapp_id']
- ];
- }
- }
- //工序表
- $process_model = new OrdersProductProcess(['channel' => date("Ymd",$value['out_order_no_time'])]);
- $process_model->setConnection($database);
- $process_model->where('order_product_id',$value['order_product_id'])
- ->where('process_id',$value['process_id'])
- ->where('dispatch_no',$value['dispatch_no'])
- ->take($value['quantity'])
- ->update([
- 'finished_time' => $time,
- 'status' => 2,
- 'finished_id' => $finished_id_tmp,
- 'team_id' => $team_tmp,
- 'equipment_id' => $equipment_id_tmp
- ]);
- if(! empty($insert_waste)) $process_model->insert($insert_waste);
- if(! empty($insert_dispatch)) $process_model->insert($insert_dispatch);
- if(! empty($scrapp)) {
- $scrapp_model = new ScrappCount();
- $scrapp_model->setConnection($database);
- $scrapp_model->insert($scrapp);
- }
- //生产订单数量
- if(! empty($waste[$key])){
- $num = $waste[$key];
- $model2 = new OrdersProduct();
- $model2->setConnection($database);
- $model2->where('id',$value['order_product_id'])->update([
- 'production_quantity' => DB::raw("production_quantity + {$num}"),
- 'scrapp_num' => DB::raw("scrapp_num + {$num}"),
- 'dispatch_complete_quantity' => DB::raw("dispatch_complete_quantity + {$num}"),//已派工数量增加
- ]);
- }
- }
- //反写数量
- // $this->writeFinishedQuantity(array_column($result,'sale_orders_product_id'),$database);
- // $this->writeFinishedQuantityByOrdersProductId(array_column($result,'order_product_id'),$database);
- DB::commit();
- }catch (\Exception $e){
- DB::rollBack();
- return [false,$e->getFile() . $e->getLine(). $e->getMessage()];
- }
- return [true,''];
- }
- public function del($data){
- if($this->isEmpty($data,'id')) return [false,'ID不能为空!'];
- return [true,'删除成功'];
- }
- public function orderDetail($data){
- if($this->isEmpty($data,'id')) return [false,'ID不能为空!'];
- $first = DispatchSub::where('id',$data['id'])->first();
- $process_model = new OrdersProductProcess(['channel' => date("Ymd",$first->out_order_no_time)]);
- $result = $process_model->where('del_time',0)
- ->where('dispatch_no',$first->dispatch_no)
- ->where('order_product_id',$first->order_product_id)
- ->where('status',4)
- ->select(DB::raw('count(id) as num'),'scrapp_id')
- ->groupBy('scrapp_id')
- ->get()->toArray();
- $map = Scrapp::whereIn('id',array_column($result,'scrapp_id'))
- ->pluck('title','id')
- ->toArray();
- foreach ($result as $key => $value){
- $result[$key]['scrapp_name'] = $map[$value['scrapp_id']] ?? '';
- }
- return [true,$result];
- }
- public function is_same_month($timestamp1,$timestamp2){
- // 格式化时间戳为年份和月份
- $year1 = date('Y', $timestamp1);
- $month1 = date('m', $timestamp1);
- $year2 = date('Y', $timestamp2);
- $month2 = date('m', $timestamp2);
- if ($year1 === $year2 && $month1 === $month2) {
- return true;
- } else {
- return false;
- }
- }
- public function orderRule($data){
- if(! isset($data['is_finish_all'])) return [false, '完工类型不能为空!'];
- if(empty($data['detail'])) return [false, '完工明细数据不能为空!'];
- $detail_map = $id = $detail_map2 = $waste_map = [];
- foreach ($data['detail'] as $value){
- if(empty($value['id'])) return [false,'请选择完工数据!'];
- $id[] = $value['id'];
- if(empty($value['device_id'])) return [false, '请选择设备!'];
- if(empty($value['team_id'])) return [false,'请选择班组!'];
- if(empty($value['finish_id'])) return [false,'请选择人员!'];
- if(! is_numeric($value['quantity']) || $value['quantity'] < 0) return [false,'完工数量不能小于0!'];
- if(! empty($value['waste'])){
- foreach ($value['waste'] as $waste){
- if(empty($waste['scrapp_id'])) return [false,'请选择损耗原因!'];
- if(! is_numeric($value['num']) || $value['num'] < 0) return [false,'损耗数量不能小于0!'];
- if(isset($waste_map[$value['id']])){
- $waste_map[$value['id']] += $value['num'];
- }else{
- $waste_map[$value['id']] = $value['num'];
- }
- }
- }
- //完工数量
- if(isset($detail_map[$value['id']])){
- $detail_map[$value['id']] += $value['quantity'];
- }else{
- $detail_map[$value['id']] = $value['quantity'];
- }
- if(! isset($detail_map2[$value['id']])){
- $detail_map2[$value['id']] = $value;
- }
- }
- //校验
- $result = DispatchSub::where('del_time',0)
- ->whereIn('id',$id)
- ->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')
- ->get()->toArray();
- if(empty($result)) return [false, '派工单不存在或已被删除!'];
- $process_map = Process::whereIn('id',array_unique(array_column($result,'process_id')))->pluck('title','id')->toArray();
- $search = "";$args = [];
- foreach ($result as $key => $value){
- $detail2 = $detail_map2[$value['id']] ?? [];
- $result[$key]['device_id'] = $detail2['device_id'] ?? 0;
- $result[$key]['team_id'] = $detail2['team_id'] ?? 0;
- $result[$key]['finish_id'] = $detail2['finish_id'] ?? 0;
- $result[$key]['waste_quantity'] = $waste_map[$value['id']] ?? 0;
- $result[$key]['waste_array'] = $detail2['waste'] ?? [];
- $q = $detail_map[$value['id']] ?? 0;
- $quantity_tmp = bcadd($q,$value['finished_num'],3);
- if($quantity_tmp > $value['dispatch_quantity']) {
- $process_tmp = $process_map[$value['process_id']] ?? "";
- return [false,'派工单:' . $value['dispatch_no']. '的工序' . $process_tmp .'完工数量不能大于派工数量'];
- }
- $result[$key]['quantity'] = $q;
- $search_key = $value['order_product_id'] . $value['crt_time'];
- if($data['is_finish_all'] && ! in_array($search_key , $args)){
- $search .= "(order_product_id = {$value['order_product_id']} and crt_time = {$value['crt_time']}) OR ";
- $args[] = $search_key;
- }
- }
- $search = rtrim($search,'OR ');
- $search = "($search)";
- if($data['is_finish_all']){
- //系统帮助完工的数据
- $result2 = DispatchSub::where('del_time',0)
- ->whereNotIn('id',$id)
- ->whereColumn('dispatch_quantity', '>', 'finished_num')
- ->whereRaw($search)
- ->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')
- ->get()->toArray();
- if(! empty($result2)){
- foreach ($result2 as $key => $value){
- $not_finished_num = $value['dispatch_quantity'] - $value['finished_num'];
- $result2[$key]['quantity'] = $not_finished_num;
- $result2[$key]['waste_array'] = [];
- $result2[$key]['waste_quantity'] = 0;
- $result2[$key]['device_id'] = 0;
- $result2[$key]['team_id'] = 0;
- $result2[$key]['finish_id'] = 0;
- }
- }
- $result = array_merge_recursive($result, $result2);
- }
- return [true, $result];
- }
- public function orderList($data){
- $model = FinishedOrderSub::where('del_time',0)
- ->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')
- ->orderBy('upd_time','desc')
- ->orderBy('id','desc');
- if(! empty($data['order_no'])) $model->where('order_no', 'LIKE', '%'.$data['order_no'].'%');
- if(! empty($data['product_title'])) $model->where('product_title', 'LIKE', '%'.$data['product_title'].'%');
- if(! empty($data['product_size'])) $model->where('product_size', 'LIKE', '%'.$data['product_size'].'%');
- if(! empty($data['technology_material'])) $model->where('technology_material', 'LIKE', '%'.$data['technology_material'].'%');
- if(! empty($data['technology_name'])) $model->where('technology_name', 'LIKE', '%'.$data['technology_name'].'%');
- if(! empty($data['wood_name'])) $model->where('wood_name', 'LIKE', '%'.$data['wood_name'].'%');
- if(! empty($data['process_mark'])) $model->where('process_mark', 'LIKE', '%'.$data['process_mark'].'%');
- if(! empty($data['table_header_mark'])) $model->where('table_header_mark', 'LIKE', '%'.$data['table_header_mark'].'%');
- if(! empty($data['table_body_mark'])) $model->where('table_body_mark', 'LIKE', '%'.$data['table_body_mark'].'%');
- if(! empty($data['dispatch_time'][0]) && ! empty($data['dispatch_time'][1])) $model->whereBetween('dispatch_time',[$data['dispatch_time'][0],$data['dispatch_time'][1]]);
- if(! empty($data['employee_id'])) {
- $team_id = Employee::from('employee as a')
- ->leftJoin('employee_team_permission as b','b.employee_id','a.id')
- ->where('a.id', $data['employee_id'])
- ->select('b.team_id')
- ->get()->toArray();
- $team_id = array_column($team_id,'team_id');
- $model->where('team_id',$team_id ?? []);
- }
- if(isset($data['status'])) $model->where('status',$data['status']);
- $list = $this->limit($model,'',$data);
- $list = $this->fillData($list);
- return [true,$list];
- }
- public function fillData($data){
- if(empty($data['data'])) return $data;
- $waste_map = $waste_map_2 = [];
- $waste = FinishedOrderScrapp::where('del_time',0)
- ->whereIn('finished_order_id',array_column($data['data'],'id'))
- ->select('finished_order_id','num','scrapp_id')
- ->get()->toArray();
- if(! empty($waste)){
- foreach ($waste as $value){
- $waste_map[$value['finished_order_id']][] = [
- 'num' => $value['num'],
- 'scrapp_id' => $value['scrapp_id']
- ];
- if(isset($waste_map_2[$value['finished_order_id']])){
- $waste_map_2[$value['finished_order_id']] += $value['num'];
- }else{
- $waste_map_2[$value['finished_order_id']] = $value['num'];
- }
- }
- }
- $team = EmployeeTeamPermission::from('employee_team_permission as a')
- ->leftJoin('employee as b','b.id','a.employee_id')
- ->whereIn('a.team_id',array_column($data['data'],'team_id'))
- ->select('b.emp_name','a.team_id')
- ->get()
- ->toArray();
- $team_map = [];
- if(! empty($team)){
- foreach ($team as $value){
- if(isset($team_map[$value['team_id']])){
- $team_map[$value['team_id']] .= ','. $value['emp_name'];
- }else{
- $team_map[$value['team_id']] = $value['emp_name'];
- }
- }
- }
- $process_map = Process::whereIn('id',array_column($data['data'],'process_id'))
- ->pluck('title','id')
- ->toArray();
- $team_maps = Team::whereIn('id',array_column($data['data'],'team_id'))
- ->pluck('title','id')
- ->toArray();
- $equipment_map = Equipment::whereIn('id',array_column($data['data'],'equipment_id'))
- ->pluck('title','id')
- ->toArray();
- foreach ($data['data'] as $key => $value){
- $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d',$value['crt_time']) : '';
- $time1 = $value['dispatch_time_start'] ? date('Y-m-d',$value['dispatch_time_start']) : '';
- $time2 = $value['dispatch_time_end'] ? date('Y-m-d',$value['dispatch_time_end']) : '';
- $data['data'][$key]['dispatch_plan_time'] = $time1 . ' ' . $time2;
- $data['data'][$key]['dispatch_time'] = $value['dispatch_time'] ? date('Y-m-d',$value['dispatch_time']) : '';
- $data['data'][$key]['team_man'] = $team_map[$value['team_id']] ?? '';
- $data['data'][$key]['process_name'] = $process_map[$value['process_id']] ?? '';
- $data['data'][$key]['team_name'] = $team_maps[$value['team_id']] ?? '';
- $data['data'][$key]['equipment_name'] = $equipment_map[$value['equipment_id']] ?? '';
- $data['data'][$key]['waste'] = $waste_map[$value['id']] ?? [];
- $data['data'][$key]['waste_quantity'] = $waste_map_2[$value['id']] ?? 0;
- $data['data'][$key]['not_finished_num'] = $value['dispatch_quantity'] - $value['finished_num'];
- }
- $data['finished_num'] = $this->getTotal($data['data'], 'finished_num');
- $data['dispatch_quantity'] = $this->getTotal($data['data'], 'dispatch_quantity');
- $data['waste_quantity'] = $this->getTotal($data['data'], 'waste_quantity');
- $data['not_finished_num'] = $this->getTotal($data['data'], 'not_finished_num');
- return $data;
- }
- //反写销售订单完工数量
- public function writeFinishedQuantity($orders_product_id){
- if(empty($orders_product_id)) return;
- $sale_orders_product_id = OrdersProduct::where('del_time',0)
- ->whereIn('id', $orders_product_id)
- ->select('sale_orders_product_id')
- ->get()->toArray();
- $sale_orders_product_id = array_unique(array_column($sale_orders_product_id,'sale_orders_product_id'));
- $production_order = OrdersProduct::where('del_time',0)
- ->whereIn('sale_orders_product_id', $sale_orders_product_id)
- ->select(DB::raw("sum(finished_num) as finished_num"),'sale_orders_product_id')
- ->groupby('sale_orders_product_id')
- ->pluck('finished_num','sale_orders_product_id')
- ->toArray();
- foreach ($production_order as $sale_orders_product_id => $finished_num){
- SaleOrdersProduct::where('id',$sale_orders_product_id)->update([
- 'finished_num' => $finished_num,
- ]);
- }
- }
- //反写生产订单完工数量
- public function writeFinishedQuantityByOrdersProductId($order_product_id){
- if(empty($order_product_id)) return;
- $result = DispatchSub::where('del_time',0)
- ->whereIn('order_product_id',$order_product_id)
- ->select('finished_num','order_product_id','crt_time','process_id')
- ->get()
- ->toArray();
- if(empty($result)) return;
- //生产订单
- $production_order = OrdersProduct::where('del_time',0)
- ->whereIn('id',array_unique(array_column($result,'order_product_id')))
- ->select('process_id','id','production_quantity')
- ->get()
- ->toArray();
- $judge = [];
- foreach ($result as $value){
- if(isset($judge[$value['order_product_id']][$value['process_id']])){
- $judge[$value['order_product_id']][$value['process_id']] += $value['finished_num'];
- }else{
- $judge[$value['order_product_id']][$value['process_id']] = $value['finished_num'];
- }
- }
- foreach ($production_order as $value){
- //生产订单下的所有工序派工单
- $tmp = $judge[$value['id']] ?? [];
- if(empty($tmp)) continue;
- //生产订单下的工序种类
- $ori_process = explode(',',$value['process_id']);
- $ori_process_count = count($ori_process);
- //生产订单下的派工单的工序种类
- $process = array_keys($tmp);
- $process_count = count($process);
- if($ori_process_count != $process_count) continue;
- //获取工序里最小的数值
- $min_finished_num = min($tmp);
- //更新完工数量
- OrdersProduct::where('id',$value['id'])->update([
- 'finished_num' => $min_finished_num
- ]);
- }
- }
- public function mobileAdd($data,$user){
- //数据校验以及填充
- list($status,$msg,$count_arr) = $this->orderMobileRule($data);
- if(!$status) return [$status,$msg];
- $user = [
- 'id' => $user['id'],
- 'operate_time' => time(),
- 'zt' => Request()->header('zt'),
- ];
- $redis = [
- 'result' => $msg,
- 'data' => $data,
- 'quantity_count' => $count_arr,
- ];
- $job = ProcessDataJob::dispatch($redis,$user,3)->onQueue(ProcessDataJob::job_one);
- if(! $job) return [false,'任务没有进入队列!'];
- //错误计数
- Redis::hSet('order_failures', md5(json_encode($redis)), 0);
- //标记进入队列的数据
- DispatchSub::whereIn('id',array_column($msg,'id'))->update([
- 'job_status' => 1,
- ]);
- return [true,'任务已进入队列!'];
- }
- public function addMobileInJob($result, $data, $count_arr,$user){
- try{
- //获取数据库连接
- $database = $this->getConnectionName($user['zt']);
- //用友数据写入------------
- $insert_sql_server = [];
- foreach ($result as $key => $value){
- $quantity_tmp = $count_arr[$value['id']];
- $result[$key]['quantity'] = $quantity_tmp;
- $process_model = new OrdersProductProcess(['channel' => date("Ymd",$value['out_order_no_time'])]);
- $process_model->setConnection($database);
- $process_id = $process_model->select('process_id')
- ->where('sort',$process_model->where('del_time',0)
- ->where('order_product_id',$value['order_product_id'])
- ->max('sort'))
- ->first();
- if(empty($process_id)) return [false,"未找到最后一道工序"];
- $process_id = $process_id->process_id;
- if($process_id == $value['process_id']){
- $insert_sql_server[] = $result[$key];
- }
- }
- if(! empty($insert_sql_server)){
- $sqlServerModel = new FyySqlServerService($user);
- if($sqlServerModel->error) return [false,$sqlServerModel->error];
- foreach ($insert_sql_server as $value){
- // list($status,$msg) = $sqlServerModel->U8Rdrecord10Save($value);
- // if(! $status) return [false,$msg];
- }
- }
- //用友数据写入结束------------
- //本地数据写入-----------
- DB::beginTransaction();
- $waste = $waste2 = [];
- foreach ($data as $value){
- if(! empty($value['waste'])){
- foreach ($value['waste'] as $v){
- if(isset($waste[$value['id']])){
- $waste[$value['id']] += $v['num'];
- $waste2[$value['order_product_id']] += $v['num'];
- }else{
- $waste[$value['id']] = $v['num'];
- $waste2[$value['order_product_id']] = $v['num'];
- }
- }
- }
- }
- $time = time();
- foreach ($result as $value){
- $finished_num = $value['quantity'] + $value['finished_num'];
- $watste_num = $waste[$value['id']] ?? 0;
- $model = new DispatchSub();
- $model->setConnection($database);
- $model->where('id',$value['id'])->update([
- 'finished_num' => $finished_num,
- 'waste_num' => $watste_num,
- 'job_status' => 0,
- 'dispatch_quantity' => DB::raw("dispatch_quantity + {$watste_num}"),//派工数量增加 派工单可以继续派送
- ]);
- //生产订单数量
- if(! empty($waste2[$value['order_product_id']])){
- $num = $waste2[$value['order_product_id']];
- $model2 = new OrdersProduct();
- $model2->setConnection($database);
- $model2->where('id',$value['order_product_id'])->update([
- 'production_quantity' => DB::raw("production_quantity + {$num}"),
- 'scrapp_num' => DB::raw("scrapp_num + {$num}"),
- 'dispatch_complete_quantity' => DB::raw("dispatch_complete_quantity + {$num}"),//已派工数量增加
- ]);
- }
- //损耗和派工 损耗统计
- $insert_waste = $insert_dispatch = $scrapp = [];
- //工序
- $process_model = new OrdersProductProcess(['channel' => date("Ymd",$value['out_order_no_time'])]);
- $process_model->setConnection($database);
- foreach ($data as $d){
- if($d['id'] == $value['id']){
- $process_model->where('order_product_id',$value['order_product_id'])
- ->where('process_id',$value['process_id'])
- ->where('dispatch_no',$value['dispatch_no'])
- ->take($d['quantity'])
- ->update([
- 'finished_time' => $time,
- 'status' => 2,
- 'finished_id' => $d['finished_id'],
- 'team_id' => $d['team_id'],
- 'equipment_id' => $d['equipment_id']
- ]);
- if(! empty($d['waste'])){
- foreach ($d['waste'] as $v){
- for($i = 0 ;$i < $v['num']; $i++){
- $insert_waste[] = [
- 'order_product_id' => $value['order_product_id'],
- 'out_order_no' => $value['out_order_no'],
- 'order_no' => $value['order_no'],
- 'product_no' => $value['product_no'],
- 'product_title' => $value['product_title'],
- 'process_id' => $value['process_id'],
- 'crt_time' => $time,
- 'dispatch_no' => $value['dispatch_no'],
- 'status' => 4,
- 'team_id' => $d['team_id'],
- 'finished_id' => $d['finished_id'],
- 'equipment_id' => $d['equipment_id'],
- 'scrapp_id' => $v['scrapp_id']
- ];
- $insert_dispatch[] = [
- 'order_product_id' => $value['order_product_id'],
- 'out_order_no' => $value['out_order_no'],
- 'order_no' => $value['order_no'],
- 'product_no' => $value['product_no'],
- 'product_title' => $value['product_title'],
- 'process_id' => $value['process_id'],
- 'crt_time' => $time,
- 'dispatch_no' => $value['dispatch_no'],
- 'status' => 1, //已派工
- 'team_id' => 0,
- 'finished_id' => 0,
- 'equipment_id' => 0,
- 'scrapp_id' => 0
- ];
- }
- $scrapp[] = [
- 'sale_orders_product_id' => $value['sale_orders_product_id'],
- 'order_product_id' => $value['order_product_id'],
- 'out_order_no' => $value['out_order_no'],
- 'order_no' => $value['order_no'],
- 'customer_no' => $value['customer_no'],
- 'customer_name' => $value['customer_name'],
- 'product_no' => $value['product_no'],
- 'product_title' => $value['product_title'],
- 'product_size' => $value['product_size'],
- 'product_unit' => $value['product_unit'],
- 'technology_material' => $value['technology_material'],
- 'technology_name' => $value['technology_name'],
- 'wood_name' => $value['wood_name'],
- 'price' => $value['price'],
- 'process_mark' => $value['process_mark'],
- 'table_body_mark' => $value['table_body_mark'],
- 'table_header_mark' => $value['table_header_mark'],
- 'crt_time' => $time,
- 'scrapp_num' => $v['num'],
- 'scrapp_id' => $v['scrapp_id']
- ];
- }
- }
- }
- }
- if(! empty($insert_waste)) $process_model->insert($insert_waste);
- if(! empty($insert_dispatch)) $process_model->insert($insert_dispatch);
- if(! empty($scrapp)) {
- $scrapp_model = new ScrappCount();
- $scrapp_model->setConnection($database);
- $scrapp_model->insert($scrapp);
- }
- }
- //反写数量
- // $this->writeFinishedQuantity(array_column($result,'sale_orders_product_id'),$database);
- // $this->writeFinishedQuantityByOrdersProductId(array_column($result,'order_product_id'),$database);
- DB::commit();
- //本地数据写入结束-----------
- }catch (\Exception $e){
- DB::rollBack();
- return [false,$e->getFile() . $e->getLine() . $e->getMessage()];
- }
- return [true,''];
- }
- public function orderMobileRule($data){
- if(empty($data)) return [false,'数据不能为空!',''];
- $dispatch_id = array_unique(array_column($data,'id'));
- $bool = DispatchSub::whereIn('id',$dispatch_id)->where('job_status',1)->exists();
- if($bool) return [false,'正在队列中,请不要重复操作!',''];
- $post = $waste = [];
- foreach ($data as $value){
- if(! is_numeric($value['quantity']) || $value['quantity'] <= 0) return [false,'请填写正确的完工数量!',''];
- if(empty($value['finished_id']) && empty($value['team_id'])) return [false,'人员和班组必须填写一项!',''];
- if(isset($post[$value['id']])){
- $post[$value['id']] += $value['quantity'];
- }else{
- $post[$value['id']] = $value['quantity'];
- }
- if(! empty($value['waste'])){
- foreach ($value['waste'] as $v){
- if(isset($waste[$value['id']])){
- $waste[$value['id']] += $v['num'];
- }else{
- $waste[$value['id']] = $v['num'];
- }
- }
- }
- }
- $result = DispatchSub::whereIn('id',$dispatch_id)
- ->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')
- ->orderBy('id','desc')
- ->get()->toArray();
- foreach ($result as $key => $value){
- $tmp = $waste[$value['id']] ?? 0;
- $quantity_tmp = $post[$value['id']] + $value['finished_num'] + $value['waste_num'] + $tmp;
- if($quantity_tmp > $value['dispatch_quantity']) return [false,"完工数量加上损耗数量不能大于派工数量",''];
- }
- return [true, $result, $post];
- }
- public function getConnectionName($name)
- {
- $mysql = "mysql";
- if ($name === '001') {
- $mysql = "mysql_001";
- } elseif ($name === '002') {
- $mysql = "mysql_002";
- }
- return $mysql;
- }
- public function insertYy($order, $user){
- //包装单
- $package_data = json_decode($order['post'], true);
- if(! empty($package_data)) {
- //用友 ------产成品入库
- $service = new FinishedOrderService();
- list($status,$msg) = $service->U8Rdrecord10Save($package_data,$user);
- if(! $status) return [false, $msg];
- }
- return [true, ''];
- }
- }
|