function($query){ /** @var Builder $query */ $query->select("id","name","deleted_at","created_at","customer_id","user_owner_group_id") ->with(["customer","userOwnerGroup"]); }]; $ownerGroups = app('UserOwnerGroupService')->getSelection(); $customers = app('CustomerService')->getSelection(); $owners = app('OwnerService')->getIntersectPermitting(); $reports = app("OwnerReportService")->paginate($request->input(),$withs); $params = $request->input(); return response()->view('customer.project.report',compact("reports","ownerGroups","customers","owners","params")); } public function projectReportExport(Request $request) { if(!Gate::allows('客户管理-项目-报表')){ return redirect('denied'); } /** @var OwnerReportService $service */ $service = app('OwnerReportService'); $withs = ["ownerBillReport","owner"=>function($query){ /** @var Builder $query */ $query->select("id","name","deleted_at","created_at","customer_id","user_owner_group_id") ->with(["customer","userOwnerGroup"]); }]; if ($request->checkAllSign ?? false){ $params = $request->input(); unset($params['checkAllSign']); $reports = $service->get($params,$withs); }else $reports = $service->get(["id"=>$request->data ?? ''],$withs); $column = ["项目小组","客户","子项目","状态","创建日期","在库时长","结算月","日均单量","结算月上月盘点面积","结算月盘点面积","初始账单金额","确认账单金额","确认日期"]; $list = []; foreach ($reports as $report){ $list[] = [ $report->owner ? ($report->owner->userOwnerGroup ? $report->owner->userOwnerGroup->name : '') : '', $report->owner ? ($report->owner->customer ? $report->owner->customer->name : '') : '', $report->owner ? $report->owner->name : '', $report->owner ? ($report->owner->deleted_at ? "冻结" : "激活") : '', $report->owner ? (string)$report->owner->created_at : '', $report->owner ? ($report->owner->created_at ? ((new \DateTime())->diff(new \DateTime($report->owner->created_at))->days)." 天" : '') : '', $report->counting_month, $report->daily_average_order_amount, $report->last_month_counting_area, $report->current_month_counting_area, $report->ownerBillReport ? $report->ownerBillReport->initial_fee : '', $report->ownerBillReport ? $report->ownerBillReport->confirm_fee : '', $report->ownerBillReport ? (string)$report->ownerBillReport->updated_at : '', ]; } $post = Http::post(config('go.export.url'),['type'=>'base','data'=>json_encode(["row"=>$column,"list"=>$list],JSON_UNESCAPED_UNICODE)]); if ($post->status() == 500){ throw new Exception($post->header("Msg")); } return response($post,200, [ "Content-type"=>"application/octet-stream", "Content-Disposition"=>"attachment; filename=客户项目报表-".date('ymdHis').'.xlsx', ]); } public function projectIndex() { if(!Gate::allows('客户管理-项目-查询')){ return redirect('denied'); } /** @var OwnerService $service */ $service = app('OwnerService'); $owners = $service->paginate(request()->input(),['customer',"userOwnerGroup","userWorkGroup", "ownerStoragePriceModels",/*"ownerAreaReport"=>function($query){ $month = date('Y-m'); /** @var Builder $query */ /*$query->where("counting_month","like",$month."%"); }*/]); $models = app('OwnerService')->getIntersectPermitting(); $customers = app('CustomerService')->getSelection(); $ownerGroups = app('UserOwnerGroupService')->getSelection(); $params = request()->input(); return response()->view('customer.project.index',compact("owners","models","customers","ownerGroups","params")); } public function projectIndexExport(Request $request) { if(!Gate::allows('客户管理-项目-查询')){ return redirect('denied'); } /** @var OwnerService $service */ $service = app('OwnerService'); $withs = ['customer',"userOwnerGroup","contracts","ownerStoragePriceModels","ownerAreaReport"=>function($query){ $month = date('Y-m'); /** @var Builder $query */ $query->where("counting_month","like",$month."%"); }]; $params = $request->input(); $params['customer_id']=true; if ($request->checkAllSign ?? false) unset($params['checkAllSign']); else $params = ["id"=>$request->data ?? '']; $owners = $service->get($params,$withs); $column = ["客户","税率","项目","货主代码","创建日期","合同号","销售名称","公司全称","联系人","联系电话","项目小组","用仓类型","当月结算面积","月单量预警","是否激活","项目描述"]; $list = []; foreach ($owners as $owner){ $list[] = [ $owner->customer ? $owner->customer->name : '', $owner->tax_rate, $owner->name, $owner->code, $owner->created_at, implode("\r\n",array_column($owner->contracts,"contract_number")), implode("\r\n",array_column($owner->contracts,"salesman")), $owner->customer ? $owner->customer->company_name : '', $owner->linkman, $owner->phone_number, $owner->userOwnerGroup ? $owner->userOwnerGroup->name : '', implode(",",array_unique(array_column(($owner->ownerStoragePriceModels)->toArray(),"using_type"))), $owner->ownerAreaReport ? $owner->ownerAreaReport->accounting_area : '', $owner->waring_line_on, $owner->deleted_at ? '否' : '是', $owner->description ]; } $post = Http::post(config('go.export.url'),['type'=>'base','data'=>json_encode(["row"=>$column,"list"=>$list],JSON_UNESCAPED_UNICODE)]); if ($post->status() == 500){ throw new Exception($post->header("Msg")); } return response($post,200, [ "Content-type"=>"application/octet-stream", "Content-Disposition"=>"attachment; filename=客户报表-".date('ymdHis').'.xlsx', ]); } public function projectCreate() { if(!Gate::allows('客户管理-项目-录入')){ return redirect('denied'); } $customers = app('CustomerService')->getSelection(); $ownerGroups = app('UserOwnerGroupService')->getSelection(); $userGroups = app('UserWorkgroupService')->getSelection(["id","name","warehouse_id"]); $warehouses = app('WarehouseService')->getSelection(); $owner = null; return response()->view('customer.project.create',compact("customers","ownerGroups","owner","warehouses","userGroups")); } public function projectUpdate() { $this->gate("客户管理-项目-录入"); if (!request("id"))$this->error("项目不存在,无法补充详细信息"); $errors = $this->validator(request()->input())->errors(); if (count($errors)>0)$this->success(["errors"=>$errors]); /** @var Owner $owner */ $owner = app('OwnerService')->find(request("id")); if (!$owner)$this->error("项目已被删除,无法操作"); $owner = app('OwnerService')->update($owner,[ "customer_id" => request("customer_id"), "warehouse_id" => request("warehouse_id"), "tax_rate" => request("tax_rate"), "linkman" => request("linkman"), "phone_number" => request("phone_number"), "user_owner_group_id" => request("owner_group_id"), "user_workgroup_id" => request("user_workgroup_id"), "waring_line_on" => request("waring_line_on"), "description" => request("description"), ]); $this->success($owner); } //获取货主下所有相关计费模型 public function getOwnerPriceModel(Request $request) { $owner = new Owner(); $owner->id = $request->id; $owner->load(["ownerPriceOperations","ownerPriceExpresses","ownerPriceLogistics","ownerPriceDirectLogistics"]); return ["success"=>true,"data"=>["ownerPriceOperations"=>$owner->ownerPriceOperations, "ownerPriceExpresses"=>$owner->ownerPriceExpresses, "ownerPriceLogistics"=>$owner->ownerPriceLogistics, "ownerPriceDirectLogistics"=>$owner->ownerPriceDirectLogistics]]; } public function projectEdit($id) { if(!Gate::allows('客户管理-项目-编辑')){ return redirect('denied'); } /** @var Owner $owner */ $owner = app('OwnerService')->find($id); $owner->loadCount(["ownerStoragePriceModels","ownerPriceOperations","ownerPriceExpresses","ownerPriceLogistics","ownerPriceDirectLogistics"]); $isExist = false; /** @var \stdClass $owner */ if($owner->owner_storage_price_models_count || $owner->owner_price_operations_count || $owner->owner_price_expresses_count || $owner->owner_price_logistics_count || $owner->owner_price_direct_logistics_count) $isExist = true; $customers = app('CustomerService')->getSelection(); $ownerGroups = app('UserOwnerGroupService')->getSelection(); $userGroups = app('UserWorkgroupService')->getSelection(["id","name","warehouse_id"]); $warehouses = app('WarehouseService')->getSelection(); $type = request("type"); return response()->view('customer.project.create',compact("customers","ownerGroups","warehouses",'owner',"isExist", "type","userGroups")); } public function projectArea(Request $request) { if(!Gate::allows('客户管理-项目-面积')){ return redirect('denied'); } $areas = app('OwnerAreaReportService')->paginate($request->input(),["owner"=>function($query){$query->with(["customer","ownerStoragePriceModels.unit"]);}]); $ownerGroups = app('UserOwnerGroupService')->getSelection(); $customers = app('CustomerService')->getSelection(); $owners = app('OwnerService')->getIntersectPermitting(); $params = $request->input(); return response()->view('customer.project.area',compact("areas","ownerGroups","customers","owners","params")); } public function updateArea(Request $request) { if(!Gate::allows('客户管理-项目-面积-编辑')){ return ["success"=>false,'data'=>"无权操作!"]; } if (!($request->id ?? false) || !($request->area ?? false)) return ["success"=>false,'data'=>"传递错误!"]; if (!request("accounting_area")){ $values = $request->area ?? null; if (!$values)return ["success"=>true,"data"=>$values]; foreach ($values as $column=>$value){ if ($value && (!is_numeric($value) || $value<0))return ["success"=>false,'data'=>$column."非数字或小于0!"]; } $accounting_area = ((int)$values["area_on_tray"]*2.5) + ((int)$values["area_on_half_tray"]*1.8) + ((int)$values["area_on_flat"]*1.3); $values["accounting_area"] = $accounting_area; }else $values = ["accounting_area"=>request("accounting_area")]; $row = app('OwnerAreaReportService')->update(["id"=>$request->id],$values); if ($row==1){ LogService::log(__METHOD__,"客户管理-修改面积",json_encode($request->input())); return ["success"=>true,"data"=>$values]; } return ["success"=>false,"data"=>"影响了".$row."条数据!"]; } public function projectAreaExport(Request $request) { if(!Gate::allows('客户管理-项目-面积')){ return redirect('denied'); } $params = $request->input(); if ($request->checkAllSign)unset($params['checkAllSign']); else $params = ["id"=>$request->data]; /** @var OwnerAreaReportService $serves */ $serves = app('OwnerAreaReportService'); $areas = $serves->get($params,["owner"=>function($query){$query->with(["customer","ownerStoragePriceModels","userOwnerGroup"]);}]); $column = ["状态","项目组","客户","子项目","结算月","录入时间","用仓类型","货物整托","货物半托","平面区面积","结算面积"]; $list = []; foreach ($areas as $area){ $list[] = [ $area->status, $area->owner ? ($area->owner->userOwnerGroup ? $area->owner->userOwnerGroup->name : '') : '', $area->owner ? ($area->owner->customer ? $area->owner->customer->name : '') : '', $area->owner ? $area->owner->name : '', $area->counting_month, $area->updated_at, $area->owner ? implode(",",array_unique(array_column(($area->owner->ownerStoragePriceModels)->toArray(),"using_type"))) : '', $area->area_on_tray, $area->area_on_half_tray, $area->area_on_flat, $area->accounting_area, ]; } $post = Http::post(config('go.export.url'),['type'=>'base','data'=>json_encode(["row"=>$column,"list"=>$list],JSON_UNESCAPED_UNICODE)]); if ($post->status() == 500){ throw new Exception($post->header("Msg")); } return response($post,200, [ "Content-type"=>"application/octet-stream", "Content-Disposition"=>"attachment; filename=项目面积报表-".date('ymdHis').'.xlsx', ]); } public function financeInstantBill(Request $request) { if(!Gate::allows('结算管理-即时账单')){ return redirect('denied'); } $params = $request->input(); $shops = app('ShopService')->getSelection(); $customers = app('CustomerService')->getSelection(); $owners = app('OwnerService')->getIntersectPermitting(); $details = app('OwnerFeeDetailService')->paginate($params,["owner.customer","shop","processMethod","logistic","items"]); return response()->view('finance.instantBill',compact("details","params","shops","customers","owners")); } public function financeInstantBillExport(Request $request) { if(!Gate::allows('结算管理-即时账单')){ return redirect('denied'); } $params = $request->input(); if ($request->checkAllSign)unset($params['checkAllSign']); else $params = ["id"=>$request->data]; $sql = app('OwnerFeeDetailService')->getSql($params); $row = ["客户", "项目", "作业时间", "类型","店铺", "单号(发/收/退/提)", "收件人", "收件人电话", "商品数量", "物流/快递单号", "体积", "重量", "承运商", "操作费", "物流费", "合计"]; $column = ["customer_name", "owner_name", "worked_at", "type","shop_name", "operation_bill", "consignee_name", "consignee_phone", "commodity_amount", "logistic_bill", "volume", "weight", "logistic_name", "work_fee", "logistic_fee", "total"]; $rule = ["work_fee"=>"mysqlDate"]; $post = Http::post(config('go.export.url'),['type'=>'unify','sql'=>$sql, 'connection'=>'mysql', 'row'=>json_encode($row,JSON_UNESCAPED_UNICODE), 'column'=>json_encode($column), 'rule'=>json_encode($rule)]); if ($post->status() == 500){ throw new Exception($post->header("Msg")); } return response($post,200, [ "Content-type"=>"application/octet-stream", "Content-Disposition"=>"attachment; filename=即时账单记录-".date('ymdHis').'.xlsx', ]); } public function financeBillConfirmation(Request $request) { if(!Gate::allows('结算管理-账单确认')){ return redirect('denied'); } $params = $request->input(); $ownerGroups = app('UserOwnerGroupService')->getSelection(); $customers = app('CustomerService')->getSelection(); $owners = app('OwnerService')->getIntersectPermitting(); $bills = app('OwnerBillReportService')->paginate($params,["owner"=>function($query){ /** @var Builder $query */ $query->with(["customer","userOwnerGroup"]); }]); return response()->view('finance.billConfirmation',compact("params","owners","ownerGroups","customers","bills")); } public function financeBillConfirmationExport(Request $request) { if(!Gate::allows('结算管理-账单确认')){ return redirect('denied'); } $params = $request->input(); if ($request->checkAllSign)unset($params['checkAllSign']); else $params = ["id"=>$request->data]; /** @var OwnerBillReportService $serves */ $serves = app('OwnerBillReportService'); $bills = $serves->get($params,["owner"=>function($query){ /** @var Builder $query */ $query->with(["customer","userOwnerGroup"]); }]); $column = ["项目小组","客户","子项目","结算月","录入日期","原始账单金额","确认账单金额","差额","状态"]; $list = []; foreach ($bills as $bill){ $list[] = [ $bill->owner ? ($bill->owner->userOwnerGroup ? $bill->owner->userOwnerGroup->name : '') : '', $bill->owner ? ($bill->owner->customer ? $bill->owner->customer->name : '') : '', $bill->owner ? $bill->owner->name : '', $bill->counting_month, $bill->updated_at, $bill->initial_fee, $bill->confirm_fee, $bill->difference, $bill->confirmed == '是' ? '已确认' : '未确认', ]; } $post = Http::post(config('go.export.url'),['type'=>'base','data'=>json_encode(["row"=>$column,"list"=>$list],JSON_UNESCAPED_UNICODE)]); if ($post->status() == 500){ throw new Exception($post->header("Msg")); } return response($post,200, [ "Content-type"=>"application/octet-stream", "Content-Disposition"=>"attachment; filename=客户账单报表-".date('ymdHis').'.xlsx', ]); } public function updateBillReport(Request $request) { if(!Gate::allows('结算管理-账单确认-编辑')){ return ["success"=>false,'data'=>"无权操作!"]; } if (!$request->confirm_fee || !is_numeric($request->confirm_fee) || $request->confirm_fee<0)return ["success"=>false,"data"=>"非法金额参数"]; $date = date('Y-m-d H:i:s'); app('OwnerBillReportService')->update(["id"=>$request->id],["confirm_fee"=>$request->confirm_fee,"difference"=>DB::raw($request->confirm_fee.'- initial_fee'),"updated_at"=>$date]); LogService::log(__METHOD__,"客户管理-修改账单报表",json_encode($request->input())); return ["success"=>true,"data"=>$date]; } public function billConfirm(Request $request) { if(!Gate::allows('结算管理-账单确认-完结')){ return ["success"=>false,'data'=>"无权操作!"]; } if (!($request->id ?? false))return["success"=>false,"data"=>"非法参数"]; app('OwnerBillReportService')->update(["id"=>$request->id],["confirmed"=>"是"]); LogService::log(__METHOD__,"客户管理-确认账单",json_encode($request->input())); $bill = app('OwnerBillReportService')->first(["id"=>$request->id,"confirmed"=>"是"]); app('OwnerAreaReportService')->lockArea(null, $bill->owner_id, $bill->counting_month); LogService::log(__METHOD__,"客户管理-锁定账单的所有面积",json_encode($bill,JSON_UNESCAPED_UNICODE)); return ["success"=>true]; } private function validator(array $params){ $validator=Validator::make($params,[ 'id' => ['required'], 'customer_id'=>['required'], 'owner_group_id'=>['required'], 'warehouse_id'=>['required'], 'tax_rate' => ["nullable",'numeric'], 'waring_line_on' => ["nullable",'integer'], ],[ 'required'=>':attribute 为必填项', 'integer'=>':attribute 必须为整数', 'numeric'=>':attribute 必须为数字', ],[ 'code'=>'项目代码', 'name'=>'项目名称', 'warehouse_id'=>'仓库', 'customer_id'=>'客户', 'owner_group_id'=>'工作组', 'tax_rate' => '税率', 'waring_line_on' => '月单量预警' ]); return $validator; } public function verifyProject(Request $request) { $this->success($this->validator($request->input())->errors()); } public function createReport() { $ids = \request("val"); if (!$ids)$this->error("未选择任何项目"); $reports = OwnerReport::query()->with("owner") ->where("counting_month",">=",date("Y-m")."-01") ->whereIn("owner_id",$ids)->get(["id","owner_id"]); $errors = []; $exist = []; foreach ($reports as $report){ $errors[] = "“".($report->owner ? $report->owner->name : $report->owner_id)."”已存在本月报表"; $exist[] = $report->owner_id; } $ids = array_diff($ids,$exist); $insert = []; $date = date("Y-m-d H:i:s"); foreach ($ids as $id){ $insert[] = [ "owner_id" => $id, "counting_month" => date("Y-m-d"), "created_at" => $date ]; } if ($insert){ OwnerReport::query()->insert($insert); LogService::log(__METHOD__,"手动生成报表",json_encode($insert)); } $reports = OwnerReport::query()->with(["owner.userOwnerGroup","owner.customer"]) ->where("counting_month",">=",date("Y-m")."-01") ->whereIn("owner_id",$ids)->get(); $result = []; foreach ($reports as $report){ $result[] = [ "id" => $report->id, "ownerGroupName" => $report->owner ? ($report->owner->userOwnerGroup ? $report->owner->userOwnerGroup->name : '') : '', "customerName" => $report->owner ? ($report->owner->customer ? $report->owner->customer->name : '') : '', "ownerName" => $report->owner ? $report->owner->name : '', "ownerStatus" => $report->owner ? ($report->owner->deleted_at ? "冻结" : "激活") : '', "ownerStorageDuration" => $report->owner ? ($report->owner->created_at ? ((new \DateTime())->diff(new \DateTime($report->owner->created_at))->days) : '') : '', "ownerCreatedAt" => $report->owner ? $report->owner->created_at : '', "countingMonth" => $report->counting_month, ]; } $this->success(["errors"=>$errors,"data"=>$result]); } public function createAreaReport() { $ids = \request("val"); if (!$ids)$this->error("未选择任何项目"); /** @var OwnerService $service */ $service = app("OwnerService"); $owners = $service->get(["id"=>$ids],["ownerStoragePriceModels"],false,true); app("OwnerAreaReportService")->notExistToInsert($owners); $reports = OwnerAreaReport::query()->with(["owner","ownerStoragePriceModel.unit"]) ->where("counting_month",">=",date("Y-m")."-01") ->whereIn("owner_id",array_column($owners->toArray(),"id"))->get(); $result = []; foreach ($reports as $report){ $result[] = [ "id" => $report->id, "ownerGroupId" => $report->user_owner_group_id, "ownerName" => $report->owner ? $report->owner->name : '', "customerName" => $report->owner ? ($report->owner->customer ? $report->owner->customer->name : '') : '', "countingMonth" => $report->counting_month, "areaOnTray" => $report->area_on_tray, "areaOnHalfTray" => $report->area_on_half_tray, "areaOnFlat" => $report->area_on_flat, "accountingArea" => $report->accounting_area, "status" => $report->status, "updatedAt" => $report->updated_at, "unitName" => $report->ownerStoragePriceModel ? ($report->ownerStoragePriceModel->unit ? $report->ownerStoragePriceModel->unit->name : '') : '', "ownerStoragePriceModel"=> $report->ownerStoragePriceModel ? $report->ownerStoragePriceModel->using_type : '' , ]; } $this->success($result); } public function resetInstantBill() { $startData = request("startDate"); $endDate = request("endDate"); $owner = request("owner"); if (!$startData)$this->error("非法参数"); $details = OwnerFeeDetail::query()->where("worked_at",">=",$startData." 00:00:00"); if ($endDate)$details->where("worked_at","<=",$endDate." 23:59:59"); if (count($owner)>0)$details->whereIn("owner_id",$owner); $details->get()->each(function ($detail){ $this->dispatch(new ResetInstantBill($detail)); }); $this->success(); } public function resetBillConfirmation() { $month = request("month"); $owner = request("owner"); $sql = <<0){ $sql.=" AND owner_id IN (''"; foreach ($owner as $o)$sql .=",'{$o}'"; $sql.=")"; } $sql .= " AND ((type = '发货' AND logistic_fee IS NOT NULL AND work_fee IS NOT NULL) OR (type <> '发货' AND work_fee IS NOT NULL)) GROUP BY owner_id"; $billDetails = DB::select(DB::raw($sql),[$month."%"]); $areas = OwnerAreaReport::query()->with("ownerStoragePriceModel")->where("counting_month","like",$month."%")->get(); $map = []; foreach($areas as $area){ if (isset($map[$area->owner_id."_".$area->counting_month])){ if (!$area->ownerStoragePriceModel)continue; $map[$area->owner_id."_".$area->counting_month] += app('OwnerStoragePriceModelService') ->calculationAmount($area->ownerStoragePriceModel,$area->accounting_area,$area->owner_id,$area->counting_month); }else{ if (!$area->ownerStoragePriceModel)continue; $map[$area->owner_id."_".$area->counting_month] = app('OwnerStoragePriceModelService') ->calculationAmount($area->ownerStoragePriceModel,$area->accounting_area,$area->owner_id,$area->counting_month); } } $chunks = array_chunk($billDetails,50); foreach ($chunks as $bills){ foreach ($bills as $bill){ $key = $bill->owner_id."_".$month; $total = $bill->total; if (isset($map[$key]))$total += $map[$key]; OwnerBillReport::query()->where("owner_id",$bill->owner_id) ->where("counting_month",$month."-01") ->update(["initial_fee"=>$total]); } } $this->success(); } }