CustomerController.php 22 KB

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