CustomerController.php 22 KB

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