OwnerLogisticFeeReportController.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 Illuminate\Http\RedirectResponse;
  10. use Illuminate\Http\Request;
  11. use Illuminate\Pagination\LengthAwarePaginator;
  12. use Oursdreams\Export\Export;
  13. class OwnerLogisticFeeReportController extends Controller
  14. {
  15. /* @var OwnerLogisticFeeReportService $service */
  16. private $service;
  17. /* @var UserService $userService */
  18. private $userService;
  19. /** @var $archiveService OwnerBillReportArchiveService */
  20. private $archiveService;
  21. /**
  22. * OwnerLogisticFeeReportController constructor.
  23. */
  24. public function __construct()
  25. {
  26. $this->middleware('auth');
  27. }
  28. /**
  29. * Display a listing of the resource.
  30. */
  31. public function index(Request $request)
  32. {
  33. $paginateParams = $request->input();
  34. list($permittingOwnerIds, $counting_month, $owner_id) = $this->getRequestParams($request->year, $request->month, $request->owner_id);
  35. list($reports, $recordTotal) = $this->service->get([
  36. 'owner_id' => $owner_id,
  37. 'counting_month' => $counting_month,
  38. 'paginateParams' => $paginateParams,
  39. 'type' => $this->service::TYPE,
  40. ]);
  41. $reportPaginator = null;
  42. if ($reports instanceof LengthAwarePaginator) {
  43. $reportPaginator = $reports;
  44. $reports = collect($reportPaginator->items());
  45. }
  46. $owner = Owner::query()->selectRaw("name,id")->find($owner_id);
  47. $owners = Owner::query()->selectRaw("id,name")->whereIn('id', $permittingOwnerIds)->get();
  48. $this->archiveService = app('OwnerBillReportArchiveService');
  49. $isArchived = $this->archiveService->isArchived($counting_month, $owner_id, OwnerBillReportArchive::$enums['type']['快递费-合计']);
  50. $request = $request->all();
  51. $request['year'] = \Carbon\Carbon::parse($counting_month)->year;
  52. $request['month'] = \Carbon\Carbon::parse($counting_month)->month;
  53. $request = collect($request);
  54. return view('finance.settlementBills.logisticFee.report.index', compact('reports', 'recordTotal', 'paginateParams', 'owners', 'owner', 'isArchived', 'request', 'reportPaginator'));
  55. }
  56. public function export(Request $request)
  57. {
  58. $this->archiveService = app('OwnerBillReportArchiveService');
  59. list($permittingOwnerIds, $counting_month, $owner_id) = $this->getRequestParams($request->year, $request->month, $request->owner_id);
  60. if ($this->archiveService->isArchived($counting_month, $owner_id, $this->service::TYPE) == 1) {
  61. //已确认账单导出
  62. list($reports, $recordTotal) = $this->service->get([
  63. 'owner_id' => $owner_id,
  64. 'counting_month' => $counting_month,
  65. 'type' => $this->service::TYPE,
  66. ]);
  67. } else {
  68. //未确认账单导出
  69. $query = $this->service->getSql($owner_id, $counting_month);
  70. if (!$request->exists('checkAllSign')) {
  71. $query->whereIn('id', explode(',', $request['data']));
  72. }
  73. $reports = $query->get();
  74. }
  75. $json = [];
  76. foreach ($reports as $report) {
  77. $json[] = [
  78. $report['logistic']['name'] ?? '',
  79. $report['province'],
  80. $report['initial_weight'],
  81. $report['initial_amount'],
  82. $report['additional_weight'],
  83. $report['additional_amount'],
  84. $report['fee'],
  85. ];
  86. }
  87. $row = ['快递公司', '地区', '首重', '订单数', '续重', '续重合计', '(省份)合计'];
  88. return Export::make($row, $json, "快递费用合计");
  89. }
  90. /**
  91. * @param $year
  92. * @param $month
  93. * @param $owner_id
  94. * @return array
  95. */
  96. private function getRequestParams($year, $month, $owner_id): array
  97. {
  98. $this->service = app('OwnerLogisticFeeReportService');
  99. $this->userService = app('UserService');
  100. $permittingOwnerIds = $this->userService->getPermittingOwnerIds(auth()->user());
  101. if (is_null($year)) {
  102. $year = now()->subMonth() ->year;
  103. }
  104. if (is_null($month)) {
  105. $month = now()->subMonth()->month;
  106. }
  107. $counting_month = $year . '-' . $month . '-' . '01';
  108. if (is_null($owner_id)) {
  109. $owner_id = $permittingOwnerIds[0];
  110. }
  111. return array($permittingOwnerIds, $counting_month, $owner_id);
  112. }
  113. /**
  114. * 确认账单
  115. * @param Request $request
  116. * @return RedirectResponse
  117. */
  118. public function confirmBill(Request $request)
  119. {
  120. $this->service = app('OwnerLogisticFeeReportService');
  121. $this->archiveService = app('OwnerBillReportArchiveService');
  122. list($permittingOwnerIds, $counting_month, $owner_id) = $this->getRequestParams($request->year, $request->month, $request->owner_id);
  123. $billReport = OwnerBillReport::query()
  124. ->select('storage_fee', 'id')
  125. ->where('owner_id', $owner_id)
  126. ->where('counting_month', $counting_month)
  127. ->firstOr(function () {
  128. return new OwnerBillReport();
  129. });
  130. $reports = $this->service->getRecords($owner_id, $counting_month);
  131. $recordTotal = $this->service->getRecordTotal($owner_id, $counting_month);
  132. OwnerBillReportArchive::query()->create([
  133. 'owner_bill_report_id' => $billReport->id ?? null,
  134. 'owner_id' => $owner_id,
  135. 'counting_mouth' => $counting_month,
  136. 'type' => $this->service::TYPE,
  137. 'archiver_id' => auth()->id(),
  138. 'archived_at' => now(),
  139. 'information' => [
  140. 'reports' => $reports,
  141. 'recordTotal' => $recordTotal,
  142. ],
  143. ]);
  144. return back()->with('success', '确认成功');
  145. }
  146. }