|
@@ -0,0 +1,256 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace App\Service;
|
|
|
+
|
|
|
+use App\Model\Dispatch;
|
|
|
+use App\Model\DispatchSub;
|
|
|
+use App\Model\Employee;
|
|
|
+use App\Model\EmployeeTeamPermission;
|
|
|
+use App\Model\Equipment;
|
|
|
+use App\Model\FinishedOrder;
|
|
|
+use App\Model\FinishedOrderSub;
|
|
|
+use App\Model\OrdersProduct;
|
|
|
+use App\Model\OrdersProductProcess;
|
|
|
+use App\Model\Process;
|
|
|
+use App\Model\SaleOrdersProduct;
|
|
|
+use App\Model\Team;
|
|
|
+use Illuminate\Support\Facades\DB;
|
|
|
+
|
|
|
+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];
|
|
|
+
|
|
|
+ $finished_no = $this->setOrderNO();
|
|
|
+ try{
|
|
|
+ DB::beginTransaction();
|
|
|
+
|
|
|
+ //主表
|
|
|
+ FinishedOrder::insert(['finished_no' => $finished_no, 'crt_time' => time()]);
|
|
|
+
|
|
|
+ //生产数据的源数据
|
|
|
+ $result = $msg;
|
|
|
+ $time = time();
|
|
|
+
|
|
|
+ date_default_timezone_set("PRC");
|
|
|
+ foreach ($result as $key => $value){
|
|
|
+ $quantity_tmp = $data['quantity'][$key];
|
|
|
+ $quantity_tmp2 = $data['waste_quantity'][$key] ?? 0;
|
|
|
+ $scrapp = $data['scrapp'][$key] ?? 0;
|
|
|
+
|
|
|
+ $result[$key]['finished_no'] = $finished_no;
|
|
|
+ $result[$key]['finished_num'] = $quantity_tmp;
|
|
|
+ $result[$key]['waste_quantity'] = $quantity_tmp2;
|
|
|
+ $result[$key]['scrapp_id'] = $scrapp;
|
|
|
+ $result[$key]['crt_time'] = $time;
|
|
|
+ $result[$key]['crt_id'] = $user['id'];
|
|
|
+
|
|
|
+ $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',$data['process_id'])
|
|
|
+ ->where('dispatch_no',$value['dispatch_no'])
|
|
|
+ ->take($quantity_tmp)
|
|
|
+ ->update([
|
|
|
+ 'finished_no' => $finished_no,
|
|
|
+ 'status' => 2
|
|
|
+ ]);
|
|
|
+
|
|
|
+ unset($result[$key]['order_product_id']);
|
|
|
+ unset($result[$key]['dispatch_no']);
|
|
|
+ }
|
|
|
+
|
|
|
+ FinishedOrderSub::insert($result);
|
|
|
+
|
|
|
+ $this->writeFinishedQuantity(array_column($result,'sale_orders_product_id'));
|
|
|
+
|
|
|
+ DB::commit();
|
|
|
+ }catch (\Exception $e){
|
|
|
+ DB::rollBack();
|
|
|
+ return [false,$e->getLine().':'.$e->getMessage()];
|
|
|
+ }
|
|
|
+
|
|
|
+ return [true,'保存成功!'];
|
|
|
+ }
|
|
|
+
|
|
|
+ public function del($data){
|
|
|
+ if($this->isEmpty($data,'id')) return [false,'ID不能为空!'];
|
|
|
+
|
|
|
+ return [true,'删除成功'];
|
|
|
+ }
|
|
|
+
|
|
|
+ public function orderDetail($data){
|
|
|
+ return [200,''];
|
|
|
+ }
|
|
|
+
|
|
|
+ public function is_same_month($timestamp1,$timestamp2){
|
|
|
+ date_default_timezone_set("PRC");
|
|
|
+
|
|
|
+ // 格式化时间戳为年份和月份
|
|
|
+ $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 orderList($data){
|
|
|
+ $model = FinishedOrderSub::where('del_time',0)
|
|
|
+ ->select('id','order_no','table_header_mark','product_no','product_title','product_size','product_unit','order_quantity','technology_material','technology_name','wood_name','process_mark','table_body_mark','dispatch_quantity','finished_num','waste_quantity','production_no','status','crt_id','process_id','equipment_id','team_id','dispatch_time_start','dispatch_time_end','dispatch_time')
|
|
|
+ ->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(isset($data['status'])) $model->where('status',$data['status']);
|
|
|
+
|
|
|
+ $list = $this->limit($model,'',$data);
|
|
|
+ $list = $this->fillData($list);
|
|
|
+
|
|
|
+ return [true,$list];
|
|
|
+ }
|
|
|
+
|
|
|
+ public function orderRule($data){
|
|
|
+ if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
|
|
|
+ if($this->isEmpty($data,'quantity')) return [false,'请填写完工数量!'];
|
|
|
+
|
|
|
+ $result = DispatchSub::whereIn('id',$data['id'])
|
|
|
+ ->select('order_product_id','dispatch_no','id as dispatch_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','process_id','equipment_id','team_id','dispatch_time_start','dispatch_time_end','crt_time as dispatch_time','sale_orders_product_id','out_order_no_time')
|
|
|
+ ->get()->toArray();
|
|
|
+
|
|
|
+ //已完工数据
|
|
|
+ $map = $this->getFinishedQuantity($data['id']);
|
|
|
+
|
|
|
+ foreach ($result as $key => $value){
|
|
|
+ if(isset($map[$value['dispatch_id']])){
|
|
|
+ if($map[$value['dispatch_id']] + $data['quantity'][$key] > $value['dispatch_quantity']) return [false,'完工数量不能大于派工数量'];
|
|
|
+ }else{
|
|
|
+ if($data['quantity'][$key] > $value['dispatch_quantity']) return [false,'完工数量不能大于派工数量'];
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return [true, $result];
|
|
|
+ }
|
|
|
+
|
|
|
+ public function fillData($data){
|
|
|
+ if(empty($data['data'])) return $data;
|
|
|
+
|
|
|
+ $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();
|
|
|
+
|
|
|
+ date_default_timezone_set("PRC");
|
|
|
+ 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['finished_num'] = array_sum(array_column($data['data'], 'finished_num'));
|
|
|
+ $data['waste_quantity'] = array_sum(array_column($data['data'], 'waste_quantity'));
|
|
|
+
|
|
|
+ return $data;
|
|
|
+ }
|
|
|
+
|
|
|
+ //返回已完工工数量
|
|
|
+ public function getFinishedQuantity($dispatch_id = []){
|
|
|
+ if(empty($dispatch_id)) return [];
|
|
|
+
|
|
|
+ $result = FinishedOrderSub::where('del_time',0)
|
|
|
+ ->whereIn("dispatch_id",$dispatch_id)
|
|
|
+ ->select(DB::raw("sum(dispatch_quantity) as finished_num"),'dispatch_id')
|
|
|
+ ->groupby('dispatch_id')
|
|
|
+ ->pluck('finished_num','dispatch_id')
|
|
|
+ ->toArray();
|
|
|
+
|
|
|
+ return $result;
|
|
|
+ }
|
|
|
+
|
|
|
+ //反写写完工数量
|
|
|
+ public function writeFinishedQuantity($sale_orders_product_id){
|
|
|
+ if(empty($sale_orders_product_id)) return;
|
|
|
+
|
|
|
+ $result = FinishedOrderSub::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();
|
|
|
+
|
|
|
+ if(empty($result)) return;
|
|
|
+
|
|
|
+ foreach ($result as $key => $value){
|
|
|
+ SaleOrdersProduct::where('id',$key)->update([
|
|
|
+ 'finished_num' => $value
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|