OwnerLogisticFeeDetailController.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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\OwnerLogisticFeeDetailService;
  8. use Carbon\Carbon;
  9. use Illuminate\Http\RedirectResponse;
  10. use Illuminate\Http\Request;
  11. use Oursdreams\Export\Export;
  12. class OwnerLogisticFeeDetailController extends Controller
  13. {
  14. /** @var $service OwnerLogisticFeeDetailService */
  15. private $service;
  16. /** @var $archiveService OwnerBillReportArchiveService */
  17. private $archiveService;
  18. /**
  19. * Display a listing of the resource.
  20. *
  21. */
  22. public function index(Request $request)
  23. {
  24. $paginateParams = $request->input();
  25. list($permittingOwnerIds, $owner_id, $start, $end) = $this->getRequestParams($request->owner_id, $request->year, $request->month);
  26. $details = $this->service->getDetails($owner_id, $start, $end, $paginateParams);
  27. $owners = Owner::query()->selectRaw("id,name")->whereIn('id', $permittingOwnerIds)->get();
  28. $owner = Owner::query()->selectRaw("name,id")->find($owner_id);
  29. $this->archiveService = app('OwnerBillReportArchiveService');
  30. $isArchived = $this->archiveService->isArchived($start, $owner_id, OwnerBillReportArchive::$enums['type']['快递费-明细']);
  31. $request = collect($request->all());
  32. return view('finance.settlementBills.logisticFee.detail.index', compact('details', 'paginateParams', 'owners', 'owner', 'request','isArchived'));
  33. }
  34. public function export(Request $request)
  35. {
  36. list($permittingOwnerIds, $owner_id, $start, $end) = $this->getRequestParams($request->owner_id, $request->year, $request->month);
  37. $query = $this->service->getSql($owner_id, $start, $end);
  38. if (!$request->exists('checkAllSign')) {
  39. $query->whereIn('id', explode(',', $request['data']));
  40. }
  41. $details = $this->service->buildDetails($query->get());
  42. $json = [];
  43. foreach ($details as $detail) {
  44. $json[] = array_values($detail);
  45. }
  46. $row = ['主键', '快递公司', '省份', '快递单号', '重量', '首重价格', '续重价格', '快递费',];
  47. return Export::make($row, $json, "快递费用详情");
  48. }
  49. /**
  50. * @param $owner_id
  51. * @param $year
  52. * @param $month
  53. * @return array
  54. */
  55. private function getRequestParams($owner_id, $year, $month): array
  56. {
  57. $this->service = app('OwnerLogisticFeeDetailService');
  58. $this->userService = app('UserService');
  59. $permittingOwnerIds = $this->userService->getPermittingOwnerIds(auth()->user());
  60. if (is_null($owner_id)) {
  61. $owner_id = $permittingOwnerIds[0];
  62. }
  63. if (is_null($year)) {
  64. $year = now()->subMonth()->year;
  65. }
  66. if (is_null($month)) {
  67. $month = now()->subMonth()->month;
  68. }
  69. $day = Carbon::parse($year . '-' . $month . '-01');
  70. return array($permittingOwnerIds, $owner_id, $day->startOfMonth()->toDateString(), $day->endOfMonth()->toDateString());
  71. }
  72. /**
  73. * 确认账单
  74. * @param Request $request
  75. * @return RedirectResponse
  76. */
  77. public function confirmBill(Request $request)
  78. {
  79. $this->service = app('OwnerLogisticFeeDetailService');
  80. $this->archiveService = app('OwnerBillReportArchiveService');
  81. list($permittingOwnerIds, $owner_id, $start, $end) = $this->getRequestParams($request->owner_id, $request->year, $request->month);
  82. $billReport = OwnerBillReport::query()
  83. ->select('storage_fee', 'id')
  84. ->where('owner_id', $owner_id)
  85. ->where('counting_month', $start)
  86. ->firstOr(function () {
  87. return new OwnerBillReport();
  88. });
  89. OwnerBillReportArchive::query()->create([
  90. 'owner_bill_report_id' => $billReport->id ?? null,
  91. 'owner_id' => $owner_id,
  92. 'counting_mouth' => $start,
  93. 'type' => $this->service::TYPE,
  94. 'archiver_id' => auth()->id(),
  95. 'archived_at' => now(),
  96. 'information' => [],
  97. ]);
  98. return back()->with('success', '确认成功');
  99. }
  100. }