SettlementBillOwnerAreaFeeController.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Owner;
  4. use App\OwnerAreaReport;
  5. use App\OwnerBillReport;
  6. use App\OwnerBillReportArchive;
  7. use Illuminate\Http\Request;
  8. /**
  9. * 结算管理-结算账单-仓储费
  10. * Class SettlementBillOwnerAreaFeeController
  11. * @package App\Http\Controllers
  12. */
  13. class SettlementBillOwnerAreaFeeController extends Controller
  14. {
  15. /**
  16. * SettlementBillOwnerAreaFeeController constructor.
  17. */
  18. public function __construct()
  19. {
  20. $this->middleware('auth');
  21. }
  22. public function index(Request $request)
  23. {
  24. list($permittingOwnerIds, $counting_month, $owner_id) = $this->getRequestParams($request);
  25. $isArchived = $this->ownerBillReportArchiveQuery($counting_month, $owner_id)->exists();
  26. $isArchived = $isArchived ? 1 : 2;
  27. list($areaReports, $billReport, $price) = $this->get($owner_id, $counting_month);
  28. $owners = Owner::query()->find($permittingOwnerIds);
  29. $owner = Owner::query()->find($owner_id);
  30. return view('finance.settlementBills.areaFee.index', compact('owner', 'owners', 'areaReports', 'billReport', 'price', 'request', 'isArchived'));
  31. }
  32. /**
  33. * @param Request $request year month owner_id
  34. * @return array
  35. */
  36. private function getRequestParams(Request $request): array
  37. {
  38. $this->service = app('OwnerLogisticFeeReportService');
  39. $this->userService = app('UserService');
  40. $permittingOwnerIds = $this->userService->getPermittingOwnerIds(auth()->user());
  41. if (is_null($request->year) || is_null($request->month)) {
  42. $counting_month = now()->subMonth()->startOfMonth()->toDateString();
  43. } else {
  44. $counting_month = $request->year . '-' . $request->month . '-' . '01';
  45. }
  46. if (is_null($request->owner_id)) {
  47. $owner_id = $permittingOwnerIds[0];
  48. } else {
  49. $owner_id = $request->owner_id;
  50. }
  51. return array($permittingOwnerIds, $counting_month, $owner_id);
  52. }
  53. public function confirmBill(Request $request)
  54. {
  55. list($permittingOwnerIds, $counting_month, $owner_id) = $this->getRequestParams($request);
  56. list($areaReports, $billReport, $price) = $this->get($owner_id, $counting_month);
  57. $information = [
  58. 'areaReports' => $areaReports,
  59. 'billReport' => $billReport,
  60. 'price' => $price,
  61. ];
  62. OwnerBillReportArchive::query()->create([
  63. 'owner_bill_report_id' => $billReport->id ?? null,
  64. 'owner_id' => $owner_id,
  65. 'counting_mouth' => $counting_month,
  66. 'type' => '仓储费',
  67. 'archiver_id' => auth()->id(),
  68. 'archived_at' => now(),
  69. 'information' => $information,
  70. ]);
  71. return back();
  72. }
  73. /**
  74. * @param $owner_id
  75. * @param $counting_month
  76. * @return array
  77. */
  78. private function get($owner_id, $counting_month): array
  79. {
  80. $archived = $this->ownerBillReportArchiveQuery($counting_month, $owner_id)->first();
  81. if ($archived ?? false) {
  82. $areaReports = collect($archived->information['areaReports']);
  83. $billReport = collect($archived->information['billReport']);
  84. $price = $archived->information['price'];
  85. } else {
  86. $areaReports = OwnerAreaReport::query()
  87. ->with('ownerStoragePriceModel:id,using_type,price')
  88. ->where('owner_id', $owner_id)
  89. ->where('counting_month', $counting_month)
  90. ->get();
  91. $billReport = OwnerBillReport::query()
  92. ->selectRaw('storage_fee')
  93. ->where('owner_id', $owner_id)
  94. ->where('counting_month', $counting_month)
  95. ->firstOr(function () {
  96. return new OwnerBillReport();
  97. });
  98. $totalArea = $areaReports->reduce(function ($carry, $areaReport) {
  99. return $carry + $areaReport->accounting_area;
  100. }, 0);
  101. try {
  102. $price = $billReport->storage_fee ?? 0 / $totalArea;
  103. } catch (\Exception $e) {
  104. $price = 0;
  105. }
  106. }
  107. return array($areaReports, $billReport, $price);
  108. }
  109. /**
  110. * @param $counting_month
  111. * @param $owner_id
  112. * @return \Illuminate\Database\Eloquent\Builder
  113. */
  114. private function ownerBillReportArchiveQuery($counting_month, $owner_id): \Illuminate\Database\Eloquent\Builder
  115. {
  116. return OwnerBillReportArchive::query()->where('counting_mouth', $counting_month)
  117. ->where('owner_id', $owner_id)->where('type', '仓储费');
  118. }
  119. }