CustomerController.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Components\AsyncResponse;
  4. use App\Owner;
  5. use App\Services\LogService;
  6. use App\Services\OwnerAreaReportService;
  7. use App\Services\OwnerBillReportService;
  8. use App\Services\OwnerReportService;
  9. use App\Services\OwnerService;
  10. use Exception;
  11. use Illuminate\Database\Eloquent\Builder;
  12. use Illuminate\Http\Request;
  13. use Illuminate\Http\Response;
  14. use Illuminate\Support\Facades\DB;
  15. use Illuminate\Support\Facades\Gate;
  16. use Illuminate\Support\Facades\Http;
  17. use Illuminate\Support\Facades\Validator;
  18. class CustomerController extends Controller
  19. {
  20. use AsyncResponse;
  21. /**
  22. * Display a listing of the resource.
  23. * @param Request $request
  24. * @return Response
  25. */
  26. public function projectReport(Request $request)
  27. {
  28. if(!Gate::allows('客户管理-项目-报表')){ return view('customer.index'); }
  29. $withs = ["ownerBillReport","owner"=>function($query){
  30. /** @var Builder $query */
  31. $query->select("id","name","deleted_at","created_at","customer_id","user_owner_group_id")
  32. ->with(["customer","userOwnerGroup"]);
  33. }];
  34. $ownerGroups = app('UserOwnerGroupService')->getSelection();
  35. $customers = app('CustomerService')->getSelection();
  36. $owners = app('OwnerService')->getIntersectPermitting();
  37. $reports = app("OwnerReportService")->paginate($request->input(),$withs);
  38. $params = $request->input();
  39. return response()->view('customer.project.report',compact("reports","ownerGroups","customers","owners","params"));
  40. }
  41. public function projectReportExport(Request $request)
  42. {
  43. if(!Gate::allows('客户管理-项目-报表')){ return redirect('denied'); }
  44. /** @var OwnerReportService $service */
  45. $service = app('OwnerReportService');
  46. $withs = ["ownerBillReport","owner"=>function($query){
  47. /** @var Builder $query */
  48. $query->select("id","name","deleted_at","created_at","customer_id","user_owner_group_id")
  49. ->with(["customer","userOwnerGroup"]);
  50. }];
  51. if ($request->checkAllSign ?? false){
  52. $params = $request->input();
  53. unset($params['checkAllSign']);
  54. $reports = $service->get($params,$withs);
  55. }else $reports = $service->get(["id"=>$request->data ?? ''],$withs);
  56. $column = ["项目小组","客户","子项目","状态","创建日期","在库时长","结算月","日均单量","结算月上月盘点面积","结算月盘点面积","初始账单金额","确认账单金额","确认日期"];
  57. $list = [];
  58. foreach ($reports as $report){
  59. $list[] = [
  60. $report->owner ? ($report->owner->userOwnerGroup ? $report->owner->userOwnerGroup->name : '') : '',
  61. $report->owner ? ($report->owner->customer ? $report->owner->customer->name : '') : '',
  62. $report->owner ? $report->owner->name : '',
  63. $report->owner ? ($report->owner->deleted_at ? "冻结" : "激活") : '',
  64. $report->owner ? (string)$report->owner->created_at : '',
  65. $report->owner ? ($report->owner->created_at ? ((new \DateTime())->diff(new \DateTime($report->owner->created_at))->days)." 天" : '') : '',
  66. $report->counting_month,
  67. $report->daily_average_order_amount,
  68. $report->last_month_counting_area,
  69. $report->current_month_counting_area,
  70. $report->ownerBillReport ? $report->ownerBillReport->initial_fee : '',
  71. $report->ownerBillReport ? $report->ownerBillReport->confirm_fee : '',
  72. $report->ownerBillReport ? (string)$report->ownerBillReport->updated_at : '',
  73. ];
  74. }
  75. $post = Http::post(config('go.export.url'),['type'=>'base','data'=>json_encode(["row"=>$column,"list"=>$list],JSON_UNESCAPED_UNICODE)]);
  76. if ($post->status() == 500){
  77. throw new Exception($post->header("Msg"));
  78. }
  79. return response($post,200, [
  80. "Content-type"=>"application/octet-stream",
  81. "Content-Disposition"=>"attachment; filename=客户项目报表-".date('ymdHis').'.xlsx',
  82. ]);
  83. }
  84. public function projectIndex()
  85. {
  86. if(!Gate::allows('客户管理-项目-查询')){ return redirect('denied'); }
  87. /** @var OwnerService $service */
  88. $service = app('OwnerService');
  89. $owners = $service->paginate(['customer_id'=>true],['customer',"contracts","userOwnerGroup","ownerStoragePriceModels","ownerAreaReport"=>function($query){
  90. $month = date('Y-m');
  91. /** @var Builder $query */
  92. $query->where("counting_month","like",$month."%");
  93. }]);
  94. return response()->view('customer.project.index',compact("owners"));
  95. }
  96. public function projectIndexExport(Request $request)
  97. {
  98. if(!Gate::allows('客户管理-项目-查询')){ return redirect('denied'); }
  99. /** @var OwnerService $service */
  100. $service = app('OwnerService');
  101. $withs = ['customer',"userOwnerGroup","contracts","ownerStoragePriceModels","ownerAreaReport"=>function($query){
  102. $month = date('Y-m');
  103. /** @var Builder $query */
  104. $query->where("counting_month","like",$month."%");
  105. }];
  106. $params = $request->input();
  107. $params['customer_id']=true;
  108. if ($request->checkAllSign ?? false) unset($params['checkAllSign']);
  109. else $params = ["id"=>$request->data ?? ''];
  110. $owners = $service->get($params,$withs);
  111. $column = ["客户","税率","项目","货主代码","创建日期","合同号","销售名称","公司全称","联系人","联系电话","项目小组","用仓类型","当月结算面积","月单量预警","是否激活","项目描述"];
  112. $list = [];
  113. foreach ($owners as $owner){
  114. $list[] = [
  115. $owner->customer ? $owner->customer->name : '',
  116. $owner->tax_rate,
  117. $owner->name,
  118. $owner->code,
  119. $owner->created_at,
  120. implode("\r\n",array_column($owner->contracts,"contract_number")),
  121. implode("\r\n",array_column($owner->contracts,"salesman")),
  122. $owner->customer ? $owner->customer->company_name : '',
  123. $owner->linkman,
  124. $owner->phone_number,
  125. $owner->userOwnerGroup ? $owner->userOwnerGroup->name : '',
  126. implode(",",array_unique(array_column(($owner->ownerStoragePriceModels)->toArray(),"using_type"))),
  127. $owner->ownerAreaReport ? $owner->ownerAreaReport->accounting_area : '',
  128. $owner->waring_line_on,
  129. $owner->deleted_at ? '否' : '是',
  130. $owner->description
  131. ];
  132. }
  133. $post = Http::post(config('go.export.url'),['type'=>'base','data'=>json_encode(["row"=>$column,"list"=>$list],JSON_UNESCAPED_UNICODE)]);
  134. if ($post->status() == 500){
  135. throw new Exception($post->header("Msg"));
  136. }
  137. return response($post,200, [
  138. "Content-type"=>"application/octet-stream",
  139. "Content-Disposition"=>"attachment; filename=客户报表-".date('ymdHis').'.xlsx',
  140. ]);
  141. }
  142. public function projectCreate()
  143. {
  144. if(!Gate::allows('客户管理-项目-录入')){ return redirect('denied'); }
  145. $customers = app('CustomerService')->getSelection();
  146. $ownerGroups = app('UserOwnerGroupService')->getSelection();
  147. $warehouses = app('WarehouseService')->getSelection();
  148. $owner = null;
  149. return response()->view('customer.project.create',compact("customers","ownerGroups","owner","warehouses"));
  150. }
  151. public function projectUpdate()
  152. {
  153. $this->gate("客户管理-项目-录入");
  154. if (!request("id"))$this->error("项目不存在,无法补充详细信息");
  155. $errors = $this->validator(request()->input())->errors();
  156. if (count($errors)>0)$this->success(["errors"=>$errors]);
  157. /** @var Owner $owner */
  158. $owner = app('OwnerService')->find(request("id"));
  159. if (!$owner)$this->error("项目已被删除,无法操作");
  160. $owner = app('OwnerService')->update($owner,[
  161. "customer_id" => request("customer_id"),
  162. "warehouse_id" => request("warehouse_id"),
  163. "tax_rate" => request("tax_rate"),
  164. "linkman" => request("linkman"),
  165. "phone_number" => request("phone_number"),
  166. "user_owner_group_id" => request("owner_group_id"),
  167. "waring_line_on" => request("waring_line_on"),
  168. "description" => request("description"),
  169. ]);
  170. $this->success($owner);
  171. }
  172. //获取货主下所有相关计费模型
  173. public function getOwnerPriceModel(Request $request)
  174. {
  175. $owner = new Owner();
  176. $owner->id = $request->id;
  177. $owner->load(["ownerPriceOperations","ownerPriceExpresses","ownerPriceLogistics","ownerPriceDirectLogistics"]);
  178. return ["success"=>true,"data"=>["ownerPriceOperations"=>$owner->ownerPriceOperations,
  179. "ownerPriceExpresses"=>$owner->ownerPriceExpresses,
  180. "ownerPriceLogistics"=>$owner->ownerPriceLogistics,
  181. "ownerPriceDirectLogistics"=>$owner->ownerPriceDirectLogistics]];
  182. }
  183. public function projectEdit($id)
  184. {
  185. if(!Gate::allows('客户管理-项目-编辑')){ return redirect('denied'); }
  186. /** @var Owner $owner */
  187. $owner = app('OwnerService')->find($id);
  188. $owner->loadCount(["ownerStoragePriceModels","ownerPriceOperations","ownerPriceExpresses","ownerPriceLogistics","ownerPriceDirectLogistics"]);
  189. $isExist = false;
  190. if($owner->owner_storage_price_models_count ||
  191. $owner->owner_price_operations_count ||
  192. $owner->owner_price_expresses_count ||
  193. $owner->owner_price_logistics_count ||
  194. $owner->owner_price_direct_logistics_count) $isExist = true;
  195. $customers = app('CustomerService')->getSelection();
  196. $ownerGroups = app('UserOwnerGroupService')->getSelection();
  197. $warehouses = app('WarehouseService')->getSelection();
  198. return response()->view('customer.project.create',compact("customers","ownerGroups","warehouses",'owner',"isExist"));
  199. }
  200. public function projectArea(Request $request)
  201. {
  202. if(!Gate::allows('客户管理-项目-面积')){ return redirect('denied'); }
  203. $areas = app('OwnerAreaReportService')->paginate($request->input(),["owner"=>function($query){$query->with(["customer","ownerStoragePriceModels"]);}]);
  204. $ownerGroups = app('UserOwnerGroupService')->getSelection();
  205. $customers = app('CustomerService')->getSelection();
  206. $owners = app('OwnerService')->getIntersectPermitting();
  207. $params = $request->input();
  208. return response()->view('customer.project.area',compact("areas","ownerGroups","customers","owners","params"));
  209. }
  210. public function updateArea(Request $request)
  211. {
  212. if(!Gate::allows('客户管理-项目-面积-编辑')){ return ["success"=>false,'data'=>"无权操作!"]; }
  213. if (!($request->id ?? false) || !($request->area ?? false)) return ["success"=>false,'data'=>"传递错误!"];
  214. $values = $request->area ?? null;
  215. if (!$values)return ["success"=>true,"data"=>$values];
  216. foreach ($values as $column=>$value){
  217. if ($value && (!is_numeric($value) || $value<0))return ["success"=>false,'data'=>$column."非数字或小于0!"];
  218. }
  219. $accounting_area = ((int)$values["area_on_tray"]*2.5) + ((int)$values["area_on_half_tray"]*1.8) + ((int)$values["area_on_flat"]*1.3);
  220. $values["accounting_area"] = $accounting_area;
  221. $row = app('OwnerAreaReportService')->update(["id"=>$request->id],$values);
  222. if ($row==1){
  223. LogService::log(__METHOD__,"客户管理-修改面积",json_encode($request->input()));
  224. return ["success"=>true,"data"=>$values];
  225. }
  226. return ["success"=>false,"data"=>"影响了".$row."条数据!"];
  227. }
  228. public function projectAreaExport(Request $request)
  229. {
  230. if(!Gate::allows('客户管理-项目-面积')){ return redirect('denied'); }
  231. $params = $request->input();
  232. if ($request->checkAllSign)unset($params['checkAllSign']);
  233. else $params = ["id"=>$request->data];
  234. /** @var OwnerAreaReportService $serves */
  235. $serves = app('OwnerAreaReportService');
  236. $areas = $serves->get($params,["owner"=>function($query){$query->with(["customer","ownerStoragePriceModels","userOwnerGroup"]);}]);
  237. $column = ["状态","项目组","客户","子项目","结算月","录入时间","用仓类型","货物整托","货物半托","平面区面积","结算面积"];
  238. $list = [];
  239. foreach ($areas as $area){
  240. $list[] = [
  241. $area->status,
  242. $area->owner ? ($area->owner->userOwnerGroup ? $area->owner->userOwnerGroup->name : '') : '',
  243. $area->owner ? ($area->owner->customer ? $area->owner->customer->name : '') : '',
  244. $area->owner ? $area->owner->name : '',
  245. $area->counting_month,
  246. $area->updated_at,
  247. $area->owner ? implode(",",array_unique(array_column(($area->owner->ownerStoragePriceModels)->toArray(),"using_type"))) : '',
  248. $area->area_on_tray,
  249. $area->area_on_half_tray,
  250. $area->area_on_flat,
  251. $area->accounting_area,
  252. ];
  253. }
  254. $post = Http::post(config('go.export.url'),['type'=>'base','data'=>json_encode(["row"=>$column,"list"=>$list],JSON_UNESCAPED_UNICODE)]);
  255. if ($post->status() == 500){
  256. throw new Exception($post->header("Msg"));
  257. }
  258. return response($post,200, [
  259. "Content-type"=>"application/octet-stream",
  260. "Content-Disposition"=>"attachment; filename=项目面积报表-".date('ymdHis').'.xlsx',
  261. ]);
  262. }
  263. public function financeInstantBill(Request $request)
  264. {
  265. if(!Gate::allows('客户管理-财务-即时账单')){ return redirect('denied'); }
  266. $params = $request->input();
  267. $shops = app('ShopService')->getSelection();
  268. $customers = app('CustomerService')->getSelection();
  269. $owners = app('OwnerService')->getIntersectPermitting();
  270. $details = app('OwnerFeeDetailService')->paginate($params,["owner"=>function($query){$query->with("customer");},"shop","processMethod","logistic"]);
  271. return response()->view('customer.finance.instantBill',compact("details","params","shops","customers","owners"));
  272. }
  273. public function financeInstantBillExport(Request $request)
  274. {
  275. if(!Gate::allows('客户管理-财务-即时账单')){ return redirect('denied'); }
  276. $params = $request->input();
  277. if ($request->checkAllSign)unset($params['checkAllSign']);
  278. else $params = ["id"=>$request->data];
  279. $sql = app('OwnerFeeDetailService')->getSql($params);
  280. $row = ["客户", "项目", "作业时间", "类型","店铺", "单号(发/收/退/提)", "收件人", "收件人电话", "商品数量",
  281. "物流/快递单号", "体积", "重量", "承运商", "操作费", "物流费", "合计"];
  282. $column = ["customer_name", "owner_name", "worked_at", "type","shop_name", "operation_bill", "consignee_name", "consignee_phone", "commodity_amount",
  283. "logistic_bill", "volume", "weight", "logistic_name", "work_fee", "logistic_fee", "total"];
  284. $rule = ["work_fee"=>"mysqlDate"];
  285. $post = Http::post(config('go.export.url'),['type'=>'unify','sql'=>$sql, 'connection'=>'mysql',
  286. 'row'=>json_encode($row,JSON_UNESCAPED_UNICODE), 'column'=>json_encode($column), 'rule'=>json_encode($rule)]);
  287. if ($post->status() == 500){
  288. throw new Exception($post->header("Msg"));
  289. }
  290. return response($post,200, [
  291. "Content-type"=>"application/octet-stream",
  292. "Content-Disposition"=>"attachment; filename=即时账单记录-".date('ymdHis').'.xlsx',
  293. ]);
  294. }
  295. public function financeBillConfirmation(Request $request)
  296. {
  297. if(!Gate::allows('客户管理-财务-账单确认')){ return redirect('denied'); }
  298. $params = $request->input();
  299. $ownerGroups = app('UserOwnerGroupService')->getSelection();
  300. $customers = app('CustomerService')->getSelection();
  301. $owners = app('OwnerService')->getIntersectPermitting();
  302. $bills = app('OwnerBillReportService')->paginate($params,["owner"=>function($query){
  303. /** @var Builder $query */
  304. $query->with(["customer","userOwnerGroup"]);
  305. }]);
  306. return response()->view('customer.finance.billConfirmation',compact("params","owners","ownerGroups","customers","bills"));
  307. }
  308. public function financeBillConfirmationExport(Request $request)
  309. {
  310. if(!Gate::allows('客户管理-财务-账单确认')){ return redirect('denied'); }
  311. $params = $request->input();
  312. if ($request->checkAllSign)unset($params['checkAllSign']);
  313. else $params = ["id"=>$request->data];
  314. /** @var OwnerBillReportService $serves */
  315. $serves = app('OwnerBillReportService');
  316. $bills = $serves->get($params,["owner"=>function($query){
  317. /** @var Builder $query */
  318. $query->with(["customer","userOwnerGroup"]);
  319. }]);
  320. $column = ["项目小组","客户","子项目","结算月","录入日期","原始账单金额","确认账单金额","差额","状态"];
  321. $list = [];
  322. foreach ($bills as $bill){
  323. $list[] = [
  324. $bill->owner ? ($bill->owner->userOwnerGroup ? $bill->owner->userOwnerGroup->name : '') : '',
  325. $bill->owner ? ($bill->owner->customer ? $bill->owner->customer->name : '') : '',
  326. $bill->owner ? $bill->owner->name : '',
  327. $bill->counting_month,
  328. $bill->updated_at,
  329. $bill->initial_fee,
  330. $bill->confirm_fee,
  331. $bill->difference,
  332. $bill->confirmed == '是' ? '已确认' : '未确认',
  333. ];
  334. }
  335. $post = Http::post(config('go.export.url'),['type'=>'base','data'=>json_encode(["row"=>$column,"list"=>$list],JSON_UNESCAPED_UNICODE)]);
  336. if ($post->status() == 500){
  337. throw new Exception($post->header("Msg"));
  338. }
  339. return response($post,200, [
  340. "Content-type"=>"application/octet-stream",
  341. "Content-Disposition"=>"attachment; filename=客户账单报表-".date('ymdHis').'.xlsx',
  342. ]);
  343. }
  344. public function updateBillReport(Request $request)
  345. {
  346. if(!Gate::allows('客户管理-财务-账单确认-编辑')){ return ["success"=>false,'data'=>"无权操作!"]; }
  347. if (!$request->confirm_fee || !is_numeric($request->confirm_fee) || $request->confirm_fee<0)return ["success"=>false,"data"=>"非法金额参数"];
  348. $date = date('Y-m-d H:i:s');
  349. app('OwnerBillReportService')->update(["id"=>$request->id],["confirm_fee"=>$request->confirm_fee,"difference"=>DB::raw($request->confirm_fee.'- initial_fee'),"updated_at"=>$date]);
  350. LogService::log(__METHOD__,"客户管理-修改账单报表",json_encode($request->input()));
  351. return ["success"=>true,"data"=>$date];
  352. }
  353. public function billConfirm(Request $request)
  354. {
  355. if(!Gate::allows('客户管理-财务-账单确认-完结')){ return ["success"=>false,'data'=>"无权操作!"]; }
  356. if (!($request->id ?? false))return["success"=>false,"data"=>"非法参数"];
  357. app('OwnerBillReportService')->update(["id"=>$request->id],["confirmed"=>"是"]);
  358. LogService::log(__METHOD__,"客户管理-确认账单",json_encode($request->input()));
  359. $bill = app('OwnerBillReportService')->first(["id"=>$request->id,"confirmed"=>"是"]);
  360. app('OwnerAreaReportService')->lockArea(null, $bill->owner_id, $bill->counting_month);
  361. LogService::log(__METHOD__,"客户管理-锁定账单的所有面积",json_encode($bill,JSON_UNESCAPED_UNICODE));
  362. return ["success"=>true];
  363. }
  364. private function validator(array $params){
  365. $validator=Validator::make($params,[
  366. 'id' => ['required'],
  367. 'customer_id'=>['required'],
  368. 'owner_group_id'=>['required'],
  369. 'warehouse_id'=>['required'],
  370. 'tax_rate' => ["nullable",'numeric'],
  371. 'waring_line_on' => ["nullable",'integer'],
  372. ],[
  373. 'required'=>':attribute 为必填项',
  374. 'integer'=>':attribute 必须为整数',
  375. 'numeric'=>':attribute 必须为数字',
  376. ],[
  377. 'code'=>'项目代码',
  378. 'name'=>'项目名称',
  379. 'warehouse_id'=>'仓库',
  380. 'customer_id'=>'客户',
  381. 'owner_group_id'=>'工作组',
  382. 'tax_rate' => '税率',
  383. 'waring_line_on' => '月单量预警'
  384. ]);
  385. return $validator;
  386. }
  387. public function verifyProject(Request $request)
  388. {
  389. $this->success($this->validator($request->input())->errors());
  390. }
  391. }