OwnerLogisticFeeDetailController.php 4.3 KB

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