max('production_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]; list($status,$msg) = $this->limitingSendRequestBackgNeed($this->lock_key); if(! $status) return [false, '销售订单正在生成生产订单数据,请稍后尝试!']; $insert_data = [ 'data' => $data, 'user' => $user ]; CommandList::insert([ 'data' => json_encode($insert_data), 'function' => 'productionAdd' ]); return [true, '生成订单数据后台生成中......']; //以下用不到----------------------------- return; $production_no = $this->setOrderNO(); if(empty($production_no)) return [false,'单据号生成失败!']; //工序 $process_arr = $data['process_id']; try{ DB::beginTransaction(); $time = time(); //主表数据写入 OrdersProductMain::insert(['production_no' => $production_no,'crt_time' => $time,'crt_id' => $user['id'], 'process_id' => implode(',',$process_arr)]); //生产数据的源数据 $result = $msg[0]; $quantity_map = $msg[1]; $boom = $process = []; foreach ($result as $key => $value){ $result[$key]['process_id'] = implode(',', $process_arr); $result[$key]['production_no'] = $production_no; $result[$key]['production_quantity'] = $quantity_map[$value['sale_orders_product_id']]; $result[$key]['production_time'] = $time; $result[$key]['crt_id'] = $user['id']; } OrdersProduct::insert($result); //获取上一次插入订单的所有id $last_insert_id = OrdersProduct::where('production_no',$production_no)->select('id')->get()->toArray(); $last_insert_id = array_column($last_insert_id,'id'); //组织process bom的数据 写入与主表的关联id $time_arr = []; foreach ($result as $key => $value){ $quantity_tmp = $quantity_map[$value['sale_orders_product_id']]; $time_tmp = date("Ymd", $value['out_order_no_time']); if(! in_array($time_tmp,$time_arr)) $time_arr[] = $time_tmp; for ($i = 1; $i <= $quantity_tmp; $i++) { $boom[$time_tmp][] = [ 'order_product_id' => $last_insert_id[$key], 'production_no' => $production_no, 'order_no' => $value['order_no'], 'out_order_no' => $value['out_order_no'], 'product_no' => $value['product_no'], 'product_title' => $value['product_title'], 'crt_time' => $time ]; foreach ($process_arr as $k => $v){ $process[$time_tmp][] = [ 'order_product_id' => $last_insert_id[$key], 'production_no' => $production_no, 'process_id' => $v, 'order_no' => $value['order_no'], 'out_order_no' => $value['out_order_no'], 'product_no' => $value['product_no'], 'product_title' => $value['product_title'], 'crt_time' => $time, 'sort' => $k, ]; } } } //反写已经生产数量 $this->writeProductionQuantity($data['id']); foreach ($time_arr as $t){ $boom_model = new OrdersProductBom(['channel'=> $t]); $boom_model->insert($boom[$t]); $process_model = new OrdersProductProcess(['channel' => $t]); $process_model->insert($process[$t]); } unset($boom);unset($process); DB::commit(); }catch (\Exception $e){ DB::rollBack(); return [false,$e->getLine().':'.$e->getMessage()]; } return [true, '']; //以下用不到----------------------------- } public function delBoxLock(){ $this->dellimitingSendRequestBackgNeed($this->lock_key); } public function del($data){ if($this->isEmpty($data,'id')) return [false,'ID不能为空!']; return [true,'删除成功']; } public function orderDetail($data){ return [200,'']; } public function orderList($data){ $model = OrdersProduct::where('del_time',0) ->select('id','order_no','out_order_no','out_order_no_time','customer_no','customer_name','table_header_mark','product_no','product_title','product_size','product_unit','order_quantity','technology_material','technology_name','wood_name','process_mark','table_body_mark','out_crt_man','out_checker_man','out_checker_time','production_quantity','production_time','production_no','status','sale_orders_product_id','dispatch_complete_quantity','pre_shipment_time') ->orderBy('id','desc'); if(! empty($data['order_no'])) $model->where('order_no', 'LIKE', '%'.$data['order_no'].'%'); if(! empty($data['out_order_no'])) $model->where('out_order_no', 'LIKE', '%'.$data['out_order_no'].'%'); if(! empty($data['production_no'])) $model->where('production_no', 'LIKE', '%'.$data['production_no'].'%'); if(! empty($data['customer_name'])) $model->where('customer_name', 'LIKE', '%'.$data['customer_name'].'%'); 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['out_checker_man'])) $model->where('out_checker_man', 'LIKE', '%'.$data['out_checker_man'].'%'); if(! empty($data['out_crt_man'])) $model->where('out_crt_man', 'LIKE', '%'.$data['out_crt_man'].'%'); if(! empty($data['out_checker_time'][0]) && ! empty($data['out_checker_time'][1])) $model->whereBetween('out_checker_time',[$data['out_checker_time'][0],$data['out_checker_time'][1]]); if(! empty($data['out_order_no_time'][0]) && ! empty($data['out_order_no_time'][1])) $model->whereBetween('out_order_no_time',[$data['out_order_no_time'][0],$data['out_order_no_time'][1]]); if(! empty($data['production_time'][0]) && ! empty($data['production_time'][1])) $model->whereBetween('production_time',[$data['production_time'][0],$data['production_time'][1]]); if(! empty($data['pre_shipment_time'][0]) && ! empty($data['pre_shipment_time'][1])) $model->whereBetween('pre_shipment_time',[$data['pre_shipment_time'][0],$data['pre_shipment_time'][1]]); if(isset($data['status'])) $model->where('status',$data['status']); if(isset($data['is_create'])) { if($data['is_create']){ $model->whereColumn('dispatch_complete_quantity', '>=', 'production_quantity'); }else{ $model->whereColumn('production_quantity', '>', 'dispatch_complete_quantity'); } } $list = $this->limit($model,'',$data); $list = $this->fillData($list); return [200,$list]; } public function orderRule(&$data){ if($this->isEmpty($data,'process_id')) return [false, '工序不能为空!']; //工序 $process = Process::whereIn('id', $data['process_id'])->get()->toArray(); if(empty($process)) return [false,'工序不存在或已被删除!']; $process_id = []; foreach ($process as $value){ if($value['del_time'] > 0) return [false, '工序:' . $value['title'] . '不存在或已被删除!']; $process_id[] = $value['id']; } $data['process_id'] = $process_id; if($this->isEmpty($data,'id')) return [false,'请选择数据!']; if($this->isEmpty($data,'quantity')) return [false,'数量不能为空!']; $quantities = array_map('floatval', $data['quantity']); if(in_array(false, $data['quantity'], true) || in_array(0.0, $quantities, true) || in_array('', $data['quantity'], true)) return [false,'数量不能为空!']; $map = []; foreach ($data['id'] as $key => $value){ $map[$value] = $data['quantity'][$key]; } $result = SaleOrdersProduct::whereIn('id',$data['id']) ->select('id as sale_orders_product_id','order_no','out_order_no','out_order_no_time','customer_no','customer_name','table_header_mark','product_no','product_title','product_size','product_unit','order_quantity','technology_material','technology_name','wood_name','process_mark','table_body_mark','out_crt_man','out_checker_man','out_checker_time','production_quantity','price','pre_shipment_time') ->orderBy('id','asc') ->get()->toArray(); // foreach ($result as $key => $value){ // $quantity_tmp = $map[$value['sale_orders_product_id']] ?? 0; // if($value['production_quantity'] + $quantity_tmp > $value['order_quantity']) return [false,'生产数量不能大于订单数量']; // unset($result[$key]['production_quantity']);//删除销售订单的已生产数量 // } return [true, [$result,$map]]; } public function fillData($data){ if(empty($data['data'])) return $data; $sale_orders_product_id = array_unique(array_column($data['data'],'sale_orders_product_id')); $production_map = $this->getProductionOrderQuantity($sale_orders_product_id); foreach ($data['data'] as $key => $value){ $data['data'][$key]['out_order_no_time'] = $value['out_order_no_time'] ? date('Y-m-d',$value['out_order_no_time']) : ''; $data['data'][$key]['out_checker_time'] = $value['out_checker_time'] ? date('Y-m-d',$value['out_checker_time']) : ''; $data['data'][$key]['production_time'] = $value['production_time'] ? date('Y-m-d',$value['production_time']) : ''; $data['data'][$key]['pre_shipment_time'] = $value['pre_shipment_time'] ? date('Y-m-d',$value['pre_shipment_time']) : ''; $all_production = $production_map[$value['sale_orders_product_id']] ?? 0; $not_production = bcsub($value['order_quantity'] , $all_production,3); if($not_production < 0) $not_production = "0.000"; $data['data'][$key]['not_production'] = $not_production; if($value['dispatch_complete_quantity'] >= $value['production_quantity']){ $data['data'][$key]['is_create'] = 1; }else{ $data['data'][$key]['is_create'] = 0; } $data['data'][$key]['status_title'] = OrdersProduct::$status_name[$value['status']] ?? ""; } return $data; } //反写已生产数量(添加) public function writeProductionQuantity($sale_orders_product_id){ if(empty($sale_orders_product_id)) return; $result = OrdersProduct::where('del_time',0) ->whereIn('sale_orders_product_id',$sale_orders_product_id) ->select(DB::raw("sum(production_quantity) as production_quantity"),'sale_orders_product_id') ->groupby('sale_orders_product_id') ->pluck('production_quantity','sale_orders_product_id') ->toArray(); if(empty($result)) return; foreach ($result as $key => $value){ SaleOrdersProduct::where('id',$key)->update([ 'production_quantity' => $value ]); } } //反写已生产数量(删除) public function writeProductionQuantityDEL($sale_orders_product_id){ if(empty($sale_orders_product_id)) return; $result = OrdersProduct::where('del_time',0) ->whereIn('sale_orders_product_id',$sale_orders_product_id) ->select(DB::raw("sum(production_quantity) as production_quantity"),'sale_orders_product_id') ->groupby('sale_orders_product_id') ->pluck('production_quantity','sale_orders_product_id') ->toArray(); foreach ($sale_orders_product_id as $value){ $quantity = $result[$value] ?? 0; SaleOrdersProduct::where('id',$value)->update([ 'production_quantity' => $quantity ]); } } //返回已生产数量 public function getProductionOrderQuantity($data){ if(empty($data)) return []; $result = OrdersProduct::where('del_time',0) ->whereIn('sale_orders_product_id',$data) ->select(DB::raw("sum(production_quantity) as production_quantity"),'sale_orders_product_id') ->groupby('sale_orders_product_id') ->pluck('production_quantity','sale_orders_product_id') ->toArray(); return $result; } //销售订单的 已生产数量 public function getSaleProductProductionQuantity($data){ if(empty($data)) return []; $result = SaleOrdersProduct::where('del_time',0) ->whereIn('id',$data) ->select(DB::raw("sum(production_quantity) as production_quantity"),'id') ->groupby('id') ->pluck('production_quantity','id') ->toArray(); return $result; } public function productionExport($data){ if(empty($data['id']) || ! is_array($data['id'])) return [false, '请选择导出的数据']; $model = OrdersProduct::where('del_time',0) ->whereIn('id',$data['id']) ->select('production_time', 'production_no', 'out_order_no_time','out_order_no','customer_no','customer_name','table_header_mark','product_no','product_title','product_size','product_unit','order_quantity','production_quantity','technology_material','technology_name','wood_name','process_mark','table_body_mark','out_crt_man','sale_orders_product_id') ->orderBy('id','desc'); if(! empty($data['order_no'])) $model->where('order_no', 'LIKE', '%'.$data['order_no'].'%'); if(! empty($data['out_order_no'])) $model->where('out_order_no', 'LIKE', '%'.$data['out_order_no'].'%'); if(! empty($data['production_no'])) $model->where('production_no', 'LIKE', '%'.$data['production_no'].'%'); if(! empty($data['customer_name'])) $model->where('customer_name', 'LIKE', '%'.$data['customer_name'].'%'); 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['out_checker_man'])) $model->where('out_checker_man', 'LIKE', '%'.$data['out_checker_man'].'%'); if(! empty($data['out_crt_man'])) $model->where('out_crt_man', 'LIKE', '%'.$data['out_crt_man'].'%'); if(! empty($data['out_checker_time'][0]) && ! empty($data['out_checker_time'][1])) $model->whereBetween('out_checker_time',[$data['out_checker_time'][0],$data['out_checker_time'][1]]); if(! empty($data['out_order_no_time'][0]) && ! empty($data['out_order_no_time'][1])) $model->whereBetween('out_order_no_time',[$data['out_order_no_time'][0],$data['out_order_no_time'][1]]); if(! empty($data['production_time'][0]) && ! empty($data['production_time'][1])) $model->whereBetween('production_time',[$data['production_time'][0],$data['production_time'][1]]); if(isset($data['status'])) $model->where('status',$data['status']); $list = $model->get()->toArray(); //组织数据 $data = $this->fillExportData($list); //保存导出数据 $name = $this->saveExportData($data); return [true,$name]; } public function fillExportData($data){ if(empty($data)) return $data; $sale_orders_product_id = array_unique(array_column($data,'sale_orders_product_id')); $production_map = $this->getProductionOrderQuantity($sale_orders_product_id); $exportFields = ['production_time', 'production_no', 'out_order_no_time','out_order_no','customer_no','customer_name','table_header_mark','product_no','product_title','product_size','product_unit','order_quantity','production_quantity','not_production','technology_material','technology_name','wood_name','process_mark','table_body_mark','out_crt_man']; // 指定要导出的字段 foreach ($data as $key => $value){ $data[$key]['out_order_no_time'] = $value['out_order_no_time'] ? date('Y-m-d',$value['out_order_no_time']) : ''; $data[$key]['production_time'] = $value['production_time'] ? date('Y-m-d',$value['production_time']) : ''; $data[$key]['not_production'] = $value['order_quantity'] - ($production_map[$value['sale_orders_product_id']] ?? 0); unset($data[$key]['sale_orders_product_id']); } return $data; } public function saveExportData($data){ $file_name = time(). '_' . rand(1000,9999); $filename = $file_name.'.' . 'xlsx'; $headers = [ ['生产订单日期','生产订单号','销售订单日期','销售订单编号','客户编码','客户名称','表头备注','产品编码','产品名称','规格型号','计量单位','订单数量','生产数量','未下生产数量','工艺/材质','工艺名称','木皮名称','加工备注','表体备注','制单人']]; $bool = Excel::store(new Exports($data,'production_order',$headers),"/public/productionOrder/{$filename}", null, 'Xlsx', []); return $filename; } }