OwnerLogisticFeeDetailController.php 4.2 KB

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