CustomerController.php 21 KB

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