OwnerLogisticFeeReportController.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Owner;
  4. use App\OwnerLogisticFeeReport;
  5. use App\Services\common\ExportService;
  6. use App\Services\OwnerLogisticFeeReportService;
  7. use App\Services\UserService;
  8. use Illuminate\Http\Request;
  9. use Oursdreams\Export\Export;
  10. class OwnerLogisticFeeReportController extends Controller
  11. {
  12. /* @var OwnerLogisticFeeReportService $service */
  13. private $service;
  14. /* @var UserService $userService */
  15. private $userService;
  16. /**
  17. * OwnerLogisticFeeReportController constructor.
  18. */
  19. public function __construct()
  20. {
  21. $this->middleware('auth');
  22. }
  23. /**
  24. * Display a listing of the resource.
  25. */
  26. public function index(Request $request)
  27. {
  28. $paginateParams = $request->input();
  29. list($permittingOwnerIds, $date, $owner_id) = $this->getRequestParams($request);
  30. $reports = $this->service->getRecordPagination($owner_id, $date, $paginateParams);
  31. $recordTotal = $this->service->getRecordTotal($owner_id, $date);
  32. $owner = Owner::query()->selectRaw("name")->find($owner_id);
  33. $owners = Owner::query()->selectRaw("id,name")->whereIn('id', $permittingOwnerIds)->get();
  34. return view('finance.settlementBills.logisticFee.report.index', compact('reports', 'recordTotal', 'paginateParams', 'owners', 'owner'));
  35. }
  36. public function export(Request $request)
  37. {
  38. list($permittingOwnerIds, $date, $owner_id) = $this->getRequestParams($request);
  39. $query = $this->service->getSql($owner_id, $date);
  40. if (!$request->exists('checkAllSign')) {
  41. $query->whereIn('id', explode(',', $request['data']));
  42. }
  43. $reports = $query->get();
  44. $json = [];
  45. foreach ($reports as $report) {
  46. $json[] = [
  47. $report->logistic->name ?? '',
  48. $report->province,
  49. $report->initial_weight,
  50. $report->initial_amount,
  51. $report->additional_weight,
  52. $report->additional_amount,
  53. $report->fee,
  54. ];
  55. }
  56. $row = ['快递公司', '地区', '首重', '订单数', '续重', '续重合计', '(省份)合计'];
  57. return Export::make($row, $json, "快递费用合计");
  58. }
  59. /**
  60. * @param Request $request
  61. * @return array
  62. */
  63. private function getRequestParams(Request $request): array
  64. {
  65. $this->service = app('OwnerLogisticFeeReportService');
  66. $this->userService = app('UserService');
  67. $permittingOwnerIds = $this->userService->getPermittingOwnerIds(auth()->user());
  68. if (is_null($request->year) || is_null($request->month)) {
  69. $date = now()->subMonth()->startOfMonth()->toDateString();
  70. } else {
  71. $date = $request->year . '-' . $request->month . '-' . '01';
  72. }
  73. if (is_null($request->owner_id)) {
  74. $owner_id = $permittingOwnerIds[0];
  75. } else {
  76. $owner_id = $request->owner_id;
  77. }
  78. return array($permittingOwnerIds, $date, $owner_id);
  79. }
  80. }