CustomerController.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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\Auth;
  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')->getSelection();
  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. $customers = $service->get(Auth::user(),['customer_id'=>true],['customer']);
  140. return response()->view('customer.project.index');
  141. }
  142. public function projectCreate()
  143. {
  144. if(!Gate::allows('客户管理-项目-录入')){ return redirect('denied'); }
  145. return response()->view('customer.project.create');
  146. }
  147. public function projectArea()
  148. {
  149. if(!Gate::allows('客户管理-项目-面积')){ return redirect('denied'); }
  150. return response()->view('customer.project.area');
  151. }
  152. public function financeInstantBill()
  153. {
  154. if(!Gate::allows('客户管理-财务-即时账单')){ return redirect('denied'); }
  155. return response()->view('customer.finance.instantBill');
  156. }
  157. public function financeBillConfirmation()
  158. {
  159. if(!Gate::allows('客户管理-财务-账单确认')){ return redirect('denied'); }
  160. return response()->view('customer.finance.billConfirmation');
  161. }
  162. }