public function edit(Request $request) { $user = \Session::get('user'); if (!$user->can('项目信息管理')) { abort(401); } $project = Project::find($request->input('id')); $product = SubProduct::find($request->input('product_id')); $old_attributes = $project->attributesToArray(); $project->ref_no = $request->input('ref_no'); // 项目编号 $project->name = $request->input('name'); // 项目名称 $project->vip = (bool) ($request->input('vip') == 'on'); $project->signed_status = $request->input('signed_status'); $old_product_id = $project->product->id; $new_product_id = $product->id; $project->product()->associate($product); // 产品类型 $project->contact_user = $request->input('contact_user'); // 联系人 $project->contact_phone = $request->input('contact_phone'); // 联系电话 $project->contact_email = $request->input('contact_email'); // 联系邮箱 $project->login_url = $request->input('login_url'); //登录地址 $signed_time = $request->input('signed_time'); if (!$signed_time) { $signed_time = null; } else { $signed_time = \Carbon\Carbon::createFromFormat('Y/m/d', $signed_time)->format('Y-m-d H:i:s'); } // 签约时间 $project->signed_time = $signed_time; # 实际验收时间 $check_time = $request->input('check_time'); if (!$check_time) { $check_time = null; } else { $check_time = \Carbon\Carbon::createFromFormat('Y/m/d', $check_time)->format('Y-m-d H:i:s'); } $project->check_time = $check_time; # 维保时间 $project->service_unit = $request->input('service_unit'); $project->service_value = $request->input('service_value'); $project->seller = $request->input('seller'); // 销售人员 $project->engineer = $request->input('engineer'); // 工程师 $project->description = $request->input('description'); // $project->deploy_address = $request->input('deploy_address'); $project->way = $request->input('way'); $project->updateServiceEndTime(); if ($project->save()) { //修改了签约类型 if ($old_product_id != $new_product_id) { //清空所有的 module, 重新关联 module $connected_modules = $project->modules()->lists('id')->all(); if (count($connected_modules)) { $project->modules()->detach($connected_modules); } foreach ($product->modules as $module) { $project->modules()->save($module); } //清空所有的 params, 重新关联 params $connected_params = $project->params()->lists('id')->all(); if (count($connected_params)) { $project->params()->detach($connected_params); } foreach ($product->params as $param) { $project->params()->save($param, ['value' => $param->pivot->value]); } } $new_attributes = $project->attributesToArray(); $change = []; $helper = ['ref_no' => '项目编号', 'name' => '项目名称', 'product_id' => '产品类型(编号显示)', 'contact_user' => '联系人', 'contact_phone' => '联系人电话', 'contact_email' => '联系人邮箱', 'engineer' => '工程师负责人', 'deploy_address' => '客户地址', 'seller' => '销售负责人', 'description' => '备注', 'way' => '乘车路线', 'signed_time' => '签约时间', 'check_time' => '实际验收时间', 'vip' => '重点项目状态', 'signed_status' => '正式/试用/售前支持状态', 'login_url' => '登录地址', 'service_unit' => '维保时长', 'service_value' => '维保时长']; foreach (array_diff_assoc($old_attributes, $new_attributes) as $key => $value) { $old_value = $old_attributes[$key]; $new_value = $new_attributes[$key]; switch ($key) { case 'vip': $new_value = $new_value === true ? '重点项目' : '普通项目'; $old_value = $old_value === true ? '重点项目' : '普通项目'; break; case 'signed_status': $new_value = \App\Project::$signed_status[$new_value]; $old_value = \App\Project::$signed_status[$old_value]; break; case 'signed_time': if ($new_value) { $new_value = $new_value->format('Y/m/d'); } if ($old_value) { $old_value = $old_value->format('Y/m/d'); } //时间需要特殊处理 if ($old_value == $new_value) { continue 2; } break; case 'check_time': if ($new_value) { $new_value = $new_value->format('Y/m/d'); } if ($old_value) { $old_value = $old_value->format('Y/m/d'); } //时间需要特殊处理 if ($old_value == $new_value) { continue 2; } break; case 'service_unit': case 'service_value': $old_value = $old_attributes['service_value'] . \App\Project::$service_units[$old_attributes['service_unit']]; $new_value = $new_attributes['service_value'] . \App\Project::$service_units[$new_attributes['service_unit']]; $key = 'service_value'; break; case 'service_end_time': break 2; default: if ($old_value === null) { $old_value = '空'; } if ($new_value === null) { $new_value = '空'; } } $change[$key] = ['old' => $old_value, 'new' => $new_value, 'title' => $helper[$key]]; } if (count($change)) { Clog::add($project, '修改基本信息', $change); foreach ($change as $c) { \Log::notice(strtr('项目基本信息修改: 用户(%name[%id]) 修改了项目 (%project_name[%project_id] 的基本信息(%title) %old_value -> %new_value', ['%name' => $user->name, '%id' => $user->id, '%project_name' => $project->name, '%project_id' => $project->id, '%title' => $c['title'], '%old_value' => $c['old'], '%new_value' => $c['new']])); } } return redirect(route('project.profile', ['id' => $project->id]))->with('message_content', '修改成功!')->with('message_type', 'info'); } }
public function param_edit($id, Request $request) { $user = \Session::get('user'); if (!$user->can('产品参数管理')) { abort(401); } $sub = SubProduct::find($id); $param_id = $request->input('param_id'); $param = \App\Param::find($param_id); //获取子产品中该参数关联的 value $old_value = $sub->params()->where('param_id', $param->id)->first()->pivot->value; if ($request->input('reset') == 'on') { //获取到 product 中该参数的 value $value = $sub->product->params()->where('id', $param->id)->first()->value; $param->value = $value; $sub->params()->detach($param_id); $sub->params()->save($param, ['value' => $value]); \Log::notice(strtr('子产品参数修改: 用户 (%name[%id]) 修改了子产品 (%product_name[%product_id] 的参数(%param_name[%param_id]) : %old_value -> %new_value', ['%name' => $user->name, '%id' => $user->id, '%product_name' => $sub->name, '%product_id' => $sub->id, '%param_name' => $param->name, '%param_id' => $param->id, '%old_value' => $old_value, '%new_value' => $value])); } else { $value = $request->input('value'); $sub->params()->updateExistingPivot($param_id, ['value' => $value, 'manual' => true]); \Log::notice(strtr('子产品参数修改: 用户 (%name[%id]) 修改了子产品 (%product_name[%product_id] 的参数(%param_name[%param_id]) : %old_value -> %new_value', ['%name' => $user->name, '%id' => $user->id, '%product_name' => $sub->name, '%product_id' => $sub->id, '%param_name' => $param->name, '%param_id' => $param->id, '%old_value' => $old_value, '%new_value' => $value])); } foreach ($sub->projects as $project) { $project_param = $project->params()->where('param_id', $param->id)->first(); $old_value = $project_param->pivot->value; //如果为默认 (不为手动), 则修改 if (!$project_param->pivot->manual) { $project->params()->updateExistingPivot($param->id, ['value' => $value]); \Log::notice(strtr('项目参数修改: 用户 (%name[%id]) 修改了项目 (%project_name[%project_id] 的参数(%param_name[%param_id]) : %old_value -> %new_value', ['%name' => $user->name, '%id' => $user->id, '%project_name' => $project->name, '%project_id' => $project->id, '%param_name' => $param->name, '%param_id' => $param->id, '%old_value' => $old_value, '%new_value' => $value])); } } return redirect()->back()->with('message_content', '参数修改成功!')->with('message_type', 'info'); }
public function del_sp(Request $request) { $id = $request->input('id'); $sp = SubProduct::find($id); $sp->delete(); }