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]; $production_no = $this->setOrderNO(); //工艺 -> 工序 $technology = Technology::where('del_time',0) ->orderBy('id','desc') ->first(); if(empty($technology)) return [false,'工艺不存在!']; $process_arr = explode(',',$technology->process_id); try{ DB::beginTransaction(); //生产数据的源数据 $result = $msg; $boom = $process = []; $time = time(); foreach ($result as $key => $value){ $quantity_tmp = $data['quantity'][$key]; $result[$key]['production_no'] = $production_no; $result[$key]['production_quantity'] = $quantity_tmp; $result[$key]['production_time'] = $time; for ($i = 1; $i <= $quantity_tmp; $i++) { $boom[$value['order_no']][] = [ '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 $v){ $process[$value['order_no']][] = [ '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 ]; } } } OrdersProduct::insert($result); foreach ($boom as $key => $value){ $boom_model = new OrdersProductBom(['channel'=> $key]); $boom_model->insert($value); }unset($boom); foreach ($process as $key => $value){ $process_model = new OrdersProductProcess(['channel' => $key]); $process_model->insert($value); }unset($process); 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 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') ->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 = $this->limit($model,'',$data); $list = $this->fillData($list); return [200,$list]; } public function orderRule($data){ if($this->isEmpty($data,'id')) return [false,'请选择数据!']; if($this->isEmpty($data,'quantity')) return [false,'数量不能为空!']; if(in_array(false, $data['quantity'], true) || in_array(0, $data['quantity'], true) || in_array('', $data['quantity'], true))return [false,'数量不能为空!']; $result = SaleOrdersProduct::whereIn('id',$data['id']) ->select('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')->get()->toArray(); $map = $this->getProductionOrderQuantity($result); foreach ($result as $key => $value){ $keys = $value['order_no'] . $value['technology_name'] . $value['wood_name']; if(isset($map[$keys])){ if($map[$keys] + $data['quantity'][$key] > $value['order_quantity']) return [false,'生产数量不能大于订单数量']; }else{ if($data['quantity'][$key] > $value['order_quantity']) return [false,'生产数量不能大于订单数量']; } } return [true, $result]; } public function fillData($data){ if(empty($data['data'])) return $data; $production_map = $this->getProductionOrderQuantity($data['data']); date_default_timezone_set("PRC"); 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']) : ''; $keys = $value['order_no'] . $value['technology_name'] . $value['wood_name']; $data['data'][$key]['not_production'] = $value['order_quantity'] - ($production_map[$keys] ?? 0); } return $data; } //返回已生产数量 public function getProductionOrderQuantity($data){ if(empty($data)) return []; $args = ''; foreach ($data as $value){ $args .= "(order_no = '{$value['order_no']}' and technology_name = '{$value['technology_name']}' and wood_name = '{$value['wood_name']}') OR "; } $args = rtrim($args,'OR '); $result = OrdersProduct::where('del_time',0) ->whereRaw("($args)") ->select(DB::raw("sum(production_quantity) as production_quantity"),'order_no','technology_name','wood_name') ->groupby('order_no','technology_name','wood_name') ->get()->toArray(); $map = []; foreach ($result as $value){ $map[$value['order_no'] . $value['technology_name'] . $value['wood_name']] = $value['production_quantity']; } return $map; } }