SettlementBillExpressFeeReportController.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Owner;
  4. use App\OwnerBillReport;
  5. use App\OwnerBillReportArchive;
  6. use App\Services\OwnerBillReportArchiveService;
  7. use App\Services\OwnerLogisticFeeReportService;
  8. use App\Services\UserService;
  9. use App\Traits\SettlementBillTrait;
  10. use Illuminate\Http\RedirectResponse;
  11. use Illuminate\Http\Request;
  12. use Illuminate\Pagination\LengthAwarePaginator;
  13. use Oursdreams\Export\Export;
  14. class SettlementBillExpressFeeReportController extends Controller
  15. {
  16. use SettlementBillTrait;
  17. /* @var OwnerLogisticFeeReportService $service */
  18. private $service;
  19. /* @var UserService $userService */
  20. private $userService;
  21. /** @var $archiveService OwnerBillReportArchiveService */
  22. private $archiveService;
  23. /**
  24. * Display a listing of the resource.
  25. */
  26. public function index(Request $request)
  27. {
  28. $paginateParams = $request->input();
  29. list($permittingOwnerIds, $counting_month, $owner_id) = $this->getRequestParams($request->year, $request->month, $request->owner_id);
  30. list($reports, $recordTotal) = $this->service->get([
  31. 'owner_id' => $owner_id,
  32. 'counting_month' => $counting_month,
  33. 'paginateParams' => $paginateParams,
  34. 'type' => $this->service::TYPE,
  35. ]);
  36. $reportPaginator = null;
  37. if ($reports instanceof LengthAwarePaginator) {
  38. $reportPaginator = $reports;
  39. $reports = collect($reportPaginator->items());
  40. }
  41. $owner = Owner::query()->selectRaw("name,id")->find($owner_id);
  42. $owners = Owner::query()->selectRaw("id,name")->whereIn('id', $permittingOwnerIds)->get();
  43. $this->archiveService = app('OwnerBillReportArchiveService');
  44. $isArchived = $this->archiveService->isArchived($counting_month, $owner_id, OwnerBillReportArchive::$enums['type']['快递费-合计']);
  45. $request = $this->buildRequest($request, $counting_month,$owner_id);
  46. return view('finance.settlementBills.expressFee.report.index', compact('reports', 'recordTotal', 'paginateParams', 'owners', 'owner', 'isArchived', 'request', 'reportPaginator'));
  47. }
  48. public function export(Request $request)
  49. {
  50. $this->archiveService = app('OwnerBillReportArchiveService');
  51. list($permittingOwnerIds, $counting_month, $owner_id) = $this->getRequestParams($request->year, $request->month, $request->owner_id);
  52. if ($this->archiveService->isArchived($counting_month, $owner_id, $this->service::TYPE) == 1) {
  53. //已确认账单导出
  54. list($reports, $recordTotal) = $this->service->get([
  55. 'owner_id' => $owner_id,
  56. 'counting_month' => $counting_month,
  57. 'type' => $this->service::TYPE,
  58. ]);
  59. } else {
  60. //未确认账单导出
  61. $query = $this->service->getSql($owner_id, $counting_month);
  62. if (!$request->exists('checkAllSign')) {
  63. $query->whereIn('id', explode(',', $request['data']));
  64. }
  65. $reports = $query->get();
  66. }
  67. $json = [];
  68. foreach ($reports as $report) {
  69. $json[] = [
  70. $report['logistic']['name'] ?? '',
  71. $report['province']['name'],
  72. $report['initial_weight'],
  73. $report['initial_amount'],
  74. $report['additional_weight'],
  75. $report['additional_amount'],
  76. $report['fee'],
  77. ];
  78. }
  79. $row = ['快递公司', '地区', '首重', '订单数', '续重', '续重合计', '(省份)合计'];
  80. return Export::make($row, $json, "快递费用合计");
  81. }
  82. /**
  83. * @param $year
  84. * @param $month
  85. * @param $owner_id
  86. * @return array
  87. */
  88. private function getRequestParams($year, $month, $owner_id): array
  89. {
  90. $this->service = app('OwnerLogisticFeeReportService');
  91. $this->userService = app('UserService');
  92. $permittingOwnerIds = $this->userService->getPermittingOwnerIds(auth()->user());
  93. if (is_null($year)) {
  94. $year = now()->subMonth() ->year;
  95. }
  96. if (is_null($month)) {
  97. $month = now()->subMonth()->month;
  98. }
  99. $counting_month = $year . '-' . $month . '-' . '01';
  100. if (is_null($owner_id)) {
  101. $owner_id = $permittingOwnerIds[0];
  102. }
  103. return array($permittingOwnerIds, $counting_month, $owner_id);
  104. }
  105. /**
  106. * 确认账单
  107. * @param Request $request
  108. * @return RedirectResponse
  109. */
  110. public function confirmBill(Request $request):RedirectResponse
  111. {
  112. $this->service = app('OwnerLogisticFeeReportService');
  113. list($permittingOwnerIds, $counting_month, $owner_id) = $this->getRequestParams($request->year, $request->month, $request->owner_id);
  114. $this->service->confirmBill($counting_month, $owner_id);
  115. return back()->with('success', '确认成功');
  116. }
  117. }