CustomerController.php 22 KB

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