productCategoryRule($data,$user,false); if(!$status) return [$status,$msg]; $update = $msg['data'][0]; $model = new ProductCategory(); $model->where('id',$data['id'])->update($update); return [true,'']; } /** * 产品分类新增 * @param $data * @param $user * @return array */ public function productCategoryAdd($data,$user){ list($status,$msg) = $this->productCategoryRule($data,$user); if(!$status) return [$status,$msg]; ProductCategory::insert($msg['data']); return [true,'']; } /** * 产品分类删除 * @param $data * @return array */ public function productCategoryDel($data){ if($this->isEmpty($data,'id')) return [false,'请选择数据!']; $bool = Product::where('del_time',0) ->where('product_category_id',$data['id']) ->exists(); if($bool) return [false,'产品分类下已添加产品,操作失败']; try { DB::beginTransaction(); ProductCategory::where('id',$data['id'])->update([ 'del_time' => time() ]); DB::commit(); }catch (\Exception $exception){ DB::rollBack(); return [false,$exception->getMessage()]; } return [true,'']; } /** * 产品分类列表 * @param $data * @param $user * @return array */ public function productCategoryList($data,$user){ $model = ProductCategory::leftJoin('product_category_orderby as a', function ($join) { $join->on('product_category.id', '=', 'a.category_id') ->where('a.del_time',0); }) ->where('product_category.del_time',0) ->select('product_category.title','product_category.id','product_category.parent_id','product_category.is_edit_unit_price') ->orderByRaw('IF(a.sort IS NULL, product_category.id, a.sort) ASC'); // 排序逻辑:优先按 a.sort 排序,没有关联时按主表 id 排序 if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%'); if(isset($data['is_edit_unit_price'])) $model->where('is_edit_unit_price', $data['is_edit_unit_price']); $list = $model->get()->toArray(); foreach ($list as $key => $value){ $list[$key]['is_edit_unit_price_title'] = ProductCategory::$is_edit_unit_price[$value['is_edit_unit_price']] ?? ""; } $list_tree = $list; if(! empty($list_tree)) { $list_tree = $this->makeTree(0,$list_tree); $list_tree = $this->set_sort_circle($list_tree); } return [200, ['data' => $list,'tree' => $list_tree]]; } /** * 产品分类参数规则 * @param $data * @param $is_add * @return array */ public function productCategoryRule($data,$user, $is_add = true){ if($this->isEmpty($data,'data')) return [false,'数据不能为空!']; $title = array_column($data['data'],'title'); $title = array_map(function($val) { return $val !== null ? $val : 0; }, $title); $title_count = array_count_values($title); foreach ($title as $value){ if(empty($value)) return [false,'名称不能为空!']; if($title_count[$value] > 1) return [false,'名称不能重复']; } foreach ($data['data'] as $key => $value){ $data['data'][$key]['upd_time'] = time(); if($is_add){ $parent_id = 0; if(! empty($value['parent_id'])) { $bool = Product::where('del_time',0) ->where('product_category_id',$value['parent_id']) ->exists(); if($bool) { $title = ProductCategory::where('id',$value['parent_id'])->select('title')->value('title'); return [false,'产品分类:'. $title .'下已添加产品,不允许添加子分类']; } $parent_id = $value['parent_id']; } $data['data'][$key]['parent_id'] = $parent_id; $data['data'][$key]['crt_time'] = time(); $data['data'][$key]['depart_id'] = $data['depart_id']; $data['data'][$key]['top_depart_id'] = $data['top_depart_id']; $bool = ProductCategory::where('title',$value['title']) ->where('top_depart_id',$data['top_depart_id']) ->where('del_time',0) ->exists(); }else{ if($this->isEmpty($data,'id')) return [false,'id不能为空!']; $top_depart_id = ProductCategory::where('id',$data['id'])->value('top_depart_id'); $bool = ProductCategory::where('title',$value['title']) ->where('id','<>',$data['id']) ->where('del_time',0) ->exists(); } if($bool) return [false,'分类名称不能重复']; } return [true, $data]; } /** * 产品编辑 * @param $data * @param $user * @return array */ public function productEdit($data,$user){ list($status,$msg) = $this->productRule($data, $user, false); if(!$status) return [$status,$msg]; try { DB::beginTransaction(); $model = Product::where('id',$data['id'])->first(); $model->category = $data['category'] ?? ''; $model->title = $data['title']; $model->code = $data['code'] ?? ''; $model->size = $data['size'] ?? ''; $model->unit = $data['unit'] ?? 0; $model->business_cost = $data['business_cost'] ?? 0; $model->cost = $data['cost'] ?? 0; $model->write_off_price = $data['write_off_price'] ?? 0; $model->major_client_settlement_price = $data['major_client_settlement_price'] ?? 0; $model->return_change_price = $data['return_change_price'] ?? 0; $model->freight_price = $data['freight_price'] ?? 0; $model->mark = $data['mark'] ?? ''; $model->save(); DB::commit(); }catch (\Exception $exception){ DB::rollBack(); return [false,$exception->getMessage()]; } return [true, '']; } /** * 产品新增 * @param $data * @param $user * @return array */ public function productAdd($data,$user){ list($status,$msg) = $this->productRule($data, $user); if(!$status) return [$status,$msg]; try { DB::beginTransaction(); $model = new Product(); $model->category = $data['category'] ?? ''; $model->title = $data['title']; $model->code = $data['code'] ?? ''; $model->size = $data['size'] ?? ''; $model->unit = $data['unit'] ?? 0; $model->business_cost = $data['business_cost'] ?? 0; $model->cost = $data['cost'] ?? 0; $model->write_off_price = $data['write_off_price'] ?? 0; $model->major_client_settlement_price = $data['major_client_settlement_price'] ?? 0; $model->return_change_price = $data['return_change_price'] ?? 0; $model->freight_price = $data['freight_price'] ?? 0; $model->mark = $data['mark'] ?? ''; $model->crt_id = $user['id']; $model->save(); DB::commit(); }catch (\Exception $exception){ DB::rollBack(); return [false,$exception->getMessage()]; } return [true, '']; } /** * 产品删除 * @param $data * @return array */ public function productDel($data){ if($this->isEmpty($data,'id')) return [false,'请选择数据!']; try { DB::beginTransaction(); $time = time(); Product::where('del_time',0)->where('id',$data['id'])->update(['del_time' => $time]); DB::commit(); }catch (\Exception $exception){ DB::rollBack(); return [false,$exception->getMessage()]; } return [true, '']; } /** * 产品详情 * @param $data * @param $user * @return array */ public function productDetail($data,$user){ if($this->isEmpty($data,'id')) return [false,'请选择数据!']; $customer = Product::where('del_time',0) ->where('id',$data['id']) ->first(); if(empty($customer)) return [false,'产品不存在或已被删除']; $customer = $customer->toArray(); $customer['crt_name'] = Employee::where('id',$customer['crt_id'])->value('emp_name'); $customer['crt_time'] = $customer['crt_time'] ? date("Y-m-d H:i:s",$customer['crt_time']): ''; return [true, $customer]; } public function productCommon($data,$user, $field = []){ if(empty($field)) $field = Product::$field; $model = Product::where('del_time',0) ->select($field) ->orderby('code', 'asc'); if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%'); if(! empty($data['category'])) $model->where('code', 'LIKE', '%'.$data['category'].'%'); if(! empty($data['size'])) $model->where('size', 'LIKE', '%'.$data['size'].'%'); if(! empty($data['code'])) $model->where('code', 'LIKE', '%'.$data['code'].'%'); if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) { $return = $this->changeDateToTimeStampAboutRange($data['crt_time']); $model->where('crt_time','>=',$return[0]); $model->where('crt_time','<=',$return[1]); } return $model; } /** * 产品列表 * @param $data * @param $user * @return array */ public function productList($data,$user){ $model = $this->productCommon($data, $user); $list = $this->limit($model,'',$data); $list = $this->fillData($list,$user,$data); return [true, $list]; } /** * 产品参数规则 * @param $data * @param $is_add * @return array */ public function productRule(&$data, $user, $is_add = true){ if(empty($data['code'])) return [false,'产品编码不能为空']; if(empty($data['title'])) return [false,'产品名称不能为空']; if(! isset($data['cost'])) return [false, '请填写成本']; if(! isset($data['business_cost'])) return [false, '请填写业务成本']; $res = $this->checkNumber($data['cost']); if(! $res) return [false,'成本请输入不超过两位小数并且大于等于0的数值']; $res = $this->checkNumber($data['business_cost']); if(! $res) return [false,'业务成本请输入不超过两位小数并且大于等于0的数值']; if(! empty($data['write_off_price'])){ $res = $this->checkNumber($data['write_off_price']); if(! $res) return [false,'核销单价请输入不超过两位小数并且大于等于0的数值']; } if(! empty($data['major_client_settlement_price'])){ $res = $this->checkNumber($data['major_client_settlement_price']); if(! $res) return [false,'大客户结算单价请输入不超过两位小数并且大于等于0的数值']; } if(! empty($data['return_change_price'])){ $res = $this->checkNumber($data['return_change_price']); if(! $res) return [false,'退货损耗单价请输入不超过两位小数并且大于等于0的数值']; } if(! empty($data['freight_price'])){ $res = $this->checkNumber($data['freight_price']); if(! $res) return [false,'运费单价请输入不超过两位小数并且大于等于0的数值']; } if($is_add){ $bool = Product::where('code', $data['code']) ->where('del_time',0) ->exists(); }else{ if(empty($data['id'])) return [false,'ID不能为空']; $bool = Product::where('code', $data['code']) ->where('id','<>',$data['id']) ->where('del_time',0) ->exists(); } if($bool) return [false,'产品编码不能重复']; return [true, $data]; } /** * 拼接数据 * @param $data * @return array */ public function fillData($data, $user, $search){ if(empty($data['data'])) return $data; foreach ($data['data'] as $key => $value){ $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : ''; $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? ''; } return $data; } }