CustomerController.php 21 KB

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