paginate($request->input(),["owners.contracts.files"]); $owners = app("OwnerService")->getIntersectPermitting(['id','name','customer_id']); return response()->view('customer.customer.index',compact("customers","owners")); } /** * Show the form for creating a new resource. * * @return Response */ public function create() { if(!Gate::allows('客户-录入')){ return redirect('denied'); } return response()->view('customer.customer.create'); } /** * Store a newly created resource in storage. * * @param Request $request * @return Response * @throws */ public function store(Request $request) { if(!Gate::allows('客户-录入')){ return redirect('denied'); } $this->validator($request->input())->validate(); app('CustomerService')->create([ "code"=>$request->input("code"), "name"=>$request->input("name"), "company_name"=>$request->input("company_name"), "invoice_address"=>$request->input("invoice_address"), "contact_man"=>$request->input("contact_man"), "phone"=>$request->input("phone"), "remark"=>$request->input("remark"), ]); LogService::log(__METHOD__,"录入客户",json_encode($request->input(),JSON_UNESCAPED_UNICODE)); return response()->redirectTo("customer/customer")->with("successTip","成功创建客户“".$request->input("name")."”"); } /** * Show the form for editing the specified resource. * * @param int $id * @return Response */ public function edit($id) { if(!Gate::allows('客户-编辑')){ return redirect('denied'); } $customer = app('CustomerService')->find($id); return response()->view('customer.customer.create',compact("customer")); } /** * Update the specified resource in storage. * * @param Request $request * @param int $id * @return Response * @throws */ public function update(Request $request, $id) { if(!Gate::allows('客户-编辑')){ return redirect('denied'); } $this->validator($request->input(),$id)->validate(); $result = app('CustomerService')->update(["id"=>$id],[ "code"=>$request->input("code"), "name"=>$request->input("name"), "company_name"=>$request->input("company_name"), "invoice_address"=>$request->input("invoice_address"), "contact_man"=>$request->input("contact_man"), "phone"=>$request->input("phone"), "remark"=>$request->input("remark"), ]); if ($result == 1){ LogService::log(__METHOD__,"修改客户",json_encode($request->input(),JSON_UNESCAPED_UNICODE)); return response()->redirectTo("customer/customer")->with("successTip","成功修改客户“".$request->input("name")."”的信息"); } return response()->view("exception.default",["code"=>"509"]); } /** * Remove the specified resource from storage. * * @param int $id * @return array */ public function destroy($id) { if(!Gate::allows('客户-删除')){ return ["success"=>false,"data"=>"无权操作!"]; } $result = app('CustomerService')->destroy($id); if ($result == 1){ LogService::log(__METHOD__,"删除客户",$id); return ["success"=>true]; } return ["success"=>false,"data"=>"删除了“".$result."”行"]; } private function validator(array $params, $id = null) { return Validator::make($params,[ 'code'=>['required',$id?"unique:customers,code,$id":'unique:customers,code','max:20'], 'name'=>['required','max:20'], ],[ 'required'=>':attribute 为必填项', 'max'=>':attribute 字符过多或输入值过大', 'unique'=>':attribute 已存在', ],[ 'code'=>'客户代码', 'name'=>'客户名称', ]); } public function relatedOwner(Request $request) { $ids = $request->input("ids") ?? []; $id = $request->input("customer_id"); $row = Owner::query()->whereIn("id",$ids)->update(["customer_id"=>$id]); if ($row==count($ids))return ["success"=>true]; return ["success"=>false,"data"=>"修改错误,影响了“".$row."”个项目"]; } }