OwnerLogisticFeeReportController.php 5.9 KB

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